//======================================================================================== // 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(); 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"]; disks.Add(disk); } } } return(disks); }
public Form1(string[] args) { UsbManager.TheParent = this; onLogin = (args.Length > 0); //previousDrivesRoots = GetDrivesList(); InitializeComponent(); manager = new UsbManager(); UsbDiskCollection disks = manager.GetAvailableDisks(); // Datagrids myDevicesGrid.AllowUserToAddRows = false; // Drives list management appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); myDataLocation = Path.Combine(appDataPath, AppName + " data"); if (!Directory.Exists(myDataLocation)) { Directory.CreateDirectory(myDataLocation); } knownDrivesFile = Path.Combine(myDataLocation, "knownDrives.txt"); extensionsFile = Path.Combine(myDataLocation, "extensions.txt"); FillExtensionsList(); if (System.IO.File.Exists(knownDrivesFile)) { knownDrivesLabels = new List <string>(System.IO.File.ReadAllLines(knownDrivesFile)); for (int i = 0; i < knownDrivesLabels.Count; i++) { string lbl = knownDrivesLabels[i].Substring(1); bool ignore = knownDrivesLabels[i][0] == '1'; myDevicesGrid.Rows.Add(!ignore, lbl); drivesList.Add(new Drive(lbl[0].ToString(), lbl[0].ToString() + ":\\", lbl, false, !ignore)); knownDrivesLabels[i] = lbl; } } foreach (UsbDisk disk in disks) { // Datagrids if (!knownDrivesLabels.Contains(disk.ToString())) { myDevicesGrid.Rows.Add(false, disk.ToString()); drivesList.Add(new Drive(disk.Name[0].ToString(), disk.Name + "\\", disk.ToString(), true, true)); // } else { foreach (Drive d in drivesList) { if (d.Label == disk.ToString()) { d.Connected = true; } } } } AddRemovebleDrives(false); manager.StateChanged += new UsbStateChangedEventHandler(DoStateChanged); startOnLoginCheckBox.Checked = Properties.Settings.Default.startOnLogin; // Copying files allTypesCheckBox.Checked = Properties.Settings.Default.allTypesBool; fileSizeLimitCheckBox.Checked = Properties.Settings.Default.fileSizeLimitBool; totalSizeLimitCheckBox.Checked = Properties.Settings.Default.totalSizeLimitBool; // Copying files fileSizeNum.Value = Properties.Settings.Default.fileSizeLimitInt; totalSizeNum.Value = Properties.Settings.Default.totalSizeLimitInt; if (Properties.Settings.Default.onlyBool) { onlyTypesRadio.Checked = true; } else { exceptionsRadio.Checked = true; } // Output location if (Properties.Settings.Default.outputLocation.Length < 2) { Properties.Settings.Default.outputLocation = outputLocationTxt.Text = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\" + Text + " data\\"; Properties.Settings.Default.Save(); // Saves settings in application configuration file } else { outputLocationTxt.Text = Properties.Settings.Default.outputLocation; } //Hide(); }