0

I am temporarily extracting a .zip file to disk and I want to delete all those files later on. During testing this, I had extracted some TortoiseSVN metadata folders in the process. When my application tried to delete them later on, it failed with an UnauthorizedAccessException.
I assume that is due to the fact that TSVNCache monitors the file system and had a lock on that folder at the time I wanted to delete it.

How can I prevent something like that?

Edit: Here's some code to illustrate the issue a little better:

  public class Package : IDisposable {

    private bool _disposed;

    public string Filename { get; set; }

    public DirectoryInfo RootFolder { get; set; }

    public Package( string filename, DirectoryInfo rootFolder ) {
      Filename   = filename;
      RootFolder = rootFolder;
    }

    public static Package Expand( string packageFileName, DirectoryInfo targetDirectory ) {
      FileInfo packageFile = new FileInfo( packageFileName );

      string publicKey      = Settings.Default.PublicKey;
      byte[] publicKeyBytes = Convert.FromBase64String( publicKey );

      byte[] packageBytes          = File.ReadAllBytes( packageFile.FullName );
      byte[] decryptedPackageBytes = Blob.DecryptBlob( packageBytes, publicKeyBytes, false );

      // Write result
      string outputFolderName = targetDirectory.FullName;
      Directory.CreateDirectory( outputFolderName );

      ZipFile zipFile = ZipFile.Read( decryptedPackageBytes );
      zipFile.ExtractAll( outputFolderName );

      Package result = new Package( packageFile.FullName, new DirectoryInfo( outputFolderName ) );
      return result;
    }

    ~Package() {
      Dispose( false );
    }

    public void Dispose() {
      Dispose( true );
      GC.SuppressFinalize( this );
    }

    private void Dispose( bool disposing ) {
      if( _disposed ) {
        return;
      }
      if( disposing ) {
        RootFolder.Delete( true );
      }
      // Dispose unmanaged resources.
      _disposed = true;
    }
  }

I would use Package.Expand in a using block and access the extracted files there (currently I just loop through them and print their names to the console). When the block exits and Dispose is called, I get the exception regarding all-wcprops. Other, non-svn related files are deleted though.

Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138

4 Answers4

0

You you sure you disposed of your handle correctly and that the user the app was running as has the rights to delete it?

Paul
  • 35,689
  • 11
  • 93
  • 122
  • The use that created the file is the same that attempts to delete it. It's all done be the same application. It works just fine with other files as well. I just noticed this behavior with the SVN files. – Oliver Salzburg May 26 '11 at 14:53
0

I have the same problem with tortiose-Git.

When i have a locking problem i use taskmanager to kill the TGitCache-Process (or in your case the TSVNCache-process)

after a short pause the TGitCache starts again and i can continue tu use tortoise.

You can also think of excluding certain directories from tortois-svn supervision

k3b
  • 14,517
  • 7
  • 53
  • 85
  • Well, this doesn't really pose a problem on my development machine. I am just worried about this behavior on a clients machine. I really want to be able to delete those files (and not have other applications touch them). – Oliver Salzburg May 26 '11 at 15:20
  • the client machine will not have tortoise installed:-) you can prevent lockeing by other by exclusively locking the file yourself after creation or by not allowing other processes to read the file via acls – k3b May 26 '11 at 15:29
  • My question wasn't specifically regarding SVN files. That was just an example (and how I noticed it could be a general issue). I'll see what I can dig up regarding ACLs ;) – Oliver Salzburg May 26 '11 at 15:35
0

Even though I'm still interested in an answer to my question, my problem had nothing to do with file locks. UnauthorizedAccessException in this case simply indicates a read-only file.

How to get around that is discussed here: How do I delete a directory with read-only files in C#?

Community
  • 1
  • 1
Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138
0

You can use the CreateFile WIN32 API (via p/invoke) to open a directory handle with ShareMode set to 0, then no other processes will be able to obtain a handle to that directory.

hemp
  • 5,602
  • 29
  • 43
  • Is there no managed equivalent? `FileShare.None` seems to prevent even my own process from opening the directory/file (according to the documentation). – Oliver Salzburg May 28 '11 at 11:31