Пример #1
0
        public formMain()
        {
            InitializeComponent();

            disableAutoRun();
            REG_DisableRegedit();
            manager = new UsbManager();
            UsbDiskCollection disks = manager.GetAvailableDisks();

            textBox.AppendText(CR);
            textBox.AppendText("Available USB disks" + CR);

            foreach (UsbDisk disk in disks)
            {
                textBox.AppendText(disk.ToString() + CR);
                textBox.AppendText("Serial: " + getUSBSerial(disk.Name) + CR);

                string hash = sha256(getUSBSerial(getUSBSerial(disk.Name)));
                if (!checkAgainstWhitelist(hash))
                {
                    usb.Eject(disk.Name);
                }
            }

            textBox.AppendText(CR);

            manager.StateChanged += new UsbStateChangedEventHandler(DoStateChanged);
        }
Пример #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 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);
        }