Ejemplo n.º 1
0
        List <DiskRecord> QueryAllDisks()
        {
            List <DiskRecord> disks = new List <DiskRecord>();

            try
            {
                using (var devenum = new DeviceEnumerator(GUID_DEVINTERFACE_DISK))
                    foreach (var info in devenum.GetAllDevices())
                    {
                        long volumeSize = 0;
                        DiskDevice.STORAGE_HOTPLUG_INFO      hotplug = null;
                        DiskDevice.STORAGE_DEVICE_DESCRIPTOR desc    = null;
                        try
                        {
                            using (var dev = new DiskDevice(info.DevicePath))
                            {
                                try
                                {
                                    hotplug = dev.QueryHotplugInfo();
                                }
                                catch (Exception ex)
                                {
                                }

                                try
                                {
                                    desc = dev.QueryDeviceDescriptor();
                                }
                                catch
                                {
                                }

                                byte[] result = new byte[8];
                                try
                                {
                                    if (dev.DeviceIoControl(IOCTL_DISK_GET_LENGTH_INFO, null, result) == 8)
                                    {
                                        volumeSize = BitConverter.ToInt64(result, 0);
                                    }
                                }
                                catch
                                {
                                }
                            }

                            disks.Add(new DiskRecord {
                                Descriptor = desc, Hotplug = hotplug, Info = info, VolumeSize = volumeSize
                            });
                        }
                        catch { }
                    }
            }
            catch
            {
            }
            return(disks);
        }
Ejemplo n.º 2
0
        private void button3_Click(object sender, EventArgs e)
        {
            DiskDevice dev = null;
            FileStream fs  = null;

            try
            {
                if (!File.Exists(txtFileName.Text))
                {
                    throw new Exception("Please select a valid image file");
                }

                if (lvDevices.SelectedItems.Count == 0)
                {
                    throw new Exception("Please select target device");
                }

                var rec  = lvDevices.SelectedItems[0].Tag as DiskRecord;
                var info = rec?.Info;
                if (info == null)
                {
                    throw new Exception("Please select a valid target device");
                }

                string devPath = info.DevicePath;

                dev = new DiskDevice(devPath);
                long   volumeSize = 0;
                byte[] result     = new byte[8];
                if (dev.DeviceIoControl(IOCTL_DISK_GET_LENGTH_INFO, null, result) == 8)
                {
                    volumeSize = BitConverter.ToInt64(result, 0);
                }
                if (volumeSize <= 0)
                {
                    throw new Exception("Please insert a card into the card reader");
                }

                fs = File.Open(txtFileName.Text, FileMode.Open, FileAccess.Read, FileShare.Read);

                if (fs.Length > volumeSize)
                {
                    throw new Exception(string.Format("The selected media ({0}) is too small for the selected image file {1})", StringHelpers.FormatByteCount(volumeSize), StringHelpers.FormatByteCount(fs.Length)));
                }

                _RegHelper.SetValue("LastImageFile", txtFileName.Text);

                var number = dev.QueryDeviceNumber();

                if (new EraseConfirmationForm(info, dev, volumeSize).ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                lblProgress.Text = "Preparing...";
                GUIEnabled       = false;
                _AbortWriting    = false;
                var ctx = new ThreadContext {
                    VolumeSize = volumeSize, DeviceNumber = number, fs = fs, devID = info.DeviceID, FileName = txtFileName.Text
                };
                if (cbResize.Checked)
                {
                    try
                    {
                        var pt = ParsedChangeFile.ReadPartitionTable(ctx.FileName);
                        if (pt != null && pt.Length > 0 && pt[pt.Length - 1].Type == 0x83)
                        {
                            ctx.ResizedPartition = pt[pt.Length - 1];
                        }
                    }
                    catch { }
                }

                new Thread(WriteThreadBody).Start(ctx);
                fs  = null;
                dev = null;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                fs?.Dispose();
                dev?.Dispose();
            }
        }