3

I am using the SharpSvn library in an application. As part of my automated integration tests, I create a test repository, check out a working copy, perform some tests, and then delete both the repository and working copy folders.

However, a simple Directory.Delete(workingCopyPath, true); always yields an UnauthorizedAccessException with the message "Access to the path 'entries' is denied.". I can reproduce the error with this code:

     using (var svnClient = new SvnClient())
     {
        svnClient.CheckOut(
           new SvnUriTarget(new Uri(repositoryPath)), workingCopyPath);
     }
     Directory.Delete(workingCopyPath, true);

This error still occurs if I

  • try to delete a working copy created by a previous run of the integration tests
  • Thread.Sleep a few seconds before trying to delete

If I use explorer to manually delete the temporary working copy, I don't get any error.

What is going wrong here? What is the proper way to programmatically delete a subversion working copy?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
  • Probably one of your integration automated test processes has its current directory set to a directory within the working copy, or is still using a file in the copy. –  Dec 30 '09 at 20:16
  • No, that's why I mention that I also can't delete working copies created by a *previous* run. The processes of the previous run is no longer running, so I'd say it cannot keep any resources locked. – Wim Coenen Dec 30 '09 at 20:19

1 Answers1

9

Turns out Directory.Delete refuses to delete read-only files.

I now use this method to delete directories:

private void DeleteDirectory(string path)
{
   var directory = new DirectoryInfo(path);
   DisableReadOnly(directory);
   directory.Delete(true);
}

private void DisableReadOnly(DirectoryInfo directory)
{
   foreach (var file in directory.GetFiles())
   {
      if (file.IsReadOnly)
         file.IsReadOnly = false;
   }
   foreach (var subdirectory in directory.GetDirectories())
   {
      DisableReadOnly(subdirectory);
   }
}
Sander Rijken
  • 21,376
  • 3
  • 61
  • 85
Wim Coenen
  • 66,094
  • 13
  • 157
  • 251