// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.IO; using System.Runtime.InteropServices; using System.Text; namespace E { [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct NTFS_VOLUME_DATA_BUFFER { public long VolumeSerialNumber; public long NumberSectors; public long TotalClusters; public long FreeClusters; public long TotalReserved; public uint BytesPerSector; public uint BytesPerCluster; public uint BytesPerFileRecordSegment; public uint ClustersPerFileRecordSegment; public long MftValidDataLength; public long MftStartLcn; public long Mft2StartLcn; public long MftZoneStart; public long MftZoneEnd; } public class Program { public const int FSCTL_GET_NTFS_VOLUME_DATA = 589924; [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern IntPtr CreateFile( string fileName, [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess, [MarshalAs(UnmanagedType.U4)] FileShare fileShare, IntPtr securityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, int flags, IntPtr template); [DllImport("kernel32.dll", SetLastError=true)] static extern bool CloseHandle(IntPtr hHandle); [DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)] public static extern bool DeviceIoControl( IntPtr hDevice, uint dwIoControlCode, IntPtr lpInBuffer, int nInBufferSize, out NTFS_VOLUME_DATA_BUFFER lpOutBuffer, int nOutBufferSize, out int pBytesReturned, IntPtr lpOverlapped); public static void Main(string[] args) { NTFS_VOLUME_DATA_BUFFER data = new NTFS_VOLUME_DATA_BUFFER(); int datalen; IntPtr h = CreateFile(@"\\.\C:", FileAccess.Read, FileShare.Write, IntPtr.Zero, FileMode.Open, 0, IntPtr.Zero); if(!DeviceIoControl(h, FSCTL_GET_NTFS_VOLUME_DATA, IntPtr.Zero, 0, out data, Marshal.SizeOf(data), out datalen, IntPtr.Zero)) { throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); } CloseHandle(h); Console.WriteLine(data.BytesPerCluster); } } }