【问题标题】:.Net Core 2 "DirectoryNotFoundException: Could not find a part of the path".Net Core 2“DirectoryNotFoundException:找不到路径的一部分”
【发布时间】:2017-08-30 19:13:56
【问题描述】:

我正在尝试编写一个网络应用程序,允许用户将文件上传到服务器,然后将其保存在网络共享(即 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

这显然是与 IIS 相关的权限问题,但我不知道问题出在哪里。

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

我不知道还能去哪里看!

【问题讨论】:

  • 尝试使用 UNC 路径而不是 X:\,因为映射的驱动器 X: 仅在您以用户交互方式登录时存在

标签: c# iis asp.net-core .net-core


【解决方案1】:

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

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

如果您改用 UNC 路径,您会发现这是可行的。

【讨论】:

  • 好悲痛。我知道我以前尝试过 UNC 共享,但是当它也不起作用时就放弃了那条路线。我一定只是输入错误或类似的愚蠢。这次走 UNC 路线奏效了。我应该知道这一点,不知道为什么它之前没有注册。呵呵!
猜你喜欢
  • 1970-01-01
  • 2023-03-29
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 2013-01-13
  • 2017-05-03
  • 2021-04-20
相关资源
最近更新 更多