Null Reference
Null Reference

Reputation: 11340

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). The code runs fine in debug environment, however in IIS, it always returns 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. It doesn't detect the file on IIS, but works fine on debug

Upvotes: 2

Views: 5163

Answers (4)

noonand
noonand

Reputation: 2855

Your web application is running under a certain security context and you need to find out what context this is. 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)

Alternatively why can't you just use a UNC path (\\serverName\shareName) and avoid all this nonsense?

EDIT: 2013-05-27 To troubleshoot this, create a new application pool, based on whatever app pool you want. Then set the identity that this pool runs under as shown in the attached screenshot.

Screenshot showing setting of IIS creds

Make sure that this user has the correct privileges on the file share and then retest it

Upvotes: 2

Stevie
Stevie

Reputation: 31

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\

If the application is deployed on a separate server we found using the full network name works for example \\server1\folder\

I hope this proves helpful to you.

Upvotes: 2

ducmami
ducmami

Reputation: 215

I think your application do not has permission on "Z:" Is "Z:" network disk?

Upvotes: 8

LbISS
LbISS

Reputation: 630

May be you should use Path.DirectorySeparatorChar

Upvotes: 0

Related Questions