My.Computer.FileSystem.DeleteFile Method
Deletes a file.
' Usage
My.Computer.FileSystem.DeleteFile(file)
My.Computer.FileSystem.DeleteFile(file ,showUI ,recycle)
My.Computer.FileSystem.DeleteFile(file ,showUI ,recycle ,onUserCancel)
' Declaration
Public Sub DeleteFile( _
ByVal file As String _
)
' -or-
Public Sub DeleteFile( _
ByVal file As String, _
ByVal showUI As UIOption, _
ByVal recycle As RecycleOption _
)
' -or-
Public Sub DeleteFile( _
ByVal file As String, _
ByVal showUI As UIOption, _
ByVal recycle As RecycleOption, _
ByVal onUserCancel As UICancelOption _
)
Parameters
file
String. Name and path of the file to be deleted. Required.showUI
UIOption. Whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs. Required.recycle
RecycleOption. Whether or not the deleted file should be sent to the Recycle Bin. Default is RecycleOption.DeletePermanently. Required.onUserCancel
UICancelOption. Specifies whether or not an exception is thrown when the user cancels the operation. Default is UICancelOption.ThrowException. Required.
Exceptions
The following conditions may cause an exception:
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 \\.\) (ArgumentException).
The path is not valid because it is Nothing (ArgumentNullException).
The path exceeds the system-defined maximum length (PathTooLongException).
A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
The file is in use (IOException).
The user lacks necessary permissions to view the path (SecurityException).
The file does not exist (FileNotFoundException).
The user does not have permission to delete the file or the file is read-only (UnauthorizedAccessException).
A partial-trust situation in which the user does not have sufficient permissions (SecurityException).
The user cancelled the operation and onUserCancel is set to UICancelOption.ThrowException (OperationCanceledException).
Remarks
The showUI, recycle, and onUserCancel parameters are not supported in applications that are not user interactive, such as Windows Services.
Tasks
The following table lists examples of tasks involving the My.Computer.FileSystem.DeleteFile method.
To |
See |
---|---|
To delete a file |
|
To delete all files in a directory |
Example
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)
Requirements
Namespace:Microsoft.VisualBasic.MyServices
Class:FileSystemProxy (provides access to FileSystem)
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
Availability by Project Type
Project type |
Available |
---|---|
Windows Application |
Yes |
Class Library |
Yes |
Console Application |
Yes |
Windows Control Library |
Yes |
Web Control Library |
Yes |
Windows Service |
Yes |
Web Site |
Yes |
Permissions
The following permissions may be necessary:
Permission |
Description |
---|---|
Controls the ability to access files and folders. Associated enumeration: Unrestricted. |
|
Controls the permissions related to user interfaces and the clipboard. Associated enumeration: SafeSubWindows. |
For more information, see Code Access Security and Requesting Permissions.
See Also
Tasks
How to: Delete a Directory in Visual Basic