title: ASP.NET Core System.UnauthorizedAccessException: Access to the path 'C:\..." is denied
body: <p>I am new to ASP.NET Core so I am playing around with making an API. I am trying to upload a picture through my Web API. The picture is to be saved onto the web server. I am running the API on a Windows Server 2016 Standard Edition VPS Machine. I have IIS 10 installed on the machine. The error I get back (when remote debugging): </p>
  
  <blockquote>
    <p>{System.UnauthorizedAccessException: Access to the path 'C:\HomeNET\Houses\13' is denied.
       at System.IO.Win32FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
       at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
       at System.IO.FileStream..ctor(String path, FileMode mode)
       at HomeNetAPI.Controllers.HouseController.d__6.MoveNext() in C:\Users\okuhl\Documents\HomeNet\Web API\HomeNetAPI\src\HomeNetAPI\Controllers\HouseController.cs:line 83}</p>
  </blockquote>
  
  <p>This is weird because directories are created, should they not exist. Here is some of the code, where the fail happens: </p>
  
  <pre><code>if (resultHouse != null)
  {
      if (!Directory.Exists($"C:/HomeNET/Houses/{resultHouse.HouseID}"))
      {
          Directory.CreateDirectory($"C:/HomeNET/Houses/{resultHouse.HouseID}");
      }
  
      String fileName = new FileInfo(imageFile.FileName).Name;
      String fileExtension = new FileInfo(imageFile.FileName).Extension;
      using (var fileStream = new FileStream($"C:/HomeNET/Houses/{resultHouse.HouseID}", 
          FileMode.Create)) //Bombs out after this line
      {
          var result = imageFile.CopyToAsync(fileStream);
          if (result.IsCompleted)
          {
              newHouse.HouseImage = Path.Combine($"C:/HomeNET/Houses/{resultHouse.HouseID}", $".{fileExtension}");
              var updateResult = await Task.Run(() =&gt;
              {
                  return houseRepository.UpdateHouse(newHouse);
              });
              if (updateResult != null)
              {
                  response.DidError = false;
                  response.Message = $"House {newHouse.Name} has been created successfully! Invite friends ad family to your house!";
                  response.Model = newHouse;
                  return Ok(response);
              }
              else
              {
                  response.DidError = true;
                  response.Message = "Something went wrong with creating the house. Please try again";
                  response.Model = newHouse;
                  return BadRequest(response);
              }
          }
          else
          {
              response.DidError = true;
              response.Message = $"Something Went wrong with creating your house. House image could not be saved onto the system. \n {result.Exception.Message}";
              response.Model = newHouse;
              return BadRequest(response);
          }
      }
  }
  </code></pre>
  
  <p>I have tried making the folder available to everyone and that didn't work. I also tried giving my AppPool permission to modify the folder and that didn't work. </p>