3

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);
Stefan Vukovic
  • 990
  • 6
  • 12
  • 1
    because you dont have write access to that path seems to be the obvious explanation – pm100 Jun 10 '22 at 22:04
  • Does this answer your question? [Why is access to the path denied?](https://stackoverflow.com/questions/8821410/why-is-access-to-the-path-denied) – Merna Mustafa Jun 10 '22 at 22:16
  • Check the security of the specific path you have declare, Make sure that you have authorize to access the specified path. Goodluck and happy coding :) – Francis Decena Jun 11 '22 at 08:58
  • Your answers seems logical, however, the specified folder is created by the application itself. My guess is that it has authorization to access the Uploads folder because of that. – Stefan Vukovic Jul 01 '22 at 15:06

2 Answers2

1

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.

A. Yassin
  • 75
  • 1
  • 2
  • 10
Necstreeam
  • 23
  • 3
1

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

Stefan Vukovic
  • 990
  • 6
  • 12