Example #1
0
        private void IterateDevices()
        {
            Console.WriteLine("Iterating");
            string model;
            string deviceString;
            ManagementObjectSearcher physicalSearch = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = \"USB\" AND Size <= 17179869184");
            foreach (ManagementObject physicalDrive in physicalSearch.Get())
            {
                if (physicalDrive["MediaType"].ToString().Substring(0, 9) != "Removable")
                    continue;

                if (physicalDrive["Model"] == null)
                    model = "Unknown Device";
                else
                    model = physicalDrive["Model"].ToString();
                deviceString = model + " - (" + Math.Round(((double)Convert.ToDouble(physicalDrive["Size"]) / 1048576), 2) + " MB)";
                ImageDevice newDevice = new ImageDevice(deviceString);
                newDevice.size = (long) Convert.ToInt64(physicalDrive["Size"]);
                newDevice.name = physicalDrive["Name"].ToString();
                newDevice.partitions = Convert.ToInt32(physicalDrive["Partitions"]);
                // Now see if there are any mounted volumes
                foreach(ManagementObject partition in new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + physicalDrive["DeviceID"] + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition").Get())
                    foreach(ManagementObject disk in new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + partition["DeviceID"] + "'} WHERE AssocClass = Win32_LogicalDiskToPartition").Get())
                        newDevice.volume = "\\\\.\\" + disk["Name"].ToString();
                deviceCombo.Items.Add(newDevice);
            }
        }
Example #2
0
        private void ButtonClicked(object sender, System.EventArgs e)
        {
            if (imageFile == "")
            {
                MessageBox.Show("Please choose an image to write", "Error", MessageBoxButtons.OK);
                return;
            }

            device = (ImageDevice) deviceCombo.SelectedItem;
            if (imageSize > device.size)
            {
                MessageBox.Show("The image is larger than your selected device", "Error", MessageBoxButtons.OK);
                return;
            }

            if (MessageBox.Show("This will overwrite everything on your disk.  Are you sure you want to proceed?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            copyWorker = new BackgroundWorker();
            progressDialog = new ProgressDialog();
            progressDialog.cancelButton.Click += new System.EventHandler(this.ProgressCancelButtonClicked);
            progressDialog.ProgressToWrite = (int) (imageSize / 1048576);
            copyWorker.WorkerReportsProgress = true;
            copyWorker.DoWork += new DoWorkEventHandler(WorkerWork);
            copyWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkerDone);
            copyWorker.ProgressChanged += new ProgressChangedEventHandler(WorkerProgressChanged);
            copyWorker.WorkerSupportsCancellation = true;
            copyWorker.RunWorkerAsync();
            progressDialog.ShowDialog();
        }