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
要删除的文件的名称。
异常
异常类型 | 条件 |
---|---|
path 是一个零长度字符串,仅包含空白或者包含一个或多个由 InvalidPathChars 定义的无效字符。 |
|
path 为 空引用(在 Visual Basic 中为 Nothing)。 |
|
指定的路径无效(例如,它位于未映射的驱动器上)。 |
|
指定的文件正在使用中。 |
|
path 的格式无效。 |
|
指定的路径、文件名或者两者都超出了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。 |
|
调用方没有所要求的权限。 - 或 - path 是一个目录。 - 或 - path 指定一个只读文件。 |
备注
允许 path 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory。
有关使用此方法的示例,请参见“示例”部分。下表列出了其他典型或相关的 I/O 任务的示例。
若要执行此操作... |
请参见本主题中的示例... |
---|---|
创建文本文件。 |
|
写入文本文件。 |
|
读取文本文件。 |
|
向文件中追加文本。 |
|
复制文件。 |
|
重命名或移动文件。 |
|
删除目录。 |
|
读取二进制文件。 |
|
写入二进制文件。 |
|
创建目录。 |
|
查看目录中的文件。 |
|
设置文件属性。 |
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 安全性
- FileIOPermission 用于删除指定的文件。关联的枚举:FileIOPermissionAccess.Write
平台
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