我正在嘗試編寫一個Web應用程序,允許用戶將文件上載到服務器,然後將該文件保存在網絡共享中(即X:\ TargetFolder)。首先這裏是代碼:.Net Core 2「DirectoryNotFoundException:找不到路徑的一部分」
using (var fileStream = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.None))
{
var inputStream = file.OpenReadStream();
await inputStream.CopyToAsync(fileStream, DefaultBufferSize, cancellationToken);
}
中的TargetFile變量的內容是:
"X:\\Intranet\\IN\\PartesTrabajo"
當這個代碼在紅隼或IIS快速運行時,它正常工作,但是當該網站是在IIS 7.5上運行,我得到以下例外:
2017-08-30 14:51:05.742 -04:00 [Error] An unhandled exception has occurred while executing the request
System.IO.DirectoryNotFoundException: Could not find a part of the path 'X:\Intranet\IN\PartesTrabajo\A1_7706_Copy of All Web Servers Batch 1.xlsx'.
at System.IO.FileStream.OpenHandle(FileMode mode, FileShare share, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at Instrumentacion.Helpers.UtilityLibrary.<WriteFileInput>d__4.MoveNext() in C:\Users\kelly\Documents\Site Remediation\Remediated Sites\Barcelona\Instrumentacion\Helpers\FileHelpers.cs:line 42
這顯然是IIS相關的權限問題,但我無法弄清楚問題出在哪裏。
我的.Net Core應用程序運行於(名爲.NetCore)中的應用程序池被配置爲以ApplicationPoolIdentity身份運行。我已經爲IIS AppPool.NetCore用戶添加了明確的完全控制權限,以便將此X:驅動器共享。我嘗試將App Pool用戶切換到Network Service(併爲其設置顯式權限),但得到了同樣的錯誤。我已經將這個共享指向了一個本地文件夾,但是仍然得到這個錯誤,但是如果我將這個保存點指向實際文件夾(即C:\ Users ... \ TargetFolder),那麼這些寫入按預期工作。
我不確定在哪裏看!
嘗試使用UNC路徑而不是X:\作爲映射驅動器X:僅在以交互方式以用戶身份登錄時才存在 – sly