2

I have some networkshares mounted to my PC. I can see them in the Windows Explorer, with drive letters etc. If I try to read or write with c#, I always get a DirectoryNotFoundException. The method to check if the directory exists

Directory.Exists(@"N:\test")

returns false (N:\ is the mounted share). If I open the path in the Explorer, the path exists.

Can you imagine, what the problem could be?

Thank you!

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Works fine for me, just tested it. – jAC Jun 02 '13 at 14:53
  • Okay, thanks. I don't know why it should NOT work?! Should be accessable like a local drive. – user2445254 Jun 02 '13 at 14:56
  • Are you sure the folder `test` exists? Can you navigate to that path with the file explorer? – jAC Jun 02 '13 at 14:59
  • Yes, the folder exists. i can navigate to this folder, and create files an so on. My OS is Windows 8, if you think that may be an influence. – user2445254 Jun 02 '13 at 15:02
  • I just tested to see if this works when I run the application as administrator and it failed. So the reason is most probably because the user under which you execute the code doesn't have access to the path. – Alex Filipovici Jun 02 '13 at 15:05
  • **Resolved!** I have to run my programm as administrator. If I dont run it as admin, it works nice! Thanks! Now I have to find a solution, that it is also working with admin rights. Thanks a lot! – user2445254 Jun 02 '13 at 15:12
  • Check out this answer: [http://stackoverflow.com/a/11268410/674700](http://stackoverflow.com/a/11268410/674700) – Alex Filipovici Jun 02 '13 at 15:18
  • I just mounted the network drive with admin rights (run cmd with admin rights, then use "net use"). Now it is also accessible, if the program is running with admin rights :-) – user2445254 Jun 02 '13 at 15:20
  • Should I add my _run as Administrator_ hunch as an answer or will you mark Soner's answer as a solution? – Alex Filipovici Jun 02 '13 at 15:22
  • yes, add it, please. I can add it only in 7 hours, because i have too less points. – user2445254 Jun 02 '13 at 15:32

2 Answers2

1

I just tested to see if this works when I run the application as administrator and it failed. So the reason is most probably because the user under which you execute the code doesn't have access to the path.

As you confirmed that you were indeed running the application with elevated privileges, you should follow the indications that are also suggested in this answer :https://stackoverflow.com/a/11268410/674700:

(...) open an administrative command prompt - where you have an elevated token all the time - and create a matching drive mapping from there (net use h: \server\share1). Since the standard user and the elevated administrator have a common understanding of what "H:" drive means, everything runs okay.

Community
  • 1
  • 1
Alex Filipovici
  • 31,789
  • 6
  • 54
  • 78
  • 1
    The drive is simply mapped under the user that mapped the drive. Sounds reasonable, doesn't it? If you map the drive as a user, and run the program as an Administrator, the administrator does not have the drive mapped. – user1908061 Jun 02 '13 at 17:45
0

Well, I just try to assume why you can get this exception, here it is;

First of all, Directory.Exists() method works fine for network mounted drives. There could be a few more reason that why you get DirectoryNotFoundException in your work.

From MSDN;

The Exists method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.

I believe you have one of this but since we can't acces your computer, we can't know the real reason :)

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364