2009-11-27 93 views
5

Nếu tôi cố gắng ghi tệp vào ổ đĩa mạng bị ngắt kết nối, tôi gặp lỗi.Kết nối lại ổ đĩa mạng đã ngắt kết nối

Nếu tôi doubleclick ổ đĩa trong trình khám phá, ổ đĩa mạng sẽ được kết nối lại.

Sử dụng System.Io.Driveinfo.IsReady Tôi có thể kiểm tra xem ổ đĩa đã sẵn sàng chưa - nhưng làm cách nào tôi có thể kết nối lại trong mã?

Trả lời

5

Đây có phải là code cho biết cách ánh xạ ổ đĩa và hủy ánh xạ động trong thời gian chạy không? Đây là trên CodeGuru.

+1

Lưu ý rằng mã này không hoạt động nữa bắt đầu từ Windows Vista. – AndresRohrAtlasInformatik

0

Khi @AndresRohrAtlasInformatik chỉ ra, giải pháp được chấp nhận không hoạt động với Windows Vista trở lên.

Vì vậy, giải pháp của tôi là để 'đơn giản' bắt đầu thám hiểm như cửa sổ ẩn, đi đến ổ đĩa mạng và thám hiểm gần. Cái thứ hai khó hơn một chút (xem here) vì trình thám hiểm có hành vi rất đặc biệt cho nhiều cửa sổ.

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(); 
    } 
} 
Các vấn đề liên quan