سؤال

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!

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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 :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top