Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting files in use

Tags:

I created a simple program to delete temporary files in C# (for fun, not a major project) and am running into locked files (in use) issues. How do you normally either exclude those files? For reference I am receiving the error:

The process cannot access the file 'ExchangePerflog_8484fa31c65c7a31cfcccd43.dat' because it is being used by another process.

Code:

static void Main(string[] args)     {         string folderPath = string.Empty;         folderPath = System.Environment.GetEnvironmentVariable("temp");         deleteFilesInDirectory(folderPath);     }      public static void deleteFilesInDirectory(string folderPath)      {          try         {             var dir = new DirectoryInfo(folderPath);             dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;             dir.Delete(true);             MessageBox.Show(folderPath + " has been cleaned.");         }         catch (System.IO.IOException ex)         {             MessageBox.Show(ex.Message);              return;          }      }      
like image 423
Geekender Avatar asked May 08 '12 18:05

Geekender


People also ask

How do you delete a file that is not deleting?

One is simply using the delete option, and the other one is deleting files permanently. When you can't delete a file normally, you can delete undeletable files Windows 10 by selecting the target file or folder and then press Shift + Delete keys on the keyboard for a try.

Can't delete file because it is open in system?

If the file you want to delete is in an “exe” file of a program, try closing the program first, then attempt to delete the file again. You can also try restarting your PC to close down any running programs or closing the apps that might be using the program you want to delete.


2 Answers

Description

There is no way to delete a file that is currently in use by another process. But you can wait till the file is not locked.

Check in a while loop till the file is unlocked with this method

protected virtual bool IsFileLocked(FileInfo file) {     FileStream stream = null;      try     {         stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);     }     catch (IOException)     {         //the file is unavailable because it is:         //still being written to         //or being processed by another thread         //or does not exist (has already been processed)         return true;     }     finally     {         if (stream != null)             stream.Close();     }      //file is not locked     return false; } 

Sample

FileInfo file = new FileInfo("PathToTheFile"); while (IsFileLocked(file))     Thread.Sleep(1000); file.Delete(); 

Update

If you want to skip locked files you can do this.

// var dir = new DirectoryInfo(folderPath); foreach(var file in dir.GetFiles()) {     try     {         file.Delete();     }     catch (IOException)     {         //file is currently locked     } } 
like image 181
dknaack Avatar answered Oct 05 '22 11:10

dknaack


Try the following code. Just add two lines before file deletion:

GC.Collect(); GC.WaitForPendingFinalizers(); 
like image 22
Aravindhkumar Avatar answered Oct 05 '22 11:10

Aravindhkumar