如何按C#获取未标记卷驱动器的总大小?
我正在用C#调查windows驱动器如何按C#获取未标记卷驱动器的总大小?,c#,windows,diskspace,disk-partitioning,driveinfo,C#,Windows,Diskspace,Disk Partitioning,Driveinfo,我正在用C#调查windows驱动器 如何使用原始分区获得卷的大小?首先要有一些东西来表示卷: public class Volume { public Volume(string path) { Path = path; ulong freeBytesAvail, totalBytes, totalFreeBytes; if (GetDiskFreeSpaceEx(path, out freeBytesAvail, out tot
如何使用原始分区获得卷的大小?首先要有一些东西来表示卷:
public class Volume
{
public Volume(string path)
{
Path = path;
ulong freeBytesAvail, totalBytes, totalFreeBytes;
if (GetDiskFreeSpaceEx(path, out freeBytesAvail, out totalBytes, out totalFreeBytes))
{
FreeBytesAvailable = freeBytesAvail;
TotalNumberOfBytes = totalBytes;
TotalNumberOfFreeBytes = totalFreeBytes;
}
}
public string Path { get; private set; }
public ulong FreeBytesAvailable { get; private set; }
public ulong TotalNumberOfBytes { get; private set; }
public ulong TotalNumberOfFreeBytes { get; private set; }
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GetDiskFreeSpaceEx([MarshalAs(UnmanagedType.LPStr)]string volumeName, out ulong freeBytesAvail,
out ulong totalBytes, out ulong totalFreeBytes);
}
下面是一个简单的卷枚举器:
public class VolumeEnumerator : IEnumerable<Volume>
{
public IEnumerator<Volume> GetEnumerator()
{
StringBuilder sb = new StringBuilder(2048);
IntPtr volumeHandle = FindFirstVolume(sb, (uint)sb.MaxCapacity);
{
if (volumeHandle == IntPtr.Zero)
yield break;
else
{
do
{
yield return new Volume(sb.ToString());
sb.Clear();
}
while (FindNextVolume(volumeHandle, sb, (uint)sb.MaxCapacity));
FindVolumeClose(volumeHandle);
}
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr FindFirstVolume([Out] StringBuilder lpszVolumeName,
uint cchBufferLength);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool FindNextVolume(IntPtr hFindVolume, [Out] StringBuilder lpszVolumeName, uint cchBufferLength);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool FindVolumeClose(IntPtr hFindVolume);
}
这都是从将p/Invokes构建到。如果这不是您想要的,您可能会在那里找到特定的信息。您可以使用WMI来获取信息:谢谢,但它不起作用。它向我显示有关ntfs的正确信息,但对于所有原始分区,它向我显示“可用的空闲字节0总字节0”。@user436862,因为对于未分配的分区,没有可用的字节。
foreach (Volume v in new VolumeEnumerator())
{
Console.WriteLine("{0}, Free bytes available {1} Total Bytes {2}", v.Path,
v.FreeBytesAvailable, v.TotalNumberOfBytes);
}