File.Delete 方法

删除指定的文件。如果指定的文件不存在,则不引发异常。

**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Shared Sub Delete ( _
    path As String _
)
用法
Dim path As String

File.Delete(path)
public static void Delete (
    string path
)
public:
static void Delete (
    String^ path
)
public static void Delete (
    String path
)
public static function Delete (
    path : String
)

参数

  • path
    要删除的文件的名称。

异常

异常类型 条件

ArgumentException

path 是一个零长度字符串,仅包含空白或者包含一个或多个由 InvalidPathChars 定义的无效字符。

ArgumentNullException

path 为 空引用(在 Visual Basic 中为 Nothing)。

DirectoryNotFoundException

指定的路径无效(例如,它位于未映射的驱动器上)。

IOException

指定的文件正在使用中。

NotSupportedException

path 的格式无效。

PathTooLongException

指定的路径、文件名或者两者都超出了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。

UnauthorizedAccessException

调用方没有所要求的权限。

- 或 -

path 是一个目录。

- 或 -

path 指定一个只读文件。

备注

允许 path 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory

有关使用此方法的示例,请参见“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

创建文本文件。

如何:向文件写入文本

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

向文件中追加文本。

如何:打开并追加到日志文件

File.AppendText

FileInfo.AppendText

复制文件。

File.Copy

FileInfo.CopyTo

重命名或移动文件。

File.Move

FileInfo.MoveTo

删除目录。

Directory.Delete

DirectoryInfo.Delete

读取二进制文件。

如何:对新建的数据文件进行读取和写入

写入二进制文件。

如何:对新建的数据文件进行读取和写入

创建目录。

CreateDirectory

Directory

查看目录中的文件。

Name

设置文件属性。

SetAttributes

Windows NT 4.0 平台说明: Delete 不删除为正常 I/O 打开的文件或已在内存中映射的文件。

示例

下面的示例从指定的路径删除一个文件。

Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"
        Try
            Dim sw As StreamWriter = File.CreateText(path)
            sw.Close()
            Dim path2 As String = path + "temp"

            ' Ensure that the target does not exist.
            File.Delete(path2)

            ' Copy the file.
            File.Copy(path, path2)
            Console.WriteLine("{0} was copied to {1}.", path, path2)

            ' Delete the newly created file.
            File.Delete(path2)
            Console.WriteLine("{0} was successfully deleted.", path2)

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class
using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        try 
        {
            using (StreamWriter sw = File.CreateText(path)) {}
            string path2 = path + "temp";

            // Ensure that the target does not exist.
            File.Delete(path2);

            // Copy the file.
            File.Copy(path, path2);
            Console.WriteLine("{0} was copied to {1}.", path, path2);

            // Delete the newly created file.
            File.Delete(path2);
            Console.WriteLine("{0} was successfully deleted.", path2);
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
using namespace System;
using namespace System::IO;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   try
   {
      StreamWriter^ sw = File::CreateText( path );
      if ( sw )
            delete (IDisposable^)sw;

      String^ path2 = String::Concat( path, "temp" );
      
      // Ensure that the target does not exist.
      File::Delete( path2 );
      
      // Copy the file.
      File::Copy( path, path2 );
      Console::WriteLine( "{0} was copied to {1}.", path, path2 );
      
      // Delete the newly created file.
      File::Delete( path2 );
      Console::WriteLine( "{0} was successfully deleted.", path2 );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }
}
import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        String path = "c:\\temp\\MyTest.txt";

        try {    
            StreamWriter sw = File.CreateText(path);
            try {
            }
            finally {
                sw.Dispose();
            }
            String path2 = path + "temp";

            // Ensure that the target does not exist.
            File.Delete(path2);

            // Copy the file.
            File.Copy(path, path2);
            Console.WriteLine("{0} was copied to {1}.", path, path2);

            // Delete the newly created file.
            File.Delete(path2);
            Console.WriteLine("{0} was successfully deleted.", path2);
        }
        catch (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    } //main
} //Test

.NET Framework 安全性

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

File 类
File 成员
System.IO 命名空间

其他资源

文件和流 I/O
如何:从文件读取文本
如何:向文件写入文本