Directory.Exists not working for a network path

后端 未结 4 1686
春和景丽
春和景丽 2020-12-10 11:02

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\\\");
         


        
相关标签:
4条回答
  • 2020-12-10 11:06

    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!

    0 讨论(0)
  • 2020-12-10 11:12

    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.

    0 讨论(0)
  • 2020-12-10 11:16

    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.

    0 讨论(0)
  • 2020-12-10 11:26

    For future references, this also works:

    bool result = false;
    try
    {
        Directory.GetAccessControl(path);
        result = true;
    }
    catch (UnauthorizedAccessException)
    {
        result = true;
    }
    catch
    {
        result = false;
    }
    
    0 讨论(0)
提交回复
热议问题