0

Below is the exception I'm getting from my web service while deleting a Directory

The process cannot access the file 'button.js' because it is being used by another process. 
3/22/2013 11:16:51 AM : Exception :The process cannot access the file 'button.js'   because it is being used by another process.

Inner Exception :

Stack Trace :

at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive) at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive) at System.IO.Directory.Delete(String path, Boolean recursive) at CRMobileAPI.v5_2.apibridge.downloadcompleted(String userid, String deviceid, String filepathurl) in E:\websites\test.cont.com\CRMobileAPI\v5_2\apibridge.asmx.cs:line 1213

Source :

mscorlib

Here is my code,

               if (Directory.Exists(_DirPath))
                {
                    try
                    {                            
                        DirectoryInfo _DirTemp = new DirectoryInfo(_DirPath);
                        _DirTemp.Delete(true);
                    }
                    catch (Exception Ex)
                    {
                        clsExHandler.Instance.Write(Ex);
                    }
                }

Note: this exception not only for 'button.js' it varies at time.

How do I get rid of this issue; my logger file stuck with this exception.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sankar M
  • 4,549
  • 12
  • 37
  • 55

1 Answers1

0

Quite probable reason can be, the file is in use. You have to make sure that the file is not being accessed by web server. However, there is no standard method for doing this.

I would suggest that you should handle this exception and retry to delete the file after a small delay. Web server generally accesses the file for a very short span and it should free it up very fast so if you retry with the same file, chances are there that you could successfully delete it.

Murtuza Kabul
  • 6,438
  • 6
  • 27
  • 34
  • You should first prepare a list of all files and directories you want to delete. Check out this - http://stackoverflow.com/questions/3710617/list-recursively-all-files-and-folders-under-the-giving-path. - Later you can iterate through each individual file and delete it. Take a second pass and delete those which were not deleted in the first pass. – Murtuza Kabul Mar 22 '13 at 11:50