Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete a file that is in use by another process?

When I try to delete a file occurs the following exception:

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

My code looks like:

string[] files = Directory.GetFiles(@"C:\SEDocumentConverter\SOURCE");
foreach (string file in files)
{               
   File.Delete(file);
}

How can I solve this problem?

like image 729
Suresh Chaudhary Avatar asked Mar 08 '11 12:03

Suresh Chaudhary


People also ask

Can't delete a file because it's open in another program?

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.

Can you delete a file if it is running as a process?

There is no way to delete a file that's currently being used by another process. You have to close whatever program has that file open first, before you can delete it. If you don't already know which program that is, you can figure it out using Handle or Process Explorer.

How can I delete a file that won't delete?

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.


1 Answers

There is no way to delete a file that's currently being used by another process. You have to close whatever program has that file open first, before you can delete it.

If you don't already know which program that is, you can figure it out using Handle or Process Explorer.

like image 82
Cody Gray Avatar answered Sep 21 '22 09:09

Cody Gray