public void Dispose() { if (!_mapped) { return; } WshNetwork net = null; try { net = new WshNetworkClass(); object force = true; object updateProfile = false; net.RemoveNetworkDrive(_letterPath, ref force, ref updateProfile); } catch (Exception ex) { Console.WriteLine("Failed to disconnect drive: {0}.", ex.Message); } finally { if (net != null) { Marshal.ReleaseComObject(net); } } }
public MappedDrive(char driveLetter, string url) { _letterPath = driveLetter + ":"; WshNetwork net = null; try { net = new WshNetworkClass(); object updateProfile = false; net.MapNetworkDrive(_letterPath, url, ref updateProfile); _mapped = true; // use windows explorer to test it! using (System.Diagnostics.Process.Start(_letterPath)) { } } catch (Exception ex) { Console.WriteLine("Failed to connect drive: {0}.", ex.Message); } finally { if (net != null) { Marshal.ReleaseComObject(net); } } }