Search code examples
svnsharpsvn

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

  • 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?


Solution

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