private void UnmountAndDetach(string vhdFileName) { FileInfo fi = new FileInfo(vhdFileName); if (!fi.Exists) { return; } Log.WriteLine("UnmountAndDetach({0})", vhdFileName); fi.IsReadOnly = false; //unlock file to enable load try { if (_virtualDisk != null) // && _virtualDisk.FileName.CompareTo(vhdFileName) != 0) { _virtualDisk.Close(); _virtualDisk = null; } if (_virtualDisk == null || !_virtualDisk.IsOpen) { _virtualDisk = new Medo.IO.VirtualDisk(vhdFileName); _virtualDisk.Open(); } string diskPath = _virtualDisk.GetAttachedPath(); if (!string.IsNullOrWhiteSpace(diskPath)) //if device is attached { DiskCryptor.DriveInfo drive = FindDriveInfo(diskPath); if (drive != null) { try { UnmountDiscryptorDrive(drive); } catch (Exception err) { Log.WriteLine("UnmountDiscryptorDrive({0}) - Error: {1}", diskPath, err); } } _virtualDisk.Detach(); } } catch (Exception err) { Log.WriteLine("UnmountAndDetach({0}): {1}", vhdFileName, err.ToString()); throw; } finally { if (_virtualDisk != null) { _virtualDisk.Close(); _virtualDisk = null; } fi.IsReadOnly = true; //lock file to improve security } }
private static void ReceivedDetach(TinyPacket packet) { try { var path = packet["Path"]; using (var disk = new Medo.IO.VirtualDisk(path)) { disk.Open(Medo.IO.VirtualDiskAccessMask.Detach); disk.Detach(); } } catch (Exception ex) { throw new InvalidOperationException(string.Format("Virtual disk file \"{0}\" cannot be detached.", (new FileInfo(packet["Path"])).Name), ex); } }
private void m_btnAttachVHDandMount_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(GetPasswordFunc())) { PopUp.Error("Password is empty", "Password"); return; } FileInfo fi = new FileInfo(m_cmbVHD_FileName.Text); if (!fi.Exists) { if (PopUp.Question("File not found: " + fi.FullName + "\nRemove from Recent Files List?", "Mount VHD - ERROR", MessageBoxImage.Asterisk, TextAlignment.Center, PopUp.PopUpButtonsType.NoYes) == PopUp.PopUpResult.Yes) { _recentFiles.RemoveFromList(m_cmbVHD_FileName.Text, m_mnuFileAttachVHD.DropDown, m_cmbVHD_FileName); } return; } ExecuteClickAction(() => { _recentFiles.AddRecent(m_cmbVHD_FileName.Text, m_mnuFileAttachVHD.DropDown, m_cmbVHD_FileName); fi.IsReadOnly = false; //unlock file to enable load _cachedDriveInfo = new List <DiskCryptor.DriveInfo>(_diskCryptor.DriveList); if (_virtualDisk != null) { _virtualDisk.Close(); } _virtualDisk = new Medo.IO.VirtualDisk(fi.FullName); _virtualDisk.Open(); try { _virtualDisk.Detach(); } catch (Exception err) { Debug.WriteLine("Cannot detach: " + err.Message); } _selectedDriveLetterForMount = _selectedDriveLetter; _diskCryptor.OnDisksAdded += OnDiskAddedMountDiskCryptorDisk; //mount DiskCryptor disk Medo.IO.VirtualDiskAttachOptions options = Medo.IO.VirtualDiskAttachOptions.NoDriveLetter; if (m_chkPermanent.Checked) { options |= Medo.IO.VirtualDiskAttachOptions.PermanentLifetime; } _virtualDisk.Attach(options); fi.IsReadOnly = true; //lock file to improve security }, sender); }
private static void DetachDrive(string path) { var device = DeviceFromPath.GetDevice(path); #region VDS COM FileInfo vhdFile = null; VdsServiceLoader loaderClass = new VdsServiceLoader(); IVdsServiceLoader loader = (IVdsServiceLoader)loaderClass; IVdsService service; loader.LoadService(null, out service); service.WaitForServiceReady(); IEnumVdsObject providerEnum; service.QueryProviders(VDS_QUERY_PROVIDER_FLAG.VDS_QUERY_VIRTUALDISK_PROVIDERS, out providerEnum); while (true) { uint fetchedProvider; object unknownProvider; providerEnum.Next(1, out unknownProvider, out fetchedProvider); if (fetchedProvider == 0) break; IVdsVdProvider provider = (IVdsVdProvider)unknownProvider; Console.WriteLine("Got VD Provider"); IEnumVdsObject diskEnum; provider.QueryVDisks(out diskEnum); while (true) { uint fetchedDisk; object unknownDisk; diskEnum.Next(1, out unknownDisk, out fetchedDisk); if (fetchedDisk == 0) break; IVdsVDisk vDisk = (IVdsVDisk)unknownDisk; VDS_VDISK_PROPERTIES vdiskProperties; vDisk.GetProperties(out vdiskProperties); try { IVdsDisk disk; provider.GetDiskFromVDisk(vDisk, out disk); VDS_DISK_PROP diskProperties; disk.GetProperties(out diskProperties); if (diskProperties.pwszName.Equals(device, StringComparison.OrdinalIgnoreCase)) { vhdFile = new FileInfo(vdiskProperties.pPath); break; } else { Trace.TraceError(diskProperties.pwszName + " = " + vdiskProperties.pPath); } Console.WriteLine("-> Disk Name=" + diskProperties.pwszName); Console.WriteLine("-> Disk Friendly=" + diskProperties.pwszFriendlyName); } catch (COMException) { } } if (vhdFile != null) { break; } } #endregion if (vhdFile != null) { using (var disk = new Medo.IO.VirtualDisk(vhdFile.FullName)) { disk.Open(Medo.IO.VirtualDiskAccessMask.Detach); disk.Detach(); } } else { throw new FormatException(string.Format("Drive \"{0}\" is not a virtual hard disk.", path)); } }
private static void DetachDrive(string path) { var device = DeviceFromPath.GetDevice(path); #region VDS COM FileInfo vhdFile = null; VdsServiceLoader loaderClass = new VdsServiceLoader(); IVdsServiceLoader loader = (IVdsServiceLoader)loaderClass; IVdsService service; loader.LoadService(null, out service); service.WaitForServiceReady(); IEnumVdsObject providerEnum; service.QueryProviders(VDS_QUERY_PROVIDER_FLAG.VDS_QUERY_VIRTUALDISK_PROVIDERS, out providerEnum); while (true) { uint fetchedProvider; object unknownProvider; providerEnum.Next(1, out unknownProvider, out fetchedProvider); if (fetchedProvider == 0) { break; } IVdsVdProvider provider = (IVdsVdProvider)unknownProvider; Console.WriteLine("Got VD Provider"); IEnumVdsObject diskEnum; provider.QueryVDisks(out diskEnum); while (true) { uint fetchedDisk; object unknownDisk; diskEnum.Next(1, out unknownDisk, out fetchedDisk); if (fetchedDisk == 0) { break; } IVdsVDisk vDisk = (IVdsVDisk)unknownDisk; VDS_VDISK_PROPERTIES vdiskProperties; vDisk.GetProperties(out vdiskProperties); try { IVdsDisk disk; provider.GetDiskFromVDisk(vDisk, out disk); VDS_DISK_PROP diskProperties; disk.GetProperties(out diskProperties); if (diskProperties.pwszName.Equals(device, StringComparison.OrdinalIgnoreCase)) { vhdFile = new FileInfo(vdiskProperties.pPath); break; } else { Trace.TraceError(diskProperties.pwszName + " = " + vdiskProperties.pPath); } Console.WriteLine("-> Disk Name=" + diskProperties.pwszName); Console.WriteLine("-> Disk Friendly=" + diskProperties.pwszFriendlyName); } catch (COMException) { } } if (vhdFile != null) { break; } } #endregion if (vhdFile != null) { using (var disk = new Medo.IO.VirtualDisk(vhdFile.FullName)) { disk.Open(Medo.IO.VirtualDiskAccessMask.Detach); disk.Detach(); } } else { throw new FormatException(string.Format("Drive \"{0}\" is not a virtual hard disk.", path)); } }