FileSystem.DeleteFile Method

Definition

Deletes a file.

Overloads

DeleteFile(String)

Deletes a file.

DeleteFile(String, UIOption, RecycleOption)

Deletes a file.

DeleteFile(String, UIOption, RecycleOption, UICancelOption)

Deletes a file.

DeleteFile(String)

Deletes a file.

public:
 static void DeleteFile(System::String ^ file);
public static void DeleteFile (string file);
static member DeleteFile : string -> unit
Public Shared Sub DeleteFile (file As String)

Parameters

file
String

Name and path of the file to be deleted.

Exceptions

The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; it has a trailing slash where a file must be specified; or it is a device path (starts with \\.\).

file is Nothing or an empty string.

The path exceeds the system-defined maximum length.

A file or directory name in the path contains a colon (:) or is in an invalid format.

The file is in use.

The user lacks necessary permissions to view the path.

The file does not exist.

The user does not have permission to delete the file or the file is read-only.

Examples

This example deletes the file Test.txt.

My.Computer.FileSystem.DeleteFile("C:\test.txt")

This example deletes the file Test.txt and allows the user to confirm that the file should be deleted.

My.Computer.FileSystem.DeleteFile(
  "C:\test.txt",
  FileIO.UIOption.AllDialogs,
  FileIO.RecycleOption.SendToRecycleBin,
  FileIO.UICancelOption.ThrowException)

This example deletes the file Test.txt and sends it to the Recycle Bin.

My.Computer.FileSystem.DeleteFile(
  "C:\test.txt",
  FileIO.UIOption.OnlyErrorDialogs,
  FileIO.RecycleOption.SendToRecycleBin,
  FileIO.UICancelOption.ThrowException)

Remarks

The following table lists an example of a task involving the My.Computer.FileSystem.DeleteFile method.

To See
To delete a file How to: Delete a File in Visual Basic

Security

FileIOPermission
Controls the ability to access files and folders. Associated enumeration: Unrestricted.

DeleteFile(String, UIOption, RecycleOption)

Deletes a file.

public:
 static void DeleteFile(System::String ^ file, Microsoft::VisualBasic::FileIO::UIOption showUI, Microsoft::VisualBasic::FileIO::RecycleOption recycle);
public static void DeleteFile (string file, Microsoft.VisualBasic.FileIO.UIOption showUI, Microsoft.VisualBasic.FileIO.RecycleOption recycle);
static member DeleteFile : string * Microsoft.VisualBasic.FileIO.UIOption * Microsoft.VisualBasic.FileIO.RecycleOption -> unit
Public Shared Sub DeleteFile (file As String, showUI As UIOption, recycle As RecycleOption)

Parameters

file
String

Name and path of the file to be deleted.

showUI
UIOption

Whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs.

recycle
RecycleOption

Whether or not the deleted file should be sent to the Recycle Bin. Default is RecycleOption.DeletePermanently.

Exceptions

The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; it has a trailing slash where a file must be specified; or it is a device path (starts with \\.\).

file is Nothing or an empty string.

The path exceeds the system-defined maximum length.

A file or directory name in the path contains a colon (:) or is in an invalid format.

The file is in use.

The user lacks necessary permissions to view the path.

The file does not exist.

The user does not have permission to delete the file or the file is read-only.

Examples

This example deletes the file Test.txt.

My.Computer.FileSystem.DeleteFile("C:\test.txt")

This example deletes the file Test.txt and allows the user to confirm that the file should be deleted.

My.Computer.FileSystem.DeleteFile(
  "C:\test.txt",
  FileIO.UIOption.AllDialogs,
  FileIO.RecycleOption.SendToRecycleBin,
  FileIO.UICancelOption.ThrowException)

This example deletes the file Test.txt and sends it to the Recycle Bin.

My.Computer.FileSystem.DeleteFile(
  "C:\test.txt",
  FileIO.UIOption.OnlyErrorDialogs,
  FileIO.RecycleOption.SendToRecycleBin,
  FileIO.UICancelOption.ThrowException)

Remarks

The showUI and recycle parameters are not supported in applications that are not user interactive, such as Windows Services.

The following table lists an example of a task involving the My.Computer.FileSystem.DeleteFile method.

To See
To delete a file How to: Delete a File in Visual Basic

Security

FileIOPermission
Controls the ability to access files and folders. Associated enumeration: Unrestricted.

UIPermission
Controls the permissions related to user interfaces and the clipboard. Associated enumeration: SafeSubWindows.

See also

DeleteFile(String, UIOption, RecycleOption, UICancelOption)

Deletes a file.

public:
 static void DeleteFile(System::String ^ file, Microsoft::VisualBasic::FileIO::UIOption showUI, Microsoft::VisualBasic::FileIO::RecycleOption recycle, Microsoft::VisualBasic::FileIO::UICancelOption onUserCancel);
public static void DeleteFile (string file, Microsoft.VisualBasic.FileIO.UIOption showUI, Microsoft.VisualBasic.FileIO.RecycleOption recycle, Microsoft.VisualBasic.FileIO.UICancelOption onUserCancel);
static member DeleteFile : string * Microsoft.VisualBasic.FileIO.UIOption * Microsoft.VisualBasic.FileIO.RecycleOption * Microsoft.VisualBasic.FileIO.UICancelOption -> unit
Public Shared Sub DeleteFile (file As String, showUI As UIOption, recycle As RecycleOption, onUserCancel As UICancelOption)

Parameters

file
String

Name and path of the file to be deleted.

showUI
UIOption

Whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs.

recycle
RecycleOption

Whether or not the deleted file should be sent to the Recycle Bin. Default is RecycleOption.DeletePermanently.

onUserCancel
UICancelOption

Specifies whether or not an exception is thrown when the user cancels the operation. Default is UICancelOption.ThrowException.

Exceptions

The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; it has a trailing slash where a file must be specified; or it is a device path (starts with \\.\).

file is Nothing or an empty string.

The path exceeds the system-defined maximum length.

A file or directory name in the path contains a colon (:) or is in an invalid format.

The file is in use.

The user lacks necessary permissions to view the path.

The file does not exist.

The user does not have permission to delete the file or the file is read-only.

The user cancelled the operation and onUserCancel is set to ThrowException.

Examples

This example deletes the file Test.txt.

My.Computer.FileSystem.DeleteFile("C:\test.txt")

This example deletes the file Test.txt and allows the user to confirm that the file should be deleted.

My.Computer.FileSystem.DeleteFile(
  "C:\test.txt",
  FileIO.UIOption.AllDialogs,
  FileIO.RecycleOption.SendToRecycleBin,
  FileIO.UICancelOption.ThrowException)

This example deletes the file Test.txt and sends it to the Recycle Bin.

My.Computer.FileSystem.DeleteFile(
  "C:\test.txt",
  FileIO.UIOption.OnlyErrorDialogs,
  FileIO.RecycleOption.SendToRecycleBin,
  FileIO.UICancelOption.ThrowException)

Remarks

The showUI, recycle, and onUserCancel parameters are not supported in applications that are not user interactive, such as Windows Services.

The following table lists an example of a task involving the My.Computer.FileSystem.DeleteFile method.

To See
To delete a file How to: Delete a File in Visual Basic

Security

FileIOPermission
Controls the ability to access files and folders. Associated enumeration: Unrestricted.

UIPermission
Controls the permissions related to user interfaces and the clipboard. Associated enumeration: SafeSubWindows.

See also

Applies to