简体   繁体   English

File.Exists()始终在IIS上返回false

[英]File.Exists() always returns false on IIS

The file path that I'm checking with File.Exists() resides on a mapped drive ( Z:\\hello.txt ). 我用File.Exists()检查的文件路径驻留在映射的驱动器上( Z:\\hello.txt )。 The code runs fine in debug environment, however in IIS, it always returns false 代码在调试环境中运行良好,但在IIS中,它总是返回false

var fullFileName = string.Format("{0}\\{1}", ConfigurationManager.AppSettings["FileName"], fileName);

if (System.IO.File.Exists(fullFileName))

Why is this so, and how can I workaround this? 为什么会如此,我该如何解决这个问题呢?

I have granted everyone full read/write permissions in that mapped drive 我已授予每个人在该映射驱动器中的完全读/写权限

EDIT: 编辑:

I tried deleting the file via \\\\192.168.1.12\\Examples\\Files\\2.xml and I get the same result. 我尝试通过\\\\192.168.1.12\\Examples\\Files\\2.xml删除文件,我得到相同的结果。 It doesn't detect the file on IIS, but works fine on debug 它不会检测IIS上的文件,但在调试时可以正常工作

我认为您的应用程序没有“Z:”的权限是“Z:”网络磁盘?

I have had similar issues using network mapped drives, when running debug code application works perfectly and when running release version application cannot find the file. 我在使用网络映射驱动器时遇到了类似的问题,当运行调试代码应用程序运行完美时,运行发行版应用程序时无法找到该文件。

If the files are stored on the same server as the application is deployed we found a solution by storing the local drive directory location of the mapped drive for example Z:\\files\\ could be E:\\folder\\folder1\\ 如果文件存储在与部署应用程序相同的服务器上,我们通过存储映射驱动器的本地驱动器目录位置找到了解决方案,例如Z:\\files\\可以是E:\\folder\\folder1\\

If the application is deployed on a separate server we found using the full network name works for example \\\\server1\\folder\\ 如果应用程序部署在单独的服务器上,我们发现使用完整的网络名称工作,例如\\\\server1\\folder\\

I hope this proves helpful to you. 我希望这证明对你有帮助。

Your web application is running under a certain security context and you need to find out what context this is. 您的Web应用程序在某个安全上下文中运行,您需要找出它的上下文。 If it's a normal user, open a command prompt as the user (using the runas tool), map the required drive using the command prompt (be sure to use the /persistent:yes flag) 如果是普通用户,请以用户(使用runas工具)打开命令提示符,使用命令提示符映射所需的驱动器(确保使用/persistent:yes标志)

Alternatively why can't you just use a UNC path ( \\\\serverName\\shareName ) and avoid all this nonsense? 或者为什么你不能只使用UNC路径( \\\\serverName\\shareName )并避免所有这些废话?

EDIT: 2013-05-27 To troubleshoot this, create a new application pool, based on whatever app pool you want. 编辑:2013-05-27要解决此问题,请根据所需的任何应用程序池创建新的应用程序池。 Then set the identity that this pool runs under as shown in the attached screenshot. 然后设置此池运行的标识,如附带的屏幕截图所示。

屏幕截图显示了IIS信用卡的设置

Make sure that this user has the correct privileges on the file share and then retest it 确保此用户对文件共享具有正确的权限,然后重新进行测试

可能是你应该使用Path.DirectorySeparatorChar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM