Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting System.UnauthorizedAccessException on File.Create

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);
like image 203
Stefan Vukovic Avatar asked Dec 18 '25 08:12

Stefan Vukovic


1 Answers

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.

like image 154
Necstreeam Avatar answered Dec 20 '25 22:12

Necstreeam