private static DiskGeometry GetDiskGeometry(DriveLayout.FileSafeHandle handle) { var name = "\\\\.\\C:"; // var handle = DriveLayout.NativeMethods.CreateFile(name, DriveLayout.NativeMethods.AccessRights.GENERIC_READ | DriveLayout.NativeMethods.AccessRights.GENERIC_WRITE, FileShare.Read | FileShare.Write, IntPtr.Zero, DriveLayout.NativeMethods.FileCreationDisposition.OPEN_EXISTING, FileAttributes.Normal, IntPtr.Zero) int geometrySize = Marshal.SizeOf(typeof(DiskGeometry)); //Console.WriteLine("geometry size = {0}", geometrySize); IntPtr geometryBlob = Marshal.AllocHGlobal(geometrySize); uint numBytesRead = 0; DriveLayout.NativeMethods.DeviceIoControl( handle, DriveLayout.NativeMethods.IoControlCode.IoCtlDiskGetDriveGeometry, IntPtr.Zero, 0, geometryBlob, (uint)geometrySize, ref numBytesRead, IntPtr.Zero ); DiskGeometry geometry = (DiskGeometry)Marshal.PtrToStructure(geometryBlob, typeof(DiskGeometry)); Marshal.FreeHGlobal(geometryBlob); Console.WriteLine("Cylinders: " + geometry.Cylinders); Console.WriteLine("SectorsPerTrack: " + geometry.SectorsPerTrack); Console.WriteLine("TracksPerCylinder: " + geometry.TracksPerCylinder); Console.WriteLine("BytesPerSector: " + geometry.BytesPerSector); return(geometry); }
private static long GrowPartition(DiskGeometry geo, DriveLayout.FileSafeHandle handle) { var name = "\\\\.\\PhysicalDrive0"; int bytesReturned5; var li = GetPartition(handle, out bytesReturned5); var currentSize = li.PartitionEntry[0].PartitionLength; var targetSize = ((long)geo.TracksPerCylinder * (long)geo.SectorsPerTrack * (long)geo.BytesPerSector * geo.Cylinders) - li.PartitionEntry[0].StartingOffset; Console.WriteLine("Current Size= " + currentSize); Console.WriteLine("Target Size=" + targetSize); var expandBy = targetSize - currentSize; Console.WriteLine("Need to expand by " + expandBy); var recommendedSectors = (li.PartitionEntry[0].PartitionLength - li.PartitionEntry[0].StartingOffset) / geo.BytesPerSector; if (expandBy == 0) { Console.WriteLine("Sectors: " + (li.PartitionEntry[0].PartitionLength / geo.BytesPerSector)); Console.WriteLine("Recommended sectors = " + recommendedSectors); return(recommendedSectors); } GrowPartition gp = new GrowPartition() { BytesToGrow = expandBy, PartitionNumber = 1 }; int bytesReturned1 = 0; ExecuteNativeActionAndCheckLastError(() => { bool w = DriveLayout.NativeMethods.DeviceIoControl(handle, DriveLayout.NativeMethods.IoControlCode.DiskGrowPartition, ref gp, Marshal.SizeOf(gp), IntPtr.Zero, 0, ref bytesReturned1, IntPtr.Zero); Console.WriteLine("Grow Success: " + w); }); Console.WriteLine("Sectors: " + (li.PartitionEntry[0].PartitionLength / geo.BytesPerSector)); Console.WriteLine("Recommended sectors = " + recommendedSectors); return(recommendedSectors); }
private static void ExpandPartition(DriveLayout.FileSafeHandle handle, DiskGeometry geometry) { int bytesReturned1; var li = GetPartition(handle, out bytesReturned1); Console.WriteLine("PartCount " + li.PartitionCount); Console.WriteLine("Current Parittion 0 size " + li.PartitionEntry[0].PartitionLength); Console.WriteLine("Starting Offset: " + li.PartitionEntry[0].StartingOffset); var SizeToBe = ((long)geometry.TracksPerCylinder * (long)geometry.SectorsPerTrack * (long)geometry.BytesPerSector * geometry.Cylinders) - li.PartitionEntry[0].StartingOffset; if (SizeToBe == li.PartitionEntry[0].PartitionLength) { Console.WriteLine("Size is at maxcap"); } else { li.PartitionEntry[0].PartitionLength = SizeToBe; Console.WriteLine("Expanding Parittion 0 to size " + li.PartitionEntry[0].PartitionLength); bool outp = false; ExecuteNativeActionAndCheckLastError(() => { outp = DriveLayout.NativeMethods.DeviceIoControl( handle, DriveLayout.NativeMethods.IoControlCode.IOCTL_DISK_SET_DRIVE_LAYOUT_EX, ref li, Marshal.SizeOf(li), IntPtr.Zero, 0, ref bytesReturned1, IntPtr.Zero ); }); Console.WriteLine("Success: " + outp); } Console.WriteLine("Sectors: " + (li.PartitionEntry[0].PartitionLength / geometry.BytesPerSector)); Console.WriteLine("Recommended sectors = " + (li.PartitionEntry[0].PartitionLength - li.PartitionEntry[0].StartingOffset) / geometry.BytesPerSector); }