Share via


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:

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

How to: Delete a File in Visual Basic

To delete all files in a directory

How to: Delete All Files in a Directory in Visual Basic

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

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.

For more information, see Code Access Security and Requesting Permissions.

See Also

Tasks

How to: Delete a Directory in Visual Basic

Reference

My.Computer.FileSystem Object

UIOption Enumeration

RecycleOption Enumeration

UICancelOption Enumeration

FileSystem.DeleteFile

My.Computer.FileSystem Object