title: Deleting file, but is access denied
body: <p>I have an mvc4 application with entity framework.</p>
  
  <p>I want to delete a file, but every time it says:</p>
  
  <p>An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code</p>
  
  <p>Additional information: Access to the path 'G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\' is denied.</p>
  
  <p>by this line: System.IO.File.Delete(path);</p>
  
  <p>This is the method:</p>
  
  <pre><code>public ActionResult DeleteFiles(int id)
          {           
                  //var fileName = Path.GetFileName(id.FileName);
  
                  var DirSeparator = Path.DirectorySeparatorChar;
                  var path = Server.MapPath("~\\Images" + DirSeparator);// + fileName.Replace('+', '_')));
                 var file = db.lolabikerPhotos.Find(id);               
                 System.IO.File.Delete(path);
                 db.SaveChanges();           
  
              return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
  
          }
  </code></pre>
  
  <p>I just run the app in visual studio, like this: <code>http://localhost:41787/Account/Edit?UserId=hallo</code></p>
  
  <p>I have done already the following things:</p>
  
  <p>Full access to the map, I added the Network Service to the map with full control. But nothing seems to work. I am using windows 7. And I run visual studio 2013 as administrator</p>
  
  <p>I also see this:</p>
  
  <pre><code>ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via &lt;identity impersonate="true"/&gt;, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.
  </code></pre>
  
  <p>Here you can see the access:</p>
  
  <p><img src="https://i.stack.imgur.com/Q0G14.png" alt="enter image description here"></p>
  
  <p>I try something like this:</p>
  
  <pre><code> &lt;system.web&gt;
  
      &lt;identity impersonate="true" userName="Administrator" password="windowsPassword"/&gt;
      &lt;httpRuntime requestValidationMode="2.0"  maxRequestLength="1048576" executionTimeout="3600" /&gt;
      &lt;compilation debug="true" targetFramework="4.5" /&gt;
  
      &lt;pages validateRequest="false" /&gt;
  
      &lt;!--&lt;httpRuntime targetFramework="4.5" /&gt;--&gt;
  
  
    &lt;/system.web&gt;
  </code></pre>
  
  <p>I've added this:</p>
  
  <pre><code>&lt;identity impersonate="true" userName="Administrator" password="windowsPassword"/&gt; 
  </code></pre>
  
  <p>OK, I can run the app, but still gives the error:</p>
  
  <pre><code>Access to the path 'G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\' is denied.
  
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
  
  Exception Details: System.UnauthorizedAccessException: Access to the path 'G:\Mijn Documents\My Web Sites\Lolabikes - Copy\C#\ContosoUniversity\Images\' is denied. 
  
  ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via &lt;identity impersonate="true"/&gt;, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. 
  
  To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.
  
  Source Error: 
  
  
  Line 489:                var path = Server.MapPath("~\\Images" + DirSeparator);// + fileName.Replace('+', '_')));
  Line 490:               var file = db.lolabikerPhotos.Find(id);               
  Line 491:               System.IO.File.Delete(path);
  Line 492:               db.SaveChanges();           
  Line 493:
  </code></pre>
  
  <p>full permission:</p>
  
  <p><img src="https://i.stack.imgur.com/FO1DC.png" alt="enter image description here"></p>
  
  <p>Advanced tab:
  <img src="https://i.stack.imgur.com/S0DlE.png" alt="enter image description here"></p>
  
  <p>Changed permissions tab:</p>
  
  <p><img src="https://i.stack.imgur.com/nR3MM.png" alt="enter image description here"></p>
  
  <p>I edited my action method like this:</p>
  
  <pre><code> public ActionResult DeleteFiles(int id)
          {           
                  var fileName = Path.GetFileName(@"\\Koala.jpg");
  
                  var DirSeparator = Path.DirectorySeparatorChar;
                  var path = Server.MapPath(@"\\Images" + DirSeparator + fileName.Replace('+', '_'));
                 var file = db.lolabikerPhotos.Find(id);
                 LolaBikePhoto lola = db.lolabikerPhotos.Find(id);
                 db.lolabikerPhotos.Remove(lola);
                 System.IO.File.Delete(path);
  
  
                 db.SaveChanges();           
  
              return Redirect(Url.Action("Edit", "Account") + "#tabs-3");
  
          }
  </code></pre>
  
  <p>And now it is working!</p>