36

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

System.IO.Directory.Exists(@"\\Server\Folder\");

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

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?

Amaranth
  • 2,433
  • 6
  • 36
  • 58

6 Answers6

32

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.

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • 1
    It worked on my local IIS for tests too, but the setting were to Application user, so it was taking my local windows user to connect to the network. I set my specific user to the official server's IIS and it worked. Thanks. – Amaranth Apr 17 '12 at 19:36
  • I don't have a user with rights to all the source and destination directories I need to copy files from and to. Is there any other way to connect to the drives? It also doesn't work with mapped network drives by using the drive letters, so doing a "net use" will also not help I guess. – IngoB Mar 25 '19 at 14:49
  • Thanks so much. Spent half a day with this headache. – Marius Lukošiūnas Apr 02 '21 at 13:47
6

I may be a little late, but i've found that there is a problem on this method of the Directory class. Instead i've used DirectoryInfo with impersonation this way:

new DirectoryInfo(path).Exists

This way you avoid the whole identity change problem, which was denied by our IT area.

I hope this helps somebody!

Gonza Oviedo
  • 1,312
  • 15
  • 20
  • 1
    This doesn't resolve the issue of impersonation. The main difference is that `Directory` is a static class and `DirectoryInfo` is an instance of a class. – Pat Migliaccio Jun 30 '16 at 16:04
  • I'll try this. We are getting intermittant failures with a share from a server between the US and Ireland. There is no indication of server trouble. My guess there is a timeout as this server is at times slow to respond. – D. Kermott Aug 25 '21 at 14:42
2

For future references, this also works:

bool result = false;
try
{
    Directory.GetAccessControl(path);
    result = true;
}
catch (UnauthorizedAccessException)
{
    result = true;
}
catch
{
    result = false;
}
Mirko Bellabarba
  • 562
  • 2
  • 13
0

I was was getting this error with code a UNC that looked like this:

@"\Server01\c$\Data\SubFolder"

I made an explicit share and got rid of the c$ and made it look like this:

@"\Server01\TheData\SubFolder"

and it started to work.

I am not 100% sure that is what fixed the permissions problem, but it started to work immediately after making that change.

Be Kind To New Users
  • 9,672
  • 13
  • 78
  • 125
0

It turned out for me all I had to do when I was adding the network drive was to run the net use ... command from an elevated command prompt. My application runs as administrator so I'm guessing that's probably why it worked after that.

net use G: \\<NETWORK_DRIVE_NAME>\<FOLDER_NAME> /u:<USER> <PASSWORD> /persistent:yes

More Info

cobbs_jobs
  • 98
  • 1
  • 6
0

When i got into the same error, what i did was

  1. Application pool identity changed to custom user with domain user credentials.
  2. Added a virtual directory path to the IIS Website with that network path.

Adding a virtual path to the website

Managing the network path

Last result