Esempio n. 1
0
        //========================================================================================
        // 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);
        }
Esempio n. 2
0
        //========================================================================================
        // 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);
        }
Esempio n. 3
0
        //========================================================================================
        // Methods
        //========================================================================================

        /// <summary>
        /// When a USB device comes online or goes offline, this handler manages the
        /// disk collection accordingly.
        /// </summary>
        /// <param name="e"></param>

        private void DoStateChanged(UsbStateChangedEventArgs e)
        {
            if (e.State == UsbStateChange.Added)
            {
                if (!disks.Contains(e.Disk.Name))
                {
                    disks.Add(e.Disk);
                }
            }
            else if (e.State == UsbStateChange.Removed)
            {
                if (disks.Contains(e.Disk.Name))
                {
                    disks.Remove(e.Disk.Name);
                }
            }

            ConveyDevices();
        }
Esempio n. 4
0
        //========================================================================================
        // 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);
        }