简体   繁体   中英

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);

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM