Exemple #1
0
        internal int GetNumber()
        {
            int	bytesWritten;
            IntPtr hDisk = GetHandle();
            IntPtr buf = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Interop.STORAGE_DEVICE_NUMBER)));

            Interop.STORAGE_DEVICE_NUMBER devNum = new Interop.STORAGE_DEVICE_NUMBER();

            try
            {
                if (!Interop.DeviceIoControl(
                    hDisk,
                    Interop.IOCTL_STORAGE_GET_DEVICE_NUMBER,
                    IntPtr.Zero,
                    0,
                    buf,
                    Marshal.SizeOf(typeof(Interop.STORAGE_DEVICE_NUMBER)),
                    out bytesWritten,
                    IntPtr.Zero))
                {
                    throw new DriverException("DeviceIoControl() failed.");
                }

                Marshal.PtrToStructure(buf, devNum);
            }
            catch (ArgumentException)
            {
                throw new DriverException("Marshaling failed.");
            }
            finally
            {
                Marshal.FreeHGlobal(buf);
                Interop.CloseHandle(hDisk);
            }

            return devNum.DeviceNumber;
        }
Exemple #2
0
        internal string GetName()
        {
            int diskNumber = GetNumber();

            ManagementObjectSearcher objectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

            ManagementObjectCollection objectCollection = objectSearcher.Get();

            foreach (ManagementObject managementObject in objectCollection)
            {
                object name = managementObject.Properties["Name"].Value;

                if (name == null)
                    continue;

                object model = managementObject.Properties["Model"].Value;

                if (model == null)
                    continue;

                IntPtr hDisk = Interop.CreateFile(
                    name.ToString(),
                    Interop.GENERIC_READ | Interop.GENERIC_WRITE,
                    Interop.FILE_SHARE_READ | Interop.FILE_SHARE_WRITE,
                    IntPtr.Zero,
                    Interop.OPEN_EXISTING,
                    0,
                    IntPtr.Zero
                    );

                if (hDisk == (IntPtr)Interop.INVALID_HANDLE_VALUE)
                    continue;

                int bytesWritten;
                IntPtr buf = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Interop.STORAGE_DEVICE_NUMBER)));

                Interop.STORAGE_DEVICE_NUMBER devNum = new Interop.STORAGE_DEVICE_NUMBER();

                try
                {
                    if (!Interop.DeviceIoControl(
                        hDisk,
                        Interop.IOCTL_STORAGE_GET_DEVICE_NUMBER,
                        IntPtr.Zero,
                        0,
                        buf,
                        Marshal.SizeOf(typeof(Interop.STORAGE_DEVICE_NUMBER)),
                        out bytesWritten,
                        IntPtr.Zero))
                    {
                        Marshal.FreeHGlobal(buf);
                        Interop.CloseHandle(hDisk);

                        continue;
                    }

                    Marshal.PtrToStructure(buf, devNum);
                }
                catch (ArgumentException)
                {
                    continue;
                }
                finally
                {
                    Marshal.FreeHGlobal(buf);
                    Interop.CloseHandle(hDisk);
                }

                if (devNum.DeviceNumber == diskNumber)
                    return "Disk " + (diskNumber + 1).ToString() + " - " + model.ToString();
            }

            return _path;
        }