title: asp.net MVC3 create folder where application can write in
body: <p>I need to create a folder to use for storing files within it, in a .Net MVC3 application, but I think the problem is common to all ASP.Net platform.
  Problem is I can create the folder, but cannot write the files, because <code>System.UnauthorizedAccessException</code> occurred.
  I also tryed givin extra permission to the user currently running the web app, but nothing changes.
  This is my code so far:</p>
  
  <pre><code>if (!System.IO.Directory.Exists(fullPath))
  {
      System.IO.Directory.CreateDirectory(fullPath);                    
      var user = System.Security.Principal.WindowsIdentity.GetCurrent().User;
      var userName = user.Translate(typeof(System.Security.Principal.NTAccount));
      var dirInfo = new System.IO.DirectoryInfo(fullPath);
      var sec = dirInfo.GetAccessControl();                    
      sec.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(userName,
                      System.Security.AccessControl.FileSystemRights.Modify,
                      System.Security.AccessControl.AccessControlType.Allow)
                      );
      dirInfo.SetAccessControl(sec);                    
      System.IO.Directory.CreateDirectory(fullPath);                    
  }
  string fullPathFileName = System.IO.Path.Combine(fullPath, fileName);                
  System.IO.File.WriteAllBytes(fullPath, viaggio.Depliant.RawFile);
  </code></pre>
  
  <p>Too bad, last line of code always throw <code>System.UnauthorizedAccessException</code>.
  I'm not impersonating user in my app, everything run under a predefined user.
  What should I do to create a folder and assure that the application can also create files within it?</p>
  
  <p><strong>Edited:</strong></p>
  
  <p>I also tryed to save the files in the App_Data special folder, but I still got the <code>System.UnauthorizedAccessException</code> error. Somebody can tell me why is that happening?</p>