C# Directory.Exists 不适用于网络路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10196811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:50:51  来源:igfitidea点击:

Directory.Exists not working for a network path

c#asp.netdirectory

提问by Amaranth

I have a line of code checking if a directory exists and then getting the list of files in it.

我有一行代码检查目录是否存在,然后获取其中的文件列表。

System.IO.Directory.Exists(@"\Server\Folder\");

I works when I test it (run from visual studio), but when I deploy the web site, it always returns false.

我在测试它时工作(从 Visual Studio 运行),但是当我部署网站时,它总是返回 false。

I do the same verification for another folder, on another server (let's say Server2) and it works fine.

我在另一台服务器(假设 Server2)上对另一个文件夹进行了相同的验证,并且工作正常。

I then thought it was an access issue, but the shared folder and network have all access to everyone... Is there another reason why it would not work?

然后我认为这是一个访问问题,但是共享文件夹和网络对每个人都可以访问......还有其他原因导致它不起作用吗?

采纳答案by Shiraz Bhaiji

When you run the code in Visual Studio it runs under the the rights of your user.

当您在 Visual Studio 中运行代码时,它会在您的用户权限下运行。

When you run the code in IIS it runs in the identity of the Application Pool which by default is the built in user "Network Service" this is a local user account which does not have access outside the local machine.

当您在 IIS 中运行代码时,它以应用程序池的身份运行,默认情况下,该应用程序池是内置用户“网络服务”,这是一个本地用户帐户,在本地机器之外没有访问权限。

The rights on the network share are the first layer, after that the NTFS rights on the directory are checked.

网络共享上的权限是第一层,然后检查目录上的 NTFS 权限。

You need to change the identity of the application pool to a domain user with the same rights as your user.

您需要将应用程序池的身份更改为与您的用户具有相同权限的域用户。

回答by Gonza Oviedo

I may be a little late, but i've found that there is a problem on this method of the Directoryclass. Instead i've used DirectoryInfowith impersonation this way:

我可能有点晚了,但我发现Directory类的这种方法存在问题。相反,我DirectoryInfo以这种方式使用了模拟:

new DirectoryInfo(path).Exists

This way you avoid the whole identity change problem, which was denied by our IT area.

这样您就可以避免整个身份更改问题,而我们的 IT 部门拒绝了这一问题。

I hope this helps somebody!

我希望这对某人有所帮助!

回答by Mirko Bellabarba

For future references, this also works:

对于将来的参考,这也适用:

bool result = false;
try
{
    Directory.GetAccessControl(path);
    result = true;
}
catch (UnauthorizedAccessException)
{
    result = true;
}
catch
{
    result = false;
}

回答by Michael Potter

I was was getting this error with code a UNC that looked like this:

我收到此错误,代码为 UNC,如下所示:

@"\Server01\c$\Data\SubFolder"

@"\Server01\c$\Data\SubFolder"

I made an explicit share and got rid of the c$and made it look like this:

我做了一个明确的分享并摆脱c$了它并使它看起来像这样:

@"\Server01\TheData\SubFolder"

@"\Server01\TheData\SubFolder"

and it started to work.

它开始起作用了。

I am not 100% sure that is what fixed the permissions problem, but it started to work immediately after making that change.

我不是 100% 确定这是解决权限问题的原因,但在进行更改后它立即开始工作。