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

Venkat K replied to balaji mogadali on 03-May-12 05:38 AM
AFAIK there is now way to delete a file which is currently in use. The only way we can delete it is if we can identify the process and kill it which is used by this file.  OR You definitely close the file before you delete it.

Thanks
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,