2

I have an issue where file.exists aways returns false.

situation: server 1 that executes file.exists has a mapped networkdrive to server 2 that is on a remote location.

Accessing and browsing the network drive is ok.

But when running the webpage, I always get a false. Even the path is ok, and de file exists.

I tried both situations: U:/folders/file.txt and //ipadress/folders/file.txt

The shared folder on server2 even has permissions set to everyone full control.

Strange thing is that when running debug in visual studio, it works, and the file.exists return true.

TomVD
  • 109
  • 2
  • 13
  • Is it as simple as having your slash character mixed up? – Paul Alan Taylor Nov 14 '12 at 12:03
  • no, already checked that. guess it has to do with some permissions – TomVD Nov 14 '12 at 12:09
  • Do you use `File.Exists(path)` or `FileInfo.Exists`? – Tim Schmelter Nov 14 '12 at 12:11
  • Do you run the Visual Studio under the Administrator account and your program under the User acccount? – Alex Nov 14 '12 at 12:21
  • U:\folders\file.txt and \\ipadress\folders\file.txt. The slashes should be like this. – Vasanthan Nov 14 '12 at 12:34
  • A few more things to check: Can you tell if the directory exists using `Directory.Exists`? When you say accessing and browsing the network drive is ok is that from the server? When you are debugging in visual studio is that debugging on the server or a local dev machine? And to confirm you can use / or \ and it recognises it as a path separator. – Chris Nov 14 '12 at 12:40
  • Recheck permissions. Even turn on guest (to test). The web server will run under a different userID. If debug you are running under your logon. – paparazzo Nov 14 '12 at 13:32

3 Answers3

1

Try parsing it to an Uri and check at LocalPath property. Uri has a "TryCreate" for this purpose if you don't want to handle a possible exception.

If you're accessing it from a webpage, it's possible the webpage doesn't have mappings (i.e. U:) and you will need to use the full name (like \\myserver\folder\etc). Check the 2nd answer in this thread for some very useful methods that will let you do this: How do I determine a mapped drive's actual path?

Community
  • 1
  • 1
Joe
  • 2,496
  • 1
  • 22
  • 30
  • that was part of the solution: the asp.net user doesn't have mapped drives.I removed the file.exists, and switched to http response on url. – TomVD Nov 15 '12 at 10:59
0

Drives are mapped per-user, not per-machine.

This means that if you mapped a drive using your own user account, but the server is using another, the server won't see your mapped drive.

Rolf Bjarne Kvinge
  • 19,253
  • 2
  • 42
  • 86
0

I guess adding the following command in the user's logon script, who is set in "Log on As:" option of IIS or your application service should resolve the issue.

net use x: \\SERVERNAME\SHARENAME
deenaik
  • 724
  • 7
  • 13