There was an error in this gadget

Search This Blog

Friday, February 11, 2011

Delete file which is already in use using C#.net

Sometime scenario arises where we need to delete a file using C#.
But sometime an error is thrown like "File is already in use. Not able to delete"

Here is the solution to delete that file

if(File.Exists(FullFileName))
{
       GC.Collect();
       GC.WaitForPendingFinalizers();
       FileInfo f = new FileInfo(FullFileName);
       f.Delete();
}