Beispiel #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);
        }
Beispiel #2
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;
        }
Beispiel #3
0
        void WriteThreadBody(object obj)
        {
            ThreadContext ctx = (ThreadContext)obj;

            UpdateProgress("Erasing partition table...", 0, 0);
            try
            {
                //Erase
                ctx.dev.SeekAbs(0);
                ctx.dev.Write(new byte[SectorSize]);
                ctx.dev.Dispose();
                ctx.dev = null;

                string devPath = null;

                using (var devenum = new DeviceEnumerator(GUID_DEVINTERFACE_DISK))
                    foreach (var info in devenum.GetAllDevices())
                    {
                        if (info.DeviceID == ctx.devID)
                        {
                            devPath = info.DevicePath;
                            if (!info.ChangeDeviceState(DeviceEnumerator.DICS.DICS_DISABLE))
                                throw new Exception("Cannot reset the card reader. Please remove the card, put it back and retry.");
                            if (!info.ChangeDeviceState(DeviceEnumerator.DICS.DICS_ENABLE))
                                throw new Exception("Cannot re-enable the card reader. Please enable it manually using device manager.");
                        }
                    }

                if (devPath == null)
                    throw new Exception("Cannot reset the card reader. Please remove the card, put it back and retry.");

                for (; ; )
                {
                    if (AttemptWrite(ctx, devPath))
                        break;
                }

                ReportCompletion(null);
            }
            catch (System.Exception ex)
            {
                ReportCompletion(ex);
            }
            finally
            {
                if (ctx.fs != null)
                    ctx.fs.Dispose();
                if (ctx.dev != null)
                    ctx.dev.Dispose();
            }
        }
Beispiel #4
0
        void WriteThreadBody(object obj)
        {
            ThreadContext ctx = (ThreadContext)obj;

            UpdateProgress("Erasing partition table...", 0, 0);
            try
            {
                //Erase
                ctx.dev.SeekAbs(0);
                ctx.dev.Write(new byte[SectorSize]);
                ctx.dev.Dispose();
                ctx.dev = null;

                string devPath = null;

                using (var devenum = new DeviceEnumerator(GUID_DEVINTERFACE_DISK))
                    foreach (var info in devenum.GetAllDevices())
                    {
                        if (info.DeviceID == ctx.devID)
                        {
                            devPath = info.DevicePath;
                            if (!info.ChangeDeviceState(DeviceEnumerator.DICS.DICS_DISABLE))
                            {
                                throw new Exception("Cannot reset the card reader. Please remove the card, put it back and retry.");
                            }
                            if (!info.ChangeDeviceState(DeviceEnumerator.DICS.DICS_ENABLE))
                            {
                                throw new Exception("Cannot re-enable the card reader. Please enable it manually using device manager.");
                            }
                        }
                    }

                if (devPath == null)
                {
                    throw new Exception("Cannot reset the card reader. Please remove the card, put it back and retry.");
                }

                for (; ;)
                {
                    if (AttemptWrite(ctx, devPath))
                    {
                        break;
                    }
                }

                ReportCompletion(null);
            }
            catch (System.Exception ex)
            {
                ReportCompletion(ex);
            }
            finally
            {
                if (ctx.fs != null)
                {
                    ctx.fs.Dispose();
                }
                if (ctx.dev != null)
                {
                    ctx.dev.Dispose();
                }
            }
        }