Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnauthorizedAccessException on newly created files

Tags:

c#

file-access

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.

like image 609
Jerry Avatar asked Jan 05 '10 16:01

Jerry


3 Answers

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);
like image 133
Martin B Avatar answered Oct 14 '22 05:10

Martin B


The only problem I see would be in this part:

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

If you do open the file and forget to close it, you still have exclusive access to it, and thus can't delete it later on.

like image 36
Femaref Avatar answered Oct 14 '22 07:10

Femaref


Sounds like you don't have access to delete the file...

system.io.file.delete

The above link says you get UnauthorizedAccessException when:

The caller does not have the required permission.

-or-

path is a directory.

-or-

path specified a read-only file.

It's one of those.

like image 27
Matt Ellen Avatar answered Oct 14 '22 06:10

Matt Ellen