创建目录后.NET Core控制台应用程序出现UnauthorizedAccessException

Ada*_*dam 2 .net c# filesystems console-application .net-core

使用 .NET Core 2.0 创建目录成功在文件系统中创建了一个目录,但我无法写入它。

var downloadPath = Path.Combine(DownloadPath, "Target");
var downloadDirectory = Directory.CreateDirectory(downloadPath); // Succeeds
DownloadMultipleFilesAsync(zoneFileUrls, downloadPath);          // Fails
Run Code Online (Sandbox Code Playgroud)

DownloadMultipleFilesAsync执行该方法时,我最终收到以下错误。FileAttributes.ReadOnly如果我从目录属性中删除也没关系,它仍然会出现此错误。

System.Net.WebException: An exception occurred during a WebClient request. ---> System.UnauthorizedAccessException: Access to the path 'D:\Projects\ETL\My.ETL\bin\Debug\netcoreapp2.0\Work\Target' is denied.
   at SafeFileHandle System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options)
   at new System.IO.FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options)
   at new System.IO.FileStream(string path, FileMode mode, FileAccess access)
   at void System.Net.WebClient.DownloadFileAsync(Uri address, string fileName, object userToken)
   --- End of inner exception stack trace ---
   at async Task My.ETL.Services.FileProviders.FileProvider.DownloadFileTaskAsync(string url, string downloadPath) in D:\Projects\ETL\My.ETL.Services\FileProviders\FileProvider.cs:line 80
Run Code Online (Sandbox Code Playgroud)

通过 Visual Studio 运行它。在文件资源管理器中,我看到我的名字是目录的所有者,所以我知道控制台应用程序正在以我的身份运行,所以不确定为什么它不允许我将文件下载到其中。

Ada*_*dam 6

我忘记将文件名附加到路径:-(