10 out of 20 rated this helpful - Rate this topic

How to: Delete a File in Visual Basic 

The DeleteFile method of the My.Computer.FileSystem object allows you to delete a file. Among the options it offers are: whether to send the deleted file to the Recycle Bin, whether to ask the user to confirm that the file should be deleted, and what to do when the user cancels the operation.

To delete a text file

  • Use the DeleteFile method to delete the file. The following code demonstrates how to delete the file named test.txt.

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

To delete a text file and ask the user to confirm that the file should be deleted

  • Use the DeleteFile method to delete the file, setting showUI to AllDialogs. The following code demonstrates how to delete the file named test.txt and allow the user to confirm that the file should be deleted.

    My.Computer.FileSystem.DeleteFile("C:\test.txt", _
            FileIO.UIOption.AllDialogs, FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.DoNothing)
    
    

To delete a text file and send it to the Recycle Bin

  • Use the DeleteFile method to delete the file, specifying SendToRecycleBin for the recycle parameter. The following code demonstrates how to delete the file named test.txt and send it to the Recycle Bin.

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

Robust Programming

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, 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 folder 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 exists in which the user does not have sufficient permissions (SecurityException).

  • The user cancelled the operation and onUserCancel is set to Microsoft.VisualBasic.FileIO.UICancelOption.ThrowException (OperationCanceledException).

See Also

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
reply
heh...dude when you try to delete that photofilename this function is not the problem

somewhere in your source you have not disposed, flushed cor closed that file
for instance if your using a filestream before this function write "filestream.close"
File Delete Issue
I am having trouble using the delete funciton in Visual Basic.

Here is my code:

My.Computer.FileSystem.DeleteFile(PhotoFileName, _
FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)

The files are selected from a listbox and a preview is shown in a picture preview control.

The delete function works sometimes, but for some reason, it shows that the file I am trying to delete is 'In use by another application' - even though this is not the case.

I had thought that the problem was that it was being used by the Photo preview control, so cleared it as follows:

If Not (PhotoRenamerMain.PhotoPreview.Image Is Nothing) Then
PhotoRenamerMain.PhotoPreview.Image.Dispose()
PhotoRenamerMain.PhotoPreview.Image = Nothing
End If

Can someone help? Is ther an obvious error here? Is it possible to programmitically close all applications which may have my target file open?

I am a bit of a newbie to programming...Your advice is appreciated.
Why not using a more 2.0 way
string[] filesToDelete = Directory.GetFiles(Path, Pattern);
Array.ForEach<string>(filesToDelete, File.Delete);
  • 6/17/2006
  • Ran
How to delete some files from a directory

If you want to use wildcards to delete a file, you can create a local array of files and delete each one:

        For Each tmpfile As String In IO.Directory.GetFiles(cDataPath, "t*.mht")
            My.Computer.FileSystem.DeleteFile(tmpfile)
        Next

Advertisement