Search code examples
visual-studiovisual-studio-2019iis-express

persistent access denied message when debugging project using IIS Express and Visual Studio


I'm debugging my NetCore 3.1 project in Visual Studio 2019. I am doing this locally using the built-in IIS Express on Windows 10.

I am trying to save files to my D: drive.

I'm getting the error below when I try to copy a file to the local file system when running my Visual Studio project in debug mode using IIS Express:

{"Access to the path 'D:\\biologyMedia\\eb4cf4c2-6434-4cfe-9fa8-0033bc9b1a08' is denied."}

The error happens in the method below in the try block:

public async Task<bool> CopyFile(IFormFile examFile, Guid diseaseId)
{
    string path = @"D:\biologyMedia\" + diseaseId + @"\";

    if (!Directory.Exists(path))
    {
        DirectoryInfo di = Directory.CreateDirectory(path);
    }

    try
    {
        using (var fileStream = new FileStream(path, FileMode.Create))
        {
            await examFile.CopyToAsync(fileStream);
        }

    } catch (Exception e)
    {
        var error = e.InnerException;
        return false;
    }

    return true;
}

Based on previous answers, I've tried the following:

  • Set Windows Authentication to enabled in the project properties
  • Started Visual Studio as Administrator
  • Given complete control of the D: drive to LOCAL SERVICE, NETWORK SERVICE, IUSR, IIS_USERS, and NETWORK, and my own Windows account(which is a member of Administrator).

I'm not sure what else I can try.

Anyone ever fix this before?

Thanks!


Solution

  • Skye, sorry, but could it be that your path in

    using (var fileStream = new FileStream(path, FileMode.Create))
            {
                await examFile.CopyToAsync(fileStream);
            }
    

    is actually the folder @"D:\biologyMedia\" + diseaseId + @"\"; and therefore not a filename?

    So you are trying to copy a file ONTO a folder not INTO it?