/// <summary> /// Populate the missing properties of the given disk before sending to listeners /// </summary> /// <param name="disk"></param> private void GetDiskInformation(UsbDisk disk) { using (var partition = new ManagementObjectSearcher( $"associators of {{Win32_LogicalDisk.DeviceID='{disk.Name}'}} where AssocClass = Win32_LogicalDiskToPartition") .First()) { if (partition != null) { using (var drive = new ManagementObjectSearcher( $"associators of {{Win32_DiskPartition.DeviceID='{partition["DeviceID"]}'}} where resultClass = Win32_DiskDrive") .First()) { if (drive != null) { disk.Model = drive["Model"].ToString(); } using (var volume = new ManagementObjectSearcher( $"select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{disk.Name}'") .First()) { if (volume != null) { disk.Volume = volume["VolumeName"].ToString(); disk.FreeSpace = (ulong)volume["FreeSpace"]; disk.Size = (ulong)volume["Size"]; } } } } } }
/// <summary> /// Populate the missing properties of the given disk before sending to listeners /// </summary> /// <param name="disk"></param> private void GetDiskInformation(UsbDisk disk) { ManagementObject partition = new ManagementObjectSearcher(String.Format( "associators of {{Win32_LogicalDisk.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition", disk.Name)).First(); if (partition != null) { ManagementObject drive = new ManagementObjectSearcher(String.Format( "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where resultClass = Win32_DiskDrive", partition["DeviceID"])).First(); //MessageBox.Show(drive.ToString()); if (drive != null) { disk.Model = drive["Model"].ToString(); } ManagementObject volume = new ManagementObjectSearcher(String.Format( "select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{0}'", disk.Name)).First(); if (volume != null) { disk.VolumeName = volume["VolumeName"].ToString(); disk.FreeSpace = (ulong)volume["FreeSpace"]; disk.Size = (ulong)volume["Size"]; disk.DriveType = Drivetypeconvert(Int32.Parse(volume["DriveType"].ToString())); //MessageBox.Show(disk.DriveType); } } }
private void SignalDeviceChange(UsbStateChange state, DEV_BROADCAST_VOLUME volume) { string name = ToUnitName(volume.dbcv_unitmask); if (StateChanged != null) { UsbDisk disk = new UsbDisk(name); StateChanged(new UsbStateChangedEventArgs(state, disk)); } }
private void SignalDeviceChange(UsbStateChange state, Message message) { DEV_BROADCAST_VOLUME volume = (DEV_BROADCAST_VOLUME) Marshal.PtrToStructure(message.LParam, typeof(DEV_BROADCAST_VOLUME)); string name = ToUnitName(volume.dbcv_unitmask); if (StateChanged != null) { UsbDisk disk = new UsbDisk(name); StateChanged(new UsbStateChangedEventArgs(state, disk)); } }
/// <summary> /// /// </summary> /// <param name="value"></param> /// <param name="targetType"></param> /// <param name="parameter"></param> /// <param name="culture"></param> /// <returns></returns> public object Convert( object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) { return("-"); } UsbDisk.UsbSpace space = (UsbDisk.UsbSpace)value; return(String.Format(Resx.FreeSpace, UsbDisk.FormatByteCount(space.FreeSpace), UsbDisk.FormatByteCount(space.Size))); }
/// <summary> /// When a new USB device is selected, we need to update the location information. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DoDeviceSelectionChanged(object sender, SelectionChangedEventArgs e) { UsbDisk disk = devicesBox.SelectedItem as UsbDisk; if (disk != null) { locationBox.Text = disk.Name + Path.DirectorySeparatorChar.ToString(); } else { locationBox.Text = Path.DirectorySeparatorChar.ToString(); } }
//======================================================================================== // Methods //======================================================================================== /// <summary> /// Gets a collection of all available USB disk drives currently mounted. /// </summary> /// <returns> /// A UsbDiskCollection containing the USB disk drives. /// </returns> public UsbDiskCollection GetAvailableDisks() { UsbDiskCollection disks = new UsbDiskCollection(); using (var searcher = new ManagementObjectSearcher( "select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'").Get()) { // browse all USB WMI physical disks foreach (var o in searcher) { var drive = (ManagementObject)o; // associate physical disks with partitions using (var partition = new ManagementObjectSearcher( $"associators of {{Win32_DiskDrive.DeviceID='{drive["DeviceID"]}'}} where AssocClass = Win32_DiskDriveToDiskPartition") .First()) { if (partition != null) { // associate partitions with logical disks (drive letter volumes) using (var logical = new ManagementObjectSearcher( $"associators of {{Win32_DiskPartition.DeviceID='{partition["DeviceID"]}'}} where AssocClass = Win32_LogicalDiskToPartition") .First()) { if (logical != null) { // finally find the logical disk entry to determine the volume name using (var volume = new ManagementObjectSearcher( $"select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{logical["Name"]}'") .First()) { var disk = new UsbDisk(logical["Name"].ToString()) { Model = drive["Model"].ToString(), Volume = volume["VolumeName"].ToString(), FreeSpace = (ulong)volume["FreeSpace"], Size = (ulong)volume["Size"] }; disks.Add(disk); } } } } } } } return(disks); }
public void CollectionTest() { UsbDiskCollection disks = new UsbDiskCollection(); Assert.IsFalse(disks.Contains("foo")); UsbDisk disk = new UsbDisk("foo"); disks.Add(disk); Assert.IsTrue(disks.Contains(disk)); Assert.IsTrue(disks.Contains("foo")); Assert.IsFalse(disks.Contains("missing")); disks.Remove("foo"); Assert.AreEqual(0, disks.Count); Assert.IsFalse(disks.Contains("foo")); }
//======================================================================================== // Methods //======================================================================================== /// <summary> /// Gets a collection of all available USB disk drives currently mounted. /// </summary> /// <returns> /// A UsbDiskCollection containing the USB disk drives. /// </returns> public UsbDiskCollection GetAvailableDisks() { UsbDiskCollection disks = new UsbDiskCollection(); // browse all USB WMI physical disks foreach (ManagementObject drive in new ManagementObjectSearcher( "select DeviceID, Model,Size from Win32_DiskDrive where InterfaceType='USB' or MediaType='External hard disk media'").Get()) { // associate physical disks with partitions foreach (ManagementObject partition in new ManagementObjectSearcher(string.Format( "associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition", drive["DeviceID"])).Get()) { try { if (partition != null) { // associate partitions with logical disks (drive letter volumes) ManagementObject logical = new ManagementObjectSearcher(String.Format( "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition", partition["DeviceID"])).First(); if (logical != null) { // finally find the logical disk entry to determine the volume name ManagementObject volume = new ManagementObjectSearcher(String.Format( "select FreeSpace, Size, VolumeName, DriveType from Win32_LogicalDisk where Name='{0}'", logical["Name"])).First(); UsbDisk disk = new UsbDisk(logical["Name"].ToString()); disk.Model = drive["Model"].ToString(); disk.Volume = volume["VolumeName"].ToString(); disk.FreeSpace = (ulong)volume["FreeSpace"]; disk.Size = (ulong)volume["Size"]; disk.DiskSize = (ulong)drive["Size"]; disk.DriveType = Drivetypeconvert(Int32.Parse(volume["DriveType"].ToString())); disks.Add(disk); //MessageBox.Show("HERE"); } } } catch { } } } return(disks); }
//======================================================================================== // Methods //======================================================================================== /// <summary> /// When the auto-detect checkbox is toggled, we need to reset the related input /// controls to reflect the most appropriate state. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DoAutoDetectChanged(object sender, RoutedEventArgs e) { bool enabled = (autoDetectBox.IsChecked != true); formatBox.IsEnabled = enabled; locationBox.IsEnabled = enabled; selectFolderButton.IsEnabled = enabled; if (!enabled) { UsbDisk disk = devicesBox.SelectedItem as UsbDisk; if (disk != null) { locationBox.Text = disk.Name + Path.DirectorySeparatorChar.ToString(); } } }
/// <summary> /// Allows the user to browse/create a target folder on the USB device. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DoSelectFolder(object sender, RoutedEventArgs e) { UsbDisk disk = devicesBox.SelectedItem as UsbDisk; if (disk != null) { using (var dialog = new WinForms.FolderBrowserDialog()) { dialog.Description = "Select the root synchronization folder"; dialog.RootFolder = Environment.SpecialFolder.MyComputer; dialog.SelectedPath = locationBox.Text; dialog.ShowNewFolderButton = true; WinForms.DialogResult result = dialog.ShowDialog(); if (result == WinForms.DialogResult.OK) { locationBox.Text = dialog.SelectedPath; } } } }
/// <summary> /// Internally handle state changes and notify listeners. /// </summary> /// <param name="e"></param> private void DoStateChanged(UsbStateChangedEventArgs e) { if (handler != null) { UsbDisk disk = e.Disk; // we can only interrogate drives that are added... // cannot see something that is no longer there! if ((e.State == UsbStateChange.Added) && (e.Disk.Name[0] != '?')) { // the following Begin/End invokes looks strange but are required // to resolve a "DisconnectedContext was detected" exception which // occurs when the current thread terminates before the WMI queries // can complete. I'm not exactly sure why that would happen... GetDiskInformationDelegate gdi = new GetDiskInformationDelegate(GetDiskInformation); IAsyncResult result = gdi.BeginInvoke(e.Disk, null, null); gdi.EndInvoke(result); } handler(e); } }
//======================================================================================== // Methods //======================================================================================== /// <summary> /// Gets a collection of all available USB disk drives currently mounted. /// </summary> /// <returns> /// A UsbDiskCollection containing the USB disk drives. /// </returns> public UsbDiskCollection GetAvailableVolumns() { UsbDiskCollection volumns = new UsbDiskCollection(); // browse all USB WMI physical disks foreach (ManagementObject drive in new ManagementObjectSearcher( "select DeviceID,Model,Size,Index,MediaType,Size,InterfaceType from Win32_DiskDrive").Get()) { try { //if ((uint)drive["Index"] < 1) continue; //if (drive["InterfaceType"].ToString() != "USB" && drive["InterfaceType"].ToString() != "SCSI") continue; if (drive["Model"].ToString().Contains("APPLE SD")) { continue; } UsbDisk disk = new UsbDisk(drive["DeviceID"].ToString()); disk.Model = drive["Model"].ToString(); disk.Index = drive["Index"].ToString(); disk.DiskSize = (ulong)drive["Size"]; disk.DriveType = drive["MediaType"].ToString(); disk.Size = (ulong)drive["Size"]; // associate physical disks with partitions foreach (ManagementObject partition in new ManagementObjectSearcher(string.Format( "associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition", drive["DeviceID"])).Get()) { try { if (partition != null) { // associate partitions with logical disks (drive letter volumes) ManagementObject logical = new ManagementObjectSearcher(string.Format( "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition", partition["DeviceID"])).First(); if (logical != null) { // finally find the logical disk entry to determine the volume name ManagementObject volume = new ManagementObjectSearcher(string.Format( "select FreeSpace, Size, VolumeName, DriveType, FileSystem from Win32_LogicalDisk where Name='{0}'", logical["Name"])).First(); UsbDisk diskVolume = new UsbDisk(drive["DeviceID"].ToString()); diskVolume.Model = drive["Model"].ToString(); diskVolume.Index = drive["Index"].ToString(); diskVolume.Volume = logical["Name"].ToString(); diskVolume.FileSystem = volume["FileSystem"].ToString(); diskVolume.DiskSize = (ulong)drive["Size"]; diskVolume.DriveType = drive["MediaType"].ToString(); diskVolume.Size = (ulong)volume["Size"]; //diskVolume.DiskSize = (ulong)volume["Size"]; diskVolume.FreeSpace = (ulong)volume["FreeSpace"]; volumns.Add(diskVolume); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); //disks.Add(new UsbDisk(ex.ToString())); } } //if (!hasAdded) disks.Add(disk); } catch (Exception e) { Console.WriteLine(e.ToString()); //MessageBox.Show(e.ToString()); } } return(volumns); }
/// <summary> /// Initialize a new instance with the specified state and disk. /// </summary> /// <param name="state">The state change code.</param> /// <param name="disk">The USB disk description.</param> public UsbStateChangedEventArgs(UsbStateChange state, UsbDisk disk) { this.State = state; this.Disk = disk; }
/// <summary> /// Populate the missing properties of the given disk before sending to listeners /// </summary> /// <param name="disk"></param> private void GetDiskInformation(UsbDisk disk) { ManagementObject partition = new ManagementObjectSearcher(String.Format( "associators of {{Win32_LogicalDisk.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition", disk.Name)).First(); if (partition != null) { ManagementObject drive = new ManagementObjectSearcher(String.Format( "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where resultClass = Win32_DiskDrive", partition["DeviceID"])).First(); if (drive != null) { disk.Model = drive["Model"].ToString(); } ManagementObject volume = new ManagementObjectSearcher(String.Format( "select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{0}'", disk.Name)).First(); if (volume != null) { disk.VolumeLabel = volume["VolumeName"].ToString(); disk.AvailableFreeSpace = (ulong)volume["FreeSpace"]; disk.TotalSize = (ulong)volume["Size"]; } } }
//======================================================================================== // Methods //======================================================================================== /// <summary> /// Gets a collection of all available USB disk drives currently mounted. /// </summary> /// <returns> /// A UsbDiskCollection containing the USB disk drives. /// </returns> public UsbDiskCollection GetAvailableDisks() { UsbDiskCollection disks = new UsbDiskCollection(); // browse all USB WMI physical disks foreach (ManagementObject drive in new ManagementObjectSearcher( "select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'").Get()) { // associate physical disks with partitions ManagementObject partition = new ManagementObjectSearcher(String.Format( "associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition", drive["DeviceID"])).First(); if (partition != null) { // associate partitions with logical disks (drive letter volumes) ManagementObject logical = new ManagementObjectSearcher(String.Format( "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition", partition["DeviceID"])).First(); if (logical != null) { // finally find the logical disk entry to determine the volume name ManagementObject volume = new ManagementObjectSearcher(String.Format( "select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{0}'", logical["Name"])).First(); try { if (volume["VolumeName"] != null) { UsbDisk disk = new UsbDisk(logical["Name"].ToString()); disk.Model = drive["Model"].ToString(); disk.VolumeLabel = volume["VolumeName"].ToString(); disk.AvailableFreeSpace = (ulong) volume["FreeSpace"]; disk.TotalSize = (ulong) volume["Size"]; disks.Add(disk); } }catch(NullReferenceException) { } } } } return disks; }
//======================================================================================== // Methods //======================================================================================== /// <summary> /// Gets a collection of all available USB disk drives currently mounted. /// </summary> /// <returns> /// A UsbDiskCollection containing the USB disk drives. /// </returns> public UsbDiskCollection GetAvailableDisks () { UsbDiskCollection disks = new UsbDiskCollection(); // browse all USB WMI physical disks foreach (ManagementObject drive in new ManagementObjectSearcher( "select DeviceID, Model from Win32_DiskDrive where InterfaceType='USB' or MediaType='External hard disk media'").Get()) { // associate physical disks with partitions foreach (ManagementObject partition in new ManagementObjectSearcher(String.Format( "associators of {{Win32_DiskDrive.DeviceID='{0}'}} where AssocClass = Win32_DiskDriveToDiskPartition", drive["DeviceID"])).Get()) { try { if (partition != null) { // associate partitions with logical disks (drive letter volumes) ManagementObject logical = new ManagementObjectSearcher(String.Format( "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition", partition["DeviceID"])).First(); if (logical != null) { // finally find the logical disk entry to determine the volume name ManagementObject volume = new ManagementObjectSearcher(String.Format( "select FreeSpace, Size, VolumeName, DriveType from Win32_LogicalDisk where Name='{0}'", logical["Name"])).First(); UsbDisk disk = new UsbDisk(logical["Name"].ToString()); disk.Model = drive["Model"].ToString(); disk.Volume = volume["VolumeName"].ToString(); disk.FreeSpace = (ulong)volume["FreeSpace"]; disk.Size = (ulong)volume["Size"]; disk.DriveType = Drivetypeconvert(Int32.Parse(volume["DriveType"].ToString())); disks.Add(disk); //MessageBox.Show("HERE"); } } } catch { } } } return disks; }
/// <summary> /// Populate the missing properties of the given disk before sending to listeners /// </summary> /// <param name="disk"></param> private void GetDiskInformation (UsbDisk disk) { ManagementObject partition = new ManagementObjectSearcher(String.Format( "associators of {{Win32_LogicalDisk.DeviceID='{0}'}} where AssocClass = Win32_LogicalDiskToPartition", disk.Name)).First(); if (partition != null) { ManagementObject drive = new ManagementObjectSearcher(String.Format( "associators of {{Win32_DiskPartition.DeviceID='{0}'}} where resultClass = Win32_DiskDrive", partition["DeviceID"])).First(); //MessageBox.Show(drive.ToString()); if (drive != null) { disk.Model = drive["Model"].ToString(); } ManagementObject volume = new ManagementObjectSearcher(String.Format( "select FreeSpace, Size, VolumeName from Win32_LogicalDisk where Name='{0}'", disk.Name)).First(); if (volume != null) { disk.Volume = volume["VolumeName"].ToString(); disk.FreeSpace = (ulong)volume["FreeSpace"]; disk.Size = (ulong)volume["Size"]; disk.DriveType = Drivetypeconvert(Int32.Parse (volume["DriveType"].ToString ())); //MessageBox.Show(disk.DriveType); } } }
private void button2_Click(object sender, EventArgs e) { folderBrowserDialog1.ShowDialog(); if (folderBrowserDialog1.SelectedPath.Length != 3) { if (folderBrowserDialog1.SelectedPath.Length != 0) { //MsgManager.getResString("Msg_UDRoot") //请选择优盘根目录 MessageBox.Show(MsgManager.GetResString("Msg_UDRoot", MsgManager.ci)); } return; } UsbDisk udM = new UsbDisk(folderBrowserDialog1.SelectedPath); //udM.Volume = folderBrowserDialog1.SelectedPath; diskCollection.Add(udM); //UDList.Add(folderBrowserDialog1.SelectedPath); OutText(true, diskCollection); }
private void GetUdiskInfo() { string newlist = string.Empty; UsbManager manager = new UsbManager(); try { diskCollection.Clear(); //UsbDiskCollection disks = manager.GetAvailableDisks(); UsbDisk udChoose = new UsbDisk(MsgManager.GetResString("Msg_chooseud", MsgManager.ci)); diskCollection.Add(udChoose); //if (disks == null) { return; } foreach (UsbDisk disk in manager.GetAvailableDisks()) { diskCollection.Add(disk); newlist += disk.ToString(); } if (newlist != currentlist) { currentlist = newlist; OutText(false, diskCollection); } } catch (Exception ex) { Log.WriteLog("GetUdiskInfo.log", ex.ToString()); } finally { manager.Dispose(); } }