代码之家  ›  专栏  ›  技术社区  ›  Kjensen

重新连接断开的网络驱动器

  •  5
  • Kjensen  · 技术社区  · 16 年前

    如果我试图写一个文件到网络驱动器,这是断开连接,我会得到一个错误。

    如果我双击资源管理器中的驱动器,网络驱动器将重新连接。

    使用system.io.driveinfo.is ready,我可以检查驱动器是否已就绪-但如何在代码中重新连接它?

    2 回复  |  直到 8 年前
        1
  •  5
  •   t0mm13b    10 年前

    会这样吗? code 下面介绍了如何在运行时映射驱动器并动态取消映射?这是代码专家。

        2
  •  0
  •   casiosmu    8 年前

    正如@andersrohratlasinformatik所指出的,公认的解决方案不能与Windows Vista及更高版本一起工作。

    因此,我的解决方案是“简单”启动Explorer作为隐藏窗口,转到网络驱动器并关闭Explorer。后者更难(见 here )因为Explorer对于多个窗口有非常特殊的行为。

    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();
        }
    }
    
    推荐文章