UnauthorizedAccessException on newly created files

Jerry picture Jerry · Jan 5, 2010 · Viewed 11k times · Source

I have an application that is looking through some files for old data. In order to make sure we don't corrupt good projects, I'm copying the files to a temporary location. Some of the directories I'm checking are source-code directories, and they have .svn folders. We use Subversion to manage our code.

Once I've searched through all of the files, I want to delete the temp cache. Sounds easy, right?

For some reason, all of my .svn directories won't delete from the cache. They crash the app.

For reasons (too deep to go into here), I have to use the temp folder, so just "scan the original file" is out of the question for political reasons.

I can go into explorer and delete them. No problem. No warnings. Just deletes. But the code crashes with "Access to {file} is denied." I'm at my wits end with this one, so any help would be appreciated.

While I've simplified the function a LITTLE for sake of your sanity, the code REALLY is about this simple.

List<string> tmpCacheManifest = new List<string>();
string oldRootPath = "C:\\some\\known\\directory\\";
string tempPath = "C:\\temp\\cache\\";

foreach (string file in ListOfFilesToScan)
{
    string newFile = file.Replace(oldRootPath, tempPath);

    // This works just fine.
    File.Copy(file, newFile);

    tmpCacheManifest.add(newFile);
}

//    ... do some stuff to the cache to verify what I need.


// Okay.. I'm done.. Delete the cache.
foreach (string file in tmpCacheManifest)
{
   // CRASH!
   File.Delete(file);
}

* Update *: The exception is UnauthorizedAccessException. The text is "Access to the path 'C:\temp\cache\some-sub-dirs\.svn\entries' is denied."

It happens under XP, XP-Pro and Windows 7.

* Update 2 * None of my validation even ATTEMPTS to look at subversion files. I do need them, however. That's part of the political crap. I have to show that EVERY file was copied... wheter it was scanned or not.

And I realize what the usual suspects are for File.Delete. I realize what UnauthorizedAccessException means. I don't have access. That's a no-brainer. But I just copied the file. How can I NOT have access to the file?

* Update 3 * The answer was in the "read-only" flag. Here's the code I used to fix it:

    foreach (string file in ListOfFilesToScan)
{
    string newFile = file.Replace(oldRootPath, tempPath);

    // This works just fine.
    File.Copy(file, newFile);

    //// NEW CODE ////
    // Clear any "Read-Only" flags
    FileInfo fi3 = new FileInfo(fn);
    if ((fi3.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
    {
        fi3.Attributes = (FileAttributes)(Convert.ToInt32(fi3.Attributes) - Convert.ToInt32(FileAttributes.ReadOnly));
    }



    tmpCacheManifest.add(newFile);
}

//    ... do some stuff to the cache to verify what I need.

Answer

Martin B picture Martin B · Jan 5, 2010

As far as I recall, Subversion marks the files in its .svn subdirectories as read-only.

Try resetting the read-only attribute before deleting the file. I don't really know any C#, but a quick Google suggests this might do the trick:

File.SetAttributes(file, FileAttributes.Normal);