Directory.Exists 不适用于网络路径 [英] Directory.Exists not working for a network path

查看:29
本文介绍了Directory.Exists 不适用于网络路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

System.IO.Directory.Exists(@"\ServerFolder");

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

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

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

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

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

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?

推荐答案

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

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

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

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.

网络共享的权限是第一层,之后检查目录的NTFS权限.

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

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

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

这篇关于Directory.Exists 不适用于网络路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆