Deletes the specified file. An exception is not thrown if the specified file does not exist.
[Visual Basic]
Public Shared Sub Delete( _
ByVal path As String _
)
[C#]
public static void Delete(
string path
);
[C++]
public: static void Delete(
String* path
);
[JScript]
public static function Delete(
path : String
);
Parameters
- path
- The name of the file to be deleted.
Exceptions
Remarks
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.
For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.
Windows NT 4.0 Platform Note: Delete does not delete a file that is open for normal I/O or a file that is memory mapped.
Example
[Visual Basic, C#, C++] The following example deletes a file from the specified path.
[Visual Basic]
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
[C#]
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());
}
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
int main() {
String* path = S"c:\\temp\\MyTest.txt";
try {
StreamWriter* sw = File::CreateText(path);
if (sw) __try_cast<IDisposable*>(sw)->Dispose();
String* path2 = String::Concat(path, S"temp");
// Ensure that the target does not exist.
File::Delete(path2);
// Copy the file.
File::Copy(path, path2);
Console::WriteLine(S"{0} was copied to {1}.", path, path2);
// Delete the newly created file.
File::Delete(path2);
Console::WriteLine(S"{0} was successfully deleted.", path2);
} catch (Exception* e) {
Console::WriteLine(S"The process failed: {0}", e);
}
}
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
.NET Framework Security:
See Also
File Class | File Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File