Exemplo n.º 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"];
                }
            }
        }
Exemplo n.º 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));
                }
            }
Exemplo n.º 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();

            // 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);
        }
Exemplo n.º 4
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;
        }