我希望能够以编程方式获取物理磁盘的大小,或者作为物理磁盘一部分的逻辑驱动器。
在正常情况下,获取逻辑驱动器大小很容易。但是,对于我使用的磁盘,我故意将原始数据写入其中,因此预计 没有已知的驱动器格式 。
由于Windows未知驱动器格式,Windows驱动器属性和.NET Framework的DriveInfo无法告诉我构成此物理磁盘的逻辑驱动器的大小。在DriveInfo foo中,按格式表示foo.DriveFormat,foo.IsReady为false,foo.TotalSize触发异常。只需右键单击驱动器并通过Windows'属性选项手动获取大小,它就会显示0字节的驱动器大小。
最好在Windows 7系统上使用C#,使用.NET Framework 4.0或更早版本,如何与物理磁盘相关联的逻辑驱动器,如何了解物理磁盘或构成物理磁盘的逻辑驱动器的大小没有已知的文件结构? 特别是物理磁盘大小对我来说是可取的。
我知道有一种方法通常使用Windows 7,因为我看到应用程序提供了相同磁盘的准确物理磁盘大小。
答案 0 :(得分:1)
解决方案最终变得非常简单,并且是现有问题Getting Disk Size Properly
的扩展在此处发布我的解决方案,因为可能完整的代码将在未来帮助某人:
(仅供参考:P / Invoke定义从http://pinvoke.net获得并最低限度修改)
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CreateFile(
[MarshalAs(UnmanagedType.LPTStr)] string filename,
[MarshalAs(UnmanagedType.U4)] FileAccess access,
[MarshalAs(UnmanagedType.U4)] FileShare share,
IntPtr securityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
IntPtr templateFile);
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool DeviceIoControl(IntPtr hDevice, uint dwIoControlCode,
IntPtr lpInBuffer, uint nInBufferSize,
IntPtr lpOutBuffer, uint nOutBufferSize,
out uint lpBytesReturned, IntPtr lpOverlapped);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool CloseHandle(IntPtr hObject);
struct GET_LENGTH_INFORMATION
{
public long Length;
};
long GetPhysDiskSize(string physDeviceID)
{
uint IOCTL_DISK_GET_LENGTH_INFO = 0x0007405C;
uint dwBytesReturned;
//Example, physDeviceID == @"\\.\PHYSICALDRIVE1"
IntPtr hVolume = CreateFile(physDeviceID, FileAccess.ReadWrite,
FileShare.None, IntPtr.Zero, FileMode.Open, FileAttributes.Normal, IntPtr.Zero);
GET_LENGTH_INFORMATION outputInfo = new GET_LENGTH_INFORMATION();
outputInfo.Length = 0;
IntPtr outBuff = Marshal.AllocHGlobal(Marshal.SizeOf(outputInfo));
bool devIOPass = DeviceIoControl(hVolume,
IOCTL_DISK_GET_LENGTH_INFO,
IntPtr.Zero, 0,
outBuff, (uint)Marshal.SizeOf(outputInfo),
out dwBytesReturned,
IntPtr.Zero);
CloseHandle(hVolume);
outputInfo = (GET_LENGTH_INFORMATION)Marshal.PtrToStructure(outBuff, typeof(GET_LENGTH_INFORMATION));
Marshal.FreeHGlobal(hVolume);
Marshal.FreeHGlobal(outBuff);
return outputInfo.Length;
}