在我的上一篇随笔“对 File.Delete
方法的一点看法”中,在 Windows 操作系统上对 File.Delete 方法进行了测试。这次,在 Linux 操作系统上使用 mono 的最新版本
1.2.5 版对 File.Delete 方法进行测试。
下面是我的运行 Linux 操作系统的计算机的基本信息:
[email protected]:~/work$ cat /etc/issue.net
Ubuntu 6.10
[email protected]:~/work$ uname -rm
2.6.17-12-server i686
[email protected]:~/work$ mono --version
Mono JIT compiler version 1.2.5 (tarball)
Copyright (C) 2002-2007 Novell, Inc and Contributors. www.mono-project.com
TLS: __thread
GC: Included Boehm (with typed GC)
SIGSEGV: normal
Architecture: x86
Disabled: none
[email protected]:~/work$ gmcs --version
Mono C# compiler version 1.2.5.0
[email protected]:~/work$ mono clrver.exe
操作系统版本 : Unix 2.6.17.12
公共语言运行库版本: 2.0.50727.42
[email protected]:~/work$
我们用下面的一段程序来测试 File.Delete 在 Linux 操作系统下的表现:
using System;
using System.IO;
sealed class Test
{
static void Main()
{
try
{
Console.Write("请输入要删除的文件名: ");
string fileName = Console.ReadLine();
if (fileName == "null") fileName = null;
if (fileName == "zero") fileName = "\0";
File.Delete(fileName);
Console.WriteLine("File.Delete 成功");
}
catch (Exception ex)
{
Console.WriteLine("错误: " + ex.ToString());
}
}
}
运行结果如下:
文件名 |
直接调用 File.Delete 方法
[email protected]:~/work$ mono test.exe |
零长度字符串 |
请输入要删除的文件名:
错误: System.ArgumentException: path at System.IO.File.Delete (System.String path) [0x00000] at Test.Main (System.String[] args) [0x00000] |
非法字符 |
请输入要删除的文件名: zero
错误: System.ArgumentException: path at System.IO.File.Delete (System.String path) [0x00000] at Test.Main (System.String[] args) [0x00000] |
空引用 |
请输入要删除的文件名: null
错误: System.ArgumentNullException: Argument cannot be null. Parameter name: path at 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: none at 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 directory at 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.exe
File.Delete 成功 |
只读文件 |
请输入要删除的文件名: readonly.file
File.Delete 成功 |
不存在的文件 |
请输入要删除的文件名: none.file
File.Delete 成功 |
正常的文件 |
请输入要删除的文件名: readwrite1.file
File.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”命令的情况:
[email protected]:~/work$ ls -l readwrite1.file
-rw-r--r-- 1 ben ben 23 2007-09-02 13:56 readwrite1.file
[email protected]:~/work$ rm readwrite1.file
[email protected]:~/work$ ls -l readwrite1.file
ls: readwrite1.file: No such file or directory
[email protected]:~/work$
[email protected]:~/work$ ls -l readonly.file
-r--r--r-- 1 ben ben 1624 2007-09-02 13:57 readonly.file
[email protected]:~/work$ rm readonly.file
rm: remove write-protected regular file `readonly.file‘? y
[email protected]:~/work$ ls -l readonly.file
ls: readonly.file: No such file or directory
[email protected]:~/work$
[email protected]:~/work$ ls -l /etc/passwd
-rw-r--r-- 1 root root 1168 2007-08-09 14:33 /etc/passwd
[email protected]:~/work$ rm /etc/passwd
rm: remove write-protected regular file `/etc/passwd‘? y
rm: cannot remove `/etc/passwd‘: Permission denied
[email protected]:~/work$ ls -l /etc/passwd
-rw-r--r-- 1 root root 1168 2007-08-09 14:33 /etc/passwd
[email protected]:~/work$
可以看出,使用“rm”命令删除只读文件时会提问用户是否确实需要删除该文件。虽然 File.Delete 方法无法提问用户是否确实需要删除只读文件,但也不能就不声不响地把只读文件给删除了,而应该引发异常才对。
总的说来,mono 还是非常成功的,Windows 操作系统很多 .NET 程序直接拷贝到 Linux 下用 mono 就可以直接运行,不用重新编译。虽然说她还有这样那样的小问题,但瑕不掩瑜,并且她还在不断进步当中。
版权声明:本文为博主http://www.zuiniusn.com原创文章,未经博主允许不得转载。