// // CREATE_VIRTUAL_DISK_VERSION_2 allows specifying a richer set a values and returns // a V2 handle. // // VIRTUAL_DISK_ACCESS_NONE is the only acceptable access mask for V2 handle opens. // // Valid BlockSize values are as follows (use 0 to indicate default value): // Fixed VHD: 0 // Dynamic VHD: 512kb, 2mb (default) // Differencing VHD: 512kb, 2mb (if parent is fixed, default is 2mb; if parent is dynamic or differencing, default is parent blocksize) // Fixed VHDX: 0 // Dynamic VHDX: 1mb, 2mb, 4mb, 8mb, 16mb, 32mb (default), 64mb, 128mb, 256mb // Differencing VHDX: 1mb, 2mb (default), 4mb, 8mb, 16mb, 32mb, 64mb, 128mb, 256mb // // Valid LogicalSectorSize values are as follows (use 0 to indicate default value): // VHD: 512 (default) // VHDX: 512 (for fixed or dynamic, default is 512; for differencing, default is parent logicalsectorsize), 4096 // // Valid PhysicalSectorSize values are as follows (use 0 to indicate default value): // VHD: 512 (default) // VHDX: 512, 4096 (for fixed or dynamic, default is 4096; for differencing, default is parent physicalsectorsize) // public static VirtualHardDisk CreateDisk( string filePath, ulong maximumSize, IntPtr overlapped, bool overwrite = false, string source = null, int blockSizeInBytes = 0, int sectorSizeInBytes = 0, int physicalSectorSizeInBytes = 0, CREATE_VIRTUAL_DISK_FLAG flags = CREATE_VIRTUAL_DISK_FLAG.CREATE_VIRTUAL_DISK_FLAG_NONE) { if (overwrite && File.Exists(filePath)) { File.Delete(filePath); } var createParams = new NativeMethods.CREATE_VIRTUAL_DISK_PARAMETERS_VERSION2(); createParams.Version = NativeMethods.CREATE_VIRTUAL_DISK_VERSION.CREATE_VIRTUAL_DISK_VERSION_2; //createParams.UniqueId = Guid.NewGuid(); createParams.MaximumSize = maximumSize; createParams.BlockSizeInBytes = blockSizeInBytes; createParams.SectorSizeInBytes = sectorSizeInBytes; createParams.PhysicalSectorSizeInBytes = physicalSectorSizeInBytes; createParams.ParentPath = null; createParams.SourcePath = source; createParams.OpenFlags = OPEN_VIRTUAL_DISK_FLAG.OPEN_VIRTUAL_DISK_FLAG_NONE; //createParams.ParentVirtualStorageType = new NativeMethods.VIRTUAL_STORAGE_TYPE(); //createParams.SourceVirtualStorageType = new NativeMethods.VIRTUAL_STORAGE_TYPE(); if (!NativeMethods.InitializeSecurityDescriptor(out var securityDescriptor, 1)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } var virtualStorageType = new NativeMethods.VIRTUAL_STORAGE_TYPE(); virtualStorageType.DeviceId = VIRTUAL_STORAGE_TYPE_DEVICE.VIRTUAL_STORAGE_TYPE_DEVICE_VHDX; int ret = NativeMethods.CreateVirtualDisk( ref virtualStorageType, filePath, VIRTUAL_DISK_ACCESS_MASK.VIRTUAL_DISK_ACCESS_NONE, ref securityDescriptor, flags, 0, ref createParams, overlapped, out var handle); if (NativeMethods.ERROR_SUCCESS != ret && NativeMethods.ERROR_IO_PENDING != ret) { throw new Win32Exception(ret); } return(new VirtualHardDisk(handle, filePath)); }
public static extern int CreateVirtualDisk( ref VIRTUAL_STORAGE_TYPE VirtualStorageType, [MarshalAs(UnmanagedType.LPWStr)] string Path, VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask, ref SECURITY_DESCRIPTOR SecurityDescriptor, CREATE_VIRTUAL_DISK_FLAG Flags, int ProviderSpecificFlags, ref CREATE_VIRTUAL_DISK_PARAMETERS_VERSION2 Parameters, IntPtr Overlapped, out IntPtr Handle);
public static extern Int32 CreateVirtualDisk(ref VIRTUAL_STORAGE_TYPE VirtualStorageType, String Path, VIRTUAL_DISK_ACCESS_MASK VirtualDiskAccessMask, IntPtr SecurityDescriptor, CREATE_VIRTUAL_DISK_FLAG Flags, Int32 ProviderSpecificFlags, ref CREATE_VIRTUAL_DISK_PARAMETERS Parameters, ref NativeOverlapped Overlapped, ref VirtualDiskSafeHandle Handle);