FileInfo.Delete 方法
永久删除文件。
**命名空间:**System.IO
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Overrides Sub Delete
用法
Dim instance As FileInfo
instance.Delete
public override void Delete ()
public:
virtual void Delete () override
public void Delete ()
public override function Delete ()
异常
异常类型 | 条件 |
---|---|
目标文件已打开或内存映射到运行 Microsoft Windows NT 的计算机上。 |
|
调用方没有所要求的权限。 |
|
路径是目录。 |
备注
如果文件不存在,则此方法不执行任何操作。
下表列出了其他典型或相关的 I/O 任务的示例。
若要执行此操作... |
请参见本主题中的示例... |
---|---|
创建文本文件。 |
|
写入文本文件。 |
|
读取文本文件。 |
|
向文件中追加文本。 |
|
复制文件。 |
|
重命名或移动文件。 |
|
删除目录。 |
|
读取二进制文件。 |
|
写入二进制文件。 |
|
创建目录。 |
|
查看目录中的文件。 |
|
设置文件属性。 |
Windows NT 4.0 平台说明: Delete 不删除对正常 I/O 打开的文件或映射到内存的文件。
示例
下面的示例说明 Delete 方法。
Imports System
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim fi As FileInfo = New FileInfo(path)
Try
Dim sw As StreamWriter = fi.CreateText()
sw.Close()
Dim path2 As String = path + "temp"
Dim fi2 As FileInfo = New FileInfo(path2)
'Ensure that the target does not exist.
fi2.Delete()
'Copy the file.
fi.CopyTo(path2)
Console.WriteLine("{0} was copied to {1}.", path, path2)
'Delete the newly created file.
fi2.Delete()
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";
FileInfo fi1 = new FileInfo(path);
try
{
using (StreamWriter sw = fi1.CreateText()) {}
string path2 = path + "temp";
FileInfo fi2 = new FileInfo(path2);
//Ensure that the target does not exist.
fi2.Delete();
//Copy the file.
fi1.CopyTo(path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);
//Delete the newly created file.
fi2.Delete();
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";
FileInfo^ fi1 = gcnew FileInfo( path );
try
{
StreamWriter^ sw = fi1->CreateText();
if ( sw )
delete (IDisposable^)sw;
String^ path2 = String::Concat( path, "temp" );
FileInfo^ fi2 = gcnew FileInfo( path2 );
//Ensure that the target does not exist.
fi2->Delete();
//Copy the file.
fi1->CopyTo( path2 );
Console::WriteLine( "{0} was copied to {1}.", path, path2 );
//Delete the newly created file.
fi2->Delete();
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";
FileInfo fi1 = new FileInfo(path);
try {
StreamWriter sw = fi1.CreateText();
try {
}
finally {
sw.Dispose();
}
String path2 = path + "temp";
FileInfo fi2 = new FileInfo(path2);
//Ensure that the target does not exist.
fi2.Delete();
//Copy the file.
fi1.CopyTo(path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);
//Delete the newly created file.
fi2.Delete();
Console.WriteLine("{0} was successfully deleted.", path2);
}
catch (System.Exception e) {
Console.WriteLine("The process failed: {0}", e.ToString());
}
} //main
} //Test
下面的示例创建、关闭并删除文件。
Imports System
Imports System.IO
Public Class DeleteTest
Public Shared Sub Main()
' Create a reference to a file.
Dim fi As New FileInfo("temp.txt")
' Actually create the file.
Dim fs As FileStream = fi.Create()
' Modify the file as required, and then close the file.
fs.Close()
' Delete the file.
fi.Delete()
End Sub 'Main
End Class 'DeleteTest
using System;
using System.IO;
public class DeleteTest
{
public static void Main()
{
// Create a reference to a file.
FileInfo fi = new FileInfo("temp.txt");
// Actually create the file.
FileStream fs = fi.Create();
// Modify the file as required, and then close the file.
fs.Close();
// Delete the file.
fi.Delete();
}
}
using namespace System;
using namespace System::IO;
int main()
{
// Create a reference to a file.
FileInfo^ fi = gcnew FileInfo( "temp.txt" );
// Actually create the file.
FileStream^ fs = fi->Create();
// Modify the file as required, and then close the file.
fs->Close();
// Delete the file.
fi->Delete();
}
import System.*;
import System.IO.*;
public class DeleteTest
{
public static void main(String[] args)
{
// Create a reference to a file.
FileInfo fi = new FileInfo("temp.txt");
// Actually create the file.
FileStream fs = fi.Create();
// Modify the file as required, and then close the file.
fs.Close();
// Delete the file.
fi.Delete();
} //main
} //DeleteTest
import System;
import System.IO;
public class DeleteTest {
public static function Main() : void {
// Create a reference to a file.
var fi : FileInfo = new FileInfo("temp.txt");
// Actually create the file.
var fs : FileStream = fi.Create();
// Modify the file as required, and then close the file.
fs.Close();
// Delete the file.
fi.Delete();
}
}
DeleteTest.Main();
.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
请参见
参考
FileInfo 类
FileInfo 成员
System.IO 命名空间