skip to Main Content

I’m getting System.UnauthorizedAccessException when trying to create a file using

using (var fileStream = System.IO.File.Create(filePath))

this is only trace i got

Exception thrown: 'System.UnauthorizedAccessException' in System.Private.CoreLib.dll

It’s ASP.NET Core 6 Web API run on docker

Any Ideas? Thanks 🙂

EDIT:
Here is mu complete malfunctioning code snippet:

var filePath = Path.Combine(_environment.WebRootPath, "Uploads");
if(!Directory.Exists(filePath))
    Directory.CreateDirectory(filePath);

using (var fileStream = System.IO.File.Create(filePath))
    await file.CopyToAsync(fileStream);

2

Answers


  1. Chosen as BEST ANSWER

    Just solved. I didn't put file name in the path.


  2. Are you trying to create this file into systems folders or similar? Did you try to change your filePath to, let’s say your images folder? You need to be sure that you’ve access rights to write into this folder, otherwise, you might have some issues with Windows permissions.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search