예제 #1
0
 public static extern Boolean DeviceIoControl(
     FileSafeHandle hDevice,
     IoControlCode dwIoControlCode,
     ref GrowPartition lpInBuffer,
     int nInBufferSize,
     IntPtr lpOutBuffer,
     Int32 nOutBufferSize,
     ref Int32 lpBytesReturned,
     IntPtr lpOverlapped
     );
예제 #2
0
        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);
        }