var di = new DirectoryInfo(path) throws exception until I use Windows Explorer to open the path for a first time

StackOverflow https://stackoverflow.com/questions/17583010

  •  02-06-2022
  •  | 
  •  

Question

var di = new DirectoryInfo(Root);
var diList = di.GetDirectories();

The above crashes when accessing the text path 'Root' that is located on the network. I only occurs when a user first logs into Windows 7 (not tested on other OS's). If a user uses windows Explorer to navigate to the specified path, then di is able to retrieve the directories.

I know doing a catch and prompting the user to click a link that opens windows explorer and navigates to the path can be done but I'm not one for work arounds. I want my program to be able to establish the connection to the network path without requiring Windows explorer to do it first.

The exception:

System.IO.IOException: The network name cannot be found.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileSystemEnumerableIterator`1.CommonInit()
at System.IO.FileSystemEnumerableIterator`1..ctor(String path,string originalUserPath,
  String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler,
  Boolean checkHost)
at System.IO.DirectoryInfo.InternalGetDirectories(String searchPattern,
 SearchOption searchOption) at Controls.ValidationControl.LoadDBox(Object sender,
 RoutedEventArgs e)
in c:\Controls\ValidationControl.xaml.cs:line 1010

Thank you in advance for your support, John

Was it helpful?

Solution

Microsoft's security will prevent you from accessing SharePoint via UNC using credentials captured by an application. Therefore, after trying all above solutions and 3 other Network access classes, none would see the path and let me login so I simply used

Process.Start(dir);

and prompt the user as to what is going on. Windows explorer launches, using the user's credentials and navigates to the path where Windows then uses the user's domain account to login to SharePoint. Windows can do it. Programmers that don't work for Microsoft can't.

OTHER TIPS

Since you said it's happening on network folders, my first inclination is that the windows process that this app is running under doesn't have security access to read the directory info. When the user browses to this folder, the security is authenticated, and so your app can now read it.

Does the app work if Root is a local folder?

Most probable reason seems that user need to log in to access the network path. This does not seems to happen until the path is accessed via Explorer. Try mapping the network folder as drive on your local machine and set it to login automatically. This might help it.

EDIT:- As pointed by Scottie in another answer the credentials under which the program runs may also need access to the shared folder.

EDIT#2:- You can use P/Invoke to pass Credentials [Ref]. Or you can call LogonUser method to do impersonation as explained here. Further details regarding impersonation and LogonUser can be found in this question and its answers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top