Reconnecting a disconnected network drive

后端 未结 2 1835
醉梦人生
醉梦人生 2020-12-11 17:55

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

If I doubleclick the drive in explorer, the network drive is reconnected.

相关标签:
2条回答
  • 2020-12-11 18:47

    As @AndresRohrAtlasInformatik pointed out, the accepted solution doesn't work with Windows Vista and later.

    So my solution is to 'simply' start explorer as hidden window, go to the network drive and close explorer. The latter is a bit more difficult (see here) because explorer has very special behaviour for multiple 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 讨论(0)
  • 2020-12-11 18:52

    Would this code which shows how to map the drive and unmap it dynamically at runtime do? This is on CodeGuru.

    0 讨论(0)
提交回复
热议问题