2009-11-27 89 views
5

如果我嘗試將文件寫入網絡驅動器,即斷開連接,則出現錯誤。重新連接斷開的網絡驅動器

如果我雙擊瀏覽器中的驅動器,網絡驅動器重新連接。

使用System.Io.Driveinfo.IsReady我可以檢查一個驅動器是否準備就緒 - 但是如何在代碼中重新連接它?

回答

5

請問這個code它顯示瞭如何映射驅動器並在運行時動態取消映射嗎?這是在CodeGuru上。

+1

請注意,從Windows Vista開始,此代碼不再有效。 – AndresRohrAtlasInformatik 2017-05-27 08:02:50

0

As @AndresRohrAtlasInformatik指出,接受的解決方案不適用於Windows Vista及更高版本。

所以我的解決方案是'簡單'啓動資源管理器作爲隱藏的窗口,去網絡驅動器和關閉資源管理器。後者有點難(見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(); 
    } 
} 
相關問題