码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2015-07-31 07:53:01      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:

在我的上一篇随笔“对 File.Delete 方法的一点看法”中,在 Windows 操作系统上对 File.Delete 方法进行了测试。这次,在 Linux 操作系统上使用 mono 的最新版本 1.2.5 版对 File.Delete 方法进行测试。
    下面是我的运行 Linux 操作系统的计算机的基本信息:

ben@ibm7k:~/work$ cat /etc/issue.net
Ubuntu 6.10
ben@ibm7k:~/work$ uname -rm
2.6.17-12-server i686
ben@ibm7k:~/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
ben@ibm7k:~/work$ gmcs --version
Mono C# compiler version 1.2.5.0
ben@ibm7k:~/work$ mono clrver.exe
操作系统版本      : Unix 2.6.17.12
公共语言运行库版本: 2.0.50727.42
ben@ibm7k:~/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 方法
ben@ibm7k:~/work$ mono test.exe
零长度字符串 请输入要删除的文件名:
错误: System.ArgumentExceptionpath
  at System.IO.File.Delete (System.String path) [0x00000]
  at Test.Main (System.String[] args) [0x00000]
非法字符 请输入要删除的文件名: zero
错误: System.ArgumentExceptionpath
  at System.IO.File.Delete (System.String path) [0x00000]
  at Test.Main (System.String[] args) [0x00000]
空引用 请输入要删除的文件名: null
错误: System.ArgumentNullExceptionArgument 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.DirectoryNotFoundExceptionDestination 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.PathTooLongExceptionPath 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.UnauthorizedAccessExceptionAccess 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”命令的情况:

ben@ibm7k:~/work$ ls -l readwrite1.file
-rw-r--r-- 1 ben ben 23 2007-09-02 13:56 readwrite1.file
ben@ibm7k:~/work$ rm readwrite1.file
ben@ibm7k:~/work$ ls -l readwrite1.file
ls: readwrite1.file: No such file or directory
ben@ibm7k:~/work$
ben@ibm7k:~/work$ ls -l readonly.file
-r--r--r-- 1 ben ben 1624 2007-09-02 13:57 readonly.file
ben@ibm7k:~/work$ rm readonly.file
rm: remove write-protected regular file `readonly.file‘? y
ben@ibm7k:~/work$ ls -l readonly.file
ls: readonly.file: No such file or directory
ben@ibm7k:~/work$
ben@ibm7k:~/work$ ls -l /etc/passwd
-rw-r--r-- 1 root root 1168 2007-08-09 14:33 /etc/passwd
ben@ibm7k:~/work$ rm /etc/passwd
rm: remove write-protected regular file `/etc/passwd‘? y
rm: cannot remove `/etc/passwd‘: Permission denied
ben@ibm7k:~/work$ ls -l /etc/passwd
-rw-r--r-- 1 root root 1168 2007-08-09 14:33 /etc/passwd
ben@ibm7k:~/work$

    可以看出,使用“rm”命令删除只读文件时会提问用户是否确实需要删除该文件。虽然 File.Delete 方法无法提问用户是否确实需要删除只读文件,但也不能就不声不响地把只读文件给删除了,而应该引发异常才对。

    总的说来,mono 还是非常成功的,Windows 操作系统很多 .NET 程序直接拷贝到 Linux 下用 mono 就可以直接运行,不用重新编译。虽然说她还有这样那样的小问题,但瑕不掩瑜,并且她还在不断进步当中。

版权声明:本文为博主http://www.zuiniusn.com原创文章,未经博主允许不得转载。

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

标签:

原文地址:http://blog.csdn.net/u013948191/article/details/47163959

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!