Reconnecting a disconnected network drive

If I try to write a file to a network drive that is offline, I get an error.

If I double-click the drive in Explorer, the network drive will reconnect.

Using System.Io.Driveinfo.IsReady I can check if the drive is ready, but how can I reconnect it in code?

+5
source share
2 answers

Will it be code that shows how to map a disk and untie it dynamically at runtime? This is on CodeGuru.

+5
source

As @AndresRohrAtlasInformatik noted, the decision made does not work with Windows Vista and later.

So, my solution is to "just" launch the explorer in the form of a hidden window, go to the network drive and close the explorer. The latter is a bit more complicated (see here ), because the researcher has a very specific behavior for several windows.

ProcessStartInfo info = new ProcessStartInfo("explorer.exe", myDriveLetter); info.WindowStyle = ProcessWindowStyle.Hidden; Process process = new Process(); process.StartInfo = info; process.Start(); Thread.Sleep(1000); //bool res = process.CloseMainWindow(); // doesn't work for explorer !! //process.Close(); //process.WaitForExit(5000); // https://stackoverflow.com/a/47574704/2925238 ShellWindows _shellWindows = new SHDocVw.ShellWindows(); string processType; foreach (InternetExplorer ie in _shellWindows) { //this parses the name of the process processType = System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower(); //this could also be used for IE windows with processType of "iexplore" if (processType.Equals("explorer") && ie.LocationURL.Contains(myDriveLetter)) { ie.Quit(); } } 
0
source

All Articles