Wim Coenen
Wim Coenen

Reputation: 66733

Cannot programmatically delete SVN working copy

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

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?

Upvotes: 3

Views: 2101

Answers (1)

Wim Coenen
Wim Coenen

Reputation: 66733

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);
   }
}

Upvotes: 9

Related Questions