ASP.NET - How to delete a file which is already in use using c#
Asked By balaji mogadali on 03-May-12 05:31 AM
hi frds
how to delete a file which is already in use using c#
pls help
S K replied to balaji mogadali on 03-May-12 05:40 AM
Ans is NO if you want to delete used file,Actually once one file in use it means it locked by another process so file can't be deleted until unless the process will be done and close.
So if that file is using within same application than you first stop that process and clear the object by GC call after that you can be able to delete the same file.
hope this help your
kalpana aparnathi replied to balaji mogadali on 03-May-12 05:40 AM
hi,
Try below code :
if(File.Exists(yourfilepath))
{
GC.Collect();
GC.WaitForPendingFinalizers();
FileInfo f = new FileInfo(DeleteUploadImge);
f.Delete();
}
Regards,