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();
}
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();
}
doesn't work for me :(
ReplyDeleteCan u give me more information about your code implementation.
ReplyDeleteTry this code
ReplyDeleteusing System;
private void button1_Click(object sender, RoutedEventArgs e)
{
string FilePath = @"F:\1.txt";
if (File.Exists(FilePath))
{
GC.Collect();
GC.WaitForPendingFinalizers();
FileInfo f = new FileInfo(FilePath);
f.Delete();
}
}
Thanks, it works for me.
ReplyDeleteTanks for everything
ReplyDeleteThanks very much! This helped me tremendously!
ReplyDelete