コード例 #1
0
ファイル: DiskIO.cs プロジェクト: vaginessa/VhdAttach
        public static void InitializeDisk(string path)
        {
            using (SafeFileHandle handle = NativeMethods.CreateFile(path, NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 0, IntPtr.Zero, NativeMethods.OPEN_EXISTING, 0, IntPtr.Zero)) {
                if (handle.IsInvalid)
                {
                    throw new Win32Exception();
                }

                var signature = new byte[4];
                RandomNumberGenerator.Create().GetBytes(signature);

                Int32 bytesOut = 0;

                var cd = new NativeMethods.CREATE_DISK();
                cd.PartitionStyle = NativeMethods.PARTITION_STYLE.PARTITION_STYLE_MBR;
                cd.Mbr.Signature  = BitConverter.ToInt32(signature, 0);
                if (NativeMethods.DeviceIoControl(handle, NativeMethods.IOCTL_DISK_CREATE_DISK, ref cd, Marshal.SizeOf(cd), IntPtr.Zero, 0, ref bytesOut, IntPtr.Zero) == false)
                {
                    throw new Win32Exception();
                }

                if (NativeMethods.DeviceIoControl(handle, NativeMethods.IOCTL_DISK_UPDATE_PROPERTIES, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesOut, IntPtr.Zero) == false)
                {
                    throw new Win32Exception();
                }                                                                                                                                                                                          //just update cache

                var pi = new NativeMethods.PARTITION_INFORMATION();
                if (NativeMethods.DeviceIoControl(handle, NativeMethods.IOCTL_DISK_GET_PARTITION_INFO, IntPtr.Zero, 0, ref pi, Marshal.SizeOf(pi), ref bytesOut, IntPtr.Zero) == false)
                {
                    throw new Win32Exception();
                }

                var dli = new NativeMethods.DRIVE_LAYOUT_INFORMATION_EX();
                dli.PartitionStyle                     = NativeMethods.PARTITION_STYLE.PARTITION_STYLE_MBR;
                dli.PartitionCount                     = 1;
                dli.Partition1.PartitionStyle          = NativeMethods.PARTITION_STYLE.PARTITION_STYLE_MBR;
                dli.Partition1.StartingOffset          = 65536;
                dli.Partition1.PartitionLength         = pi.PartitionLength - dli.Partition1.StartingOffset;
                dli.Partition1.PartitionNumber         = 1;
                dli.Partition1.RewritePartition        = true;
                dli.Partition1.Mbr.PartitionType       = NativeMethods.PARTITION_IFS;
                dli.Partition1.Mbr.BootIndicator       = true;
                dli.Partition1.Mbr.RecognizedPartition = true;
                dli.Partition1.Mbr.HiddenSectors       = 0;
                dli.Mbr.Signature = BitConverter.ToInt32(signature, 0);
                if (NativeMethods.DeviceIoControl(handle, NativeMethods.IOCTL_DISK_SET_DRIVE_LAYOUT_EX, ref dli, Marshal.SizeOf(dli), IntPtr.Zero, 0, ref bytesOut, IntPtr.Zero) == false)
                {
                    throw new Win32Exception();
                }

                if (NativeMethods.DeviceIoControl(handle, NativeMethods.IOCTL_DISK_UPDATE_PROPERTIES, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesOut, IntPtr.Zero) == false)
                {
                    throw new Win32Exception();
                }                                                                                                                                                                                          //just update cache
            }
        }
コード例 #2
0
ファイル: DiskIO.cs プロジェクト: WORawSon/VhdAttach
        public static void InitializeDisk(string path)
        {
            using (SafeFileHandle handle = NativeMethods.CreateFile(path, NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 0, IntPtr.Zero, NativeMethods.OPEN_EXISTING, 0, IntPtr.Zero)) {
                if (handle.IsInvalid) { throw new Win32Exception(); }

                var signature = new byte[4];
                RandomNumberGenerator.Create().GetBytes(signature);

                Int32 bytesOut = 0;

                var cd = new NativeMethods.CREATE_DISK();
                cd.PartitionStyle = NativeMethods.PARTITION_STYLE.PARTITION_STYLE_MBR;
                cd.Mbr.Signature = BitConverter.ToInt32(signature, 0);
                if (NativeMethods.DeviceIoControl(handle, NativeMethods.IOCTL_DISK_CREATE_DISK, ref cd, Marshal.SizeOf(cd), IntPtr.Zero, 0, ref bytesOut, IntPtr.Zero) == false) { throw new Win32Exception(); }

                if (NativeMethods.DeviceIoControl(handle, NativeMethods.IOCTL_DISK_UPDATE_PROPERTIES, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesOut, IntPtr.Zero) == false) { throw new Win32Exception(); } //just update cache

                var pi = new NativeMethods.PARTITION_INFORMATION();
                if (NativeMethods.DeviceIoControl(handle, NativeMethods.IOCTL_DISK_GET_PARTITION_INFO, IntPtr.Zero, 0, ref pi, Marshal.SizeOf(pi), ref bytesOut, IntPtr.Zero) == false) { throw new Win32Exception(); }

                var dli = new NativeMethods.DRIVE_LAYOUT_INFORMATION_EX();
                dli.PartitionStyle = NativeMethods.PARTITION_STYLE.PARTITION_STYLE_MBR;
                dli.PartitionCount = 1;
                dli.Partition1.PartitionStyle = NativeMethods.PARTITION_STYLE.PARTITION_STYLE_MBR;
                dli.Partition1.StartingOffset = 65536;
                dli.Partition1.PartitionLength = pi.PartitionLength - dli.Partition1.StartingOffset;
                dli.Partition1.PartitionNumber = 1;
                dli.Partition1.RewritePartition = true;
                dli.Partition1.Mbr.PartitionType = NativeMethods.PARTITION_IFS;
                dli.Partition1.Mbr.BootIndicator = true;
                dli.Partition1.Mbr.RecognizedPartition = true;
                dli.Partition1.Mbr.HiddenSectors = 0;
                dli.Mbr.Signature = BitConverter.ToInt32(signature, 0);
                if (NativeMethods.DeviceIoControl(handle, NativeMethods.IOCTL_DISK_SET_DRIVE_LAYOUT_EX, ref dli, Marshal.SizeOf(dli), IntPtr.Zero, 0, ref bytesOut, IntPtr.Zero) == false) { throw new Win32Exception(); }

                if (NativeMethods.DeviceIoControl(handle, NativeMethods.IOCTL_DISK_UPDATE_PROPERTIES, IntPtr.Zero, 0, IntPtr.Zero, 0, ref bytesOut, IntPtr.Zero) == false) { throw new Win32Exception(); } //just update cache
            }
        }