Beispiel #1
0
        /// <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.Volume    = volume["VolumeName"].ToString();
                    disk.FreeSpace = (ulong)volume["FreeSpace"];
                    disk.Size      = (ulong)volume["Size"];
                }
            }
        }
Beispiel #2
0
            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));
                }
            }
Beispiel #3
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(WMI_DISK_SEARCHER))
            {
                // browse all USB WMI physical disks
                foreach (ManagementObject drive in searcher.Get())
                {
                    string deviceId    = drive["DeviceID"].ToString();
                    string pnpDeviceId = drive["PNPDeviceID"].ToString();
                    string driveModel  = drive["Model"].ToString();

                    // associate physical disks with partitions

                    using (var partitionSearcher = new ManagementObjectSearcher(String.Format(WMI_PARTITION_SEARCHER, deviceId)))
                    {
                        ManagementObject partition = partitionSearcher.First();

                        if (partition != null)
                        {
                            // associate partitions with logical disks (drive letter volumes)

                            using (var logicalSearcher = new ManagementObjectSearcher(String.Format(WMI_LOGICAL_SEARCHER, partition["DeviceID"])))
                            {
                                ManagementObject logical = logicalSearcher.First();

                                if (logical != null)
                                {
                                    var logicalName = logical["Name"].ToString();

                                    // finally find the logical disk entry to determine the volume name
                                    using (var logicalDiskSearcher = new ManagementObjectSearcher(String.Format(WMI_LOGICAL_DISK_SEARCHER, logicalName)))
                                    {
                                        ManagementObject volume = logicalDiskSearcher.First();

                                        UsbDisk disk = new UsbDisk(logicalName);
                                        disk.DeviceId     = deviceId;
                                        disk.PnpDeviceId  = pnpDeviceId;
                                        disk.Model        = driveModel;
                                        disk.Volume       = volume["VolumeName"].ToString();
                                        disk.FreeSpace    = (ulong)volume["FreeSpace"];
                                        disk.Size         = (ulong)volume["Size"];
                                        disk.SerialNumber = volume["VolumeSerialNumber"].ToString();
                                        disks.Add(disk);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(disks);
        }
Beispiel #4
0
        public static UsbDisk deserialize(string serialized)
        {
            string[] a    = serialized.Split(SERIAL_DELIMITOR_OBJECT);
            UsbDisk  disk = new UsbDisk(a[0]);

            disk.Volume       = a[1];
            disk.Model        = a[2];
            disk.SerialNumber = a[3];
            disk.Size         = (ulong)Int64.Parse(a[4]);
            return(disk);
        }
Beispiel #5
0
        /// <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);
            }
        }
        /// <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;
        }