Linux 操作系统下 File.Delete 的表现

在我的上一篇随笔“对 File.Delete 方法的一点看法最新版本 1.2.5 版对 File.Delete 方法进行测试。下面是我的运行 Linux 操作系统的计算机的基本信息:ben@ibm7k:~/work$cat /etc/issue.netUbuntu 6.10ben@ibm7k:~/work$uname -rm2.6.17-12-server i686ben@ibm7k:~/work$mono –versionMono JIT compiler version 1.2.5 (tarball)Copyright (C) 2002-2007 Novell, Inc and Contributors. TLS: __thread GC: Included Boehm (with typed GC) SIGSEGV: normal Architecture: x86 Disabled: noneben@ibm7k:~/work$gmcs –versionMono C# compiler version 1.2.5.0ben@ibm7k:~/work$mono clrver.exe操作系统版本 : Unix 2.6.17.12公共语言运行库版本: 2.0.50727.42ben@ibm7k:~/work$我们用下面的一段程序来测试 File.Delete 在 Linux 操作系统下的表现:

usingSystem;usingSystem.IO;Test{Main(){try{Console.Write();Console.ReadLine();;;File.Delete(fileName);Console.WriteLine();}catch(Exceptionex){Console.WriteLine(ex.ToString());}}}

运行结果如下:

文件名直接调用 File.Delete 方法ben@ibm7k:~/work$mono test.exe

零长度字符串请输入要删除的文件名:错误: System.ArgumentException:pathat System.IO.File.Delete (System.String path) [0x00000]at Test.Main (System.String[] args) [0x00000]

非法字符请输入要删除的文件名: zero错误: System.ArgumentException:pathat System.IO.File.Delete (System.String path) [0x00000]at Test.Main (System.String[] args) [0x00000]

空引用请输入要删除的文件名: null错误: System.ArgumentNullException:Argument cannot be null.Parameter name: pathat System.IO.File.Delete (System.String path) [0x00000]at Test.Main (System.String[] args) [0x00000]

无效的路径请输入要删除的文件名: none/a.txt错误: System.IO.DirectoryNotFoundException:Destination directory not found: noneat System.IO.File.Delete (System.String path) [0x00000]at Test.Main (System.String[] args) [0x00000]

文件名太长请输入要删除的文件名: -this-string’s-length-is-256-错误: System.IO.PathTooLongException:Path is too long. Path: -this-string’s-length-is-256-at System.IO.Directory.Exists (System.String path) [0x00000]at System.IO.File.Delete (System.String path) [0x00000]at Test.Main (System.String[] args) [0x00000]

一个目录请输入要删除的文件名: /错误: System.UnauthorizedAccessException:/ is a directoryat System.IO.File.Delete (System.String path) [0x00000]at Test.Main (System.String[] args) [0x00000]

没有权限的文件请输入要删除的文件名: /etc/passwd错误: System.UnauthorizedAccessException:Access to the path "/etc/passwd" is denied.at System.IO.File.Delete (System.String path) [0x00000]at Test.Main (System.String[] args) [0x00000]

正在使用的文件请输入要删除的文件名: test.exeFile.Delete 成功

只读文件请输入要删除的文件名: readonly.fileFile.Delete 成功

不存在的文件请输入要删除的文件名: none.fileFile.Delete 成功

正常的文件请输入要删除的文件名: readwrite1.fileFile.Delete 成功

几点说明:1. 在上面的表格中,加粗的黑色文字是将程序中的“ex.ToString()”替换为“ex.Message”时显示的内容,这正是我们平常显示错误信息的方法。但是表格中的“零长度字符串”和“非法字符”的情况下错误信息只有“path”,这对用户来说是非常不友好的。在 Windows 操作系统中,对应的错误信息分别是:“路径的形式不合法”和“路径中具有非法字符”。2. 我使用的 Linux 操作系统中文件名的允许最大长度是 255 个字符,而在 Windows XP Professional SP2 和 Windows Server 2003 SP2 中分别是 251 和 248 个字符。3. 在 Linux 操作系统中,不能作为目录名使用的字符只有(‘\0’)这么一个。不能作为文件名使用的字符只有(‘\0’)和(‘/’)两个。而在基于 Windows 的桌面平台上,无效路径字符可能包括从 1 到 31 的 ASCII/Unicode 字符,,以及引号 (")、小于号 (<)、大于号 (>)、管道符号 (|)、退格 (\b)、空 (\0) 和制表符 (\t)。4. 从上面的表格中可以看到:“正在使用的文件”和“只读文件”也可以被 File.Delete 方法成功删除。下面使用 Linux 操作系统的“rm”命令的情况:ben@ibm7k:~/work$ls -l readwrite1.file-rw-r–r– 1 ben ben 23 2007-09-02 13:56 readwrite1.fileben@ibm7k:~/work$rm readwrite1.fileben@ibm7k:~/work$ls -l readwrite1.filels: readwrite1.file: No such file or directoryben@ibm7k:~/work$ben@ibm7k:~/work$ls -l readonly.file-r–r–r– 1 ben ben 1624 2007-09-02 13:57 readonly.fileben@ibm7k:~/work$rm readonly.filerm: removewrite-protectedregular file `readonly.file’?yben@ibm7k:~/work$ls -l readonly.filels: readonly.file: No such file or directoryben@ibm7k:~/work$ben@ibm7k:~/work$ls -l /etc/passwd-rw-r–r– 1rootroot 1168 2007-08-09 14:33 /etc/passwdben@ibm7k:~/work$rm /etc/passwdrm: removewrite-protectedregular file `/etc/passwd’?yrm: cannot remove `/etc/passwd’: Permission deniedben@ibm7k:~/work$ls -l /etc/passwd-rw-r–r– 1 root root 1168 2007-08-09 14:33 /etc/passwdben@ibm7k:~/work$可以看出,使用“rm”命令删除只读文件时会提问用户是否确实需要删除该文件。虽然 File.Delete 方法无法提问用户是否确实需要删除只读文件,但也不能就不声不响地把只读文件给删除了,而应该引发异常才对。总的说来,mono 还是非常成功的,Windows 操作系统很多 .NET 程序直接拷贝到 Linux 下用 mono 就可以直接运行,不用重新编译。虽然说她还有这样那样的小问题,但瑕不掩瑜,并且她还在不断进步当中。

版权声明:本文为博主原创文章,未经博主允许不得转载。

只要你扬帆,便会有八面来风。启程了,人的生命才真正开始。

Linux 操作系统下 File.Delete 的表现

相关文章:

你感兴趣的文章:

标签云: