title: UnauthorizedAccessException on newly created files
body: <p>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.</p>
  
  <p>Once I've searched through all of the files, I want to delete the temp cache. Sounds easy, right?</p>
  
  <p>For some reason, all of my .svn directories won't delete from the cache. They crash the app.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>While I've simplified the function a LITTLE for sake of your sanity, the code REALLY is about this simple.</p>
  
  <p><code></p>
  
  <pre><code>List&lt;string&gt; tmpCacheManifest = new List&lt;string&gt;();
  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);
  }
  </code></pre>
  
  <p></code></p>
  
  <p><strong>* Update *</strong>: The exception is UnauthorizedAccessException. The text is "Access to the path 'C:\temp\cache\some-sub-dirs\.svn\entries' is denied."</p>
  
  <p>It happens under XP, XP-Pro and Windows 7.</p>
  
  <p><strong>* Update 2 *</strong> 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. </p>
  
  <p>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 <em>NOT</em> have access to the file? </p>
  
  <p><strong>* Update 3 *</strong>
  The answer was in the "read-only" flag. Here's the code I used to fix it:</p>
  
  <p><CODE></p>
  
  <pre><code>    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 &amp; 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.
  </code></pre>
  
  <p></CODE></p>