tags:

views:

158

answers:

1

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 programatically delete a subversion working copy?

+3  A: 

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);
   }
}
Wim Coenen
That way should work pretty well yeah. You should be able to accept your own answer by the way.
Sander Rijken