Directory.Exists网络路径返回false [英] Directory.Exists network path returns false

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

问题描述




在ASP.NET页面上,我尝试使用

以下代码访问网络共享目录:



 字符串 txtDir = TextBox1.Text.ToString(); 
if (Directory.Exists(txtDir))
{
Response.Write( 目录存在);
}
else
{
Response.Write( 目录不存在);
}







当我运行代码时这很好用。



但是在IIS中托管后,尽管目录具有所需的权限,但Directory.Exists总是返回false。



我试图放置一个文件上传控件,它允许我浏览'' Directory.Exists ''返回false的路径。



有谁知道为什么这个方法为网络路径返回true?





谢谢

解决方案

因为C#代码在服务器上运行,而不是在客户端上运行。上传控件在客户端上查找,并非常愉快地找到该文件夹​​。您的代码查看服务器的文件系统,该文件夹不存在。


在Visual Studio中运行代码时,它以用户的权限运行。



在IIS中运行代码时,它以应用程序池的标识运行,默认情况下是内置用户网络服务这是本地用户帐户,无权访问本地计算机。网络共享上的权限是第一层,之后检查目录上的NTFS权限。您需要将应用程序池的标识更改为具有与您的用户相同权限的域用户。

[实际答案参考 [ ^ ]]





- -Amit

Hi
On an ASP.NET page I try to access a network shared directory using the
following code:

String txtDir = TextBox1.Text.ToString();
if (Directory.Exists(txtDir))
{
   Response.Write("The Directory exists");
}
else
{
   Response.Write("The Directory doesn't exists");
}




This works fine when I ran through the code.

But after hosting it in the IIS, Directory.Exists always returns false though the directory has the required permissions.

I tried to place a File upload control and it allows me to browse the path for which the ''Directory.Exists'' returns false.

Does anyone know why this method returns true for a network path?


Thanks

解决方案

Because C# code runs on the server, not the client. The upload control looks on the client, and finds the folder perfectly happily. Your code looks at the file system of the Server, where the folder does not exist.


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

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. 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.
[Actual answer Reference[^]]


--Amit


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

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