C# .Net核心2“;DirectoryNotFoundException:找不到路径的一部分;

C# .Net核心2“;DirectoryNotFoundException:找不到路径的一部分;,c#,iis,asp.net-core,.net-core,C#,Iis,Asp.net Core,.net Core,我正在尝试编写一个web应用程序,允许用户将文件上载到服务器,然后将其保存在网络共享(即X:\TargetFolder)上。首先,代码如下: using (var fileStream = new FileStream(targetFile, FileMode.Create, FileAccess.Write, FileShare.None)) { var inputStream = file.OpenReadStream(); await inputStream.CopyToA

我正在尝试编写一个web应用程序,允许用户将文件上载到服务器,然后将其保存在网络共享(即X:\TargetFolder)上。首先,代码如下:

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"
当此代码在Kestrel或IIS Express中运行时,它会按预期工作,但当站点在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
2017-08-30 14:51:05.742-04:00[错误]执行请求时发生未经处理的异常
System.IO.DirectoryNotFoundException:找不到路径“X:\Intranet\IN\PartesTrabajo\A1\U 7706\U所有Web服务器批处理1.xlsx”的一部分。
位于System.IO.FileStream.OpenHandle(文件模式、文件共享、文件选项)
位于System.IO.FileStream..ctor(字符串路径、文件模式、文件访问访问、文件共享、Int32 bufferSize、文件选项)
位于System.IO.FileStream..ctor(字符串路径、文件模式、文件访问访问、文件共享)
位于C:\Users\kelly\Documents\Site Recovery\Recoveridated Sites\Barcelona\Instrumentacion\Helpers\FileHelpers.cs中的Instrumentacion.Helpers.Uuu 4.MoveNext()处
这显然是一个与IIS相关的权限问题,但我无法找出问题所在

应用程序池my.Net核心应用程序正在(名为.NetCore)中运行,配置为作为ApplicationPoolIdentity运行。我已将IIS AppPool.NetCore用户的显式完全控制权限添加到此X:驱动器共享。我尝试将应用程序池用户切换到网络服务(并为其设置显式权限),但出现了相同的错误。我已将此共享指向本地文件夹,但仍不断收到此错误,但是如果我将保存指向实际文件夹(即C:\Users…\TargetFolder),则写入操作将按预期进行


我不知道还能去哪里看

它在Kestrel和IIS Express中工作,因为应用程序在用户会话的上下文中运行

在用户会话中映射的网络驱动器只能由该用户访问,而不能由运行IIS的任何服务帐户访问


如果改用UNC路径,您将看到这一点。

尝试使用UNC路径而不是X:\,因为映射的驱动器X:仅在您以用户身份交互登录时才存在。我知道我以前尝试过UNC共享,但也没有成功就放弃了。。我一定是打错了字或是类似的蠢事。这次走北卡罗来纳州大学的路线奏效了。我应该知道这一点,不知道为什么它以前没有注册。哼!