C# 如何获取磁盘剩余空间
2010-04-29 15:33
435 查看
using System;
using System.Management;
...
ManagementObject disk = new
ManagementObject("win32_logicaldisk.deviceid="c:"");
disk.Get();
Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes");
Console.WriteLine("Logical Disk FreeSpace = " + disk["FreeSpace"] + "
bytes");
Drive Free Space
There are several ways to get the drive free space:
1. The interop way.
using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
public static extern bool GetDiskFreeSpaceEx(
string lpDirectoryName,
out UInt64 lpFreeBytesAvailable,
out UInt64 lpTotalNumberOfBytes,
out UInt64 lpTotalNumberOfFreeBytes);
ulong freeBytesAvailable = 0;
ulong totalNumberOfBytes = 0;
ulong totalNumberOfFreeBytes = 0;
GetDiskFreeSpaceEx(
"c:\\",
out freeBytesAvailable,
out totalNumberOfBytes,
out totalNumberOfFreeBytes);
2. The WMI Way
We can use WMI by connecting to the “root\\cimv2” namespace and using the “Win32_LogicalDisk” class. Programming WMI isn’t pretty, but you can use the WMI extensions for VS ‘03 Server Explorer which makes it tolerable. Once installed, you’ll get a list of the management classes in server explorer and you can simply drag and drop the disk volume that you want to pull information from onto your application designer.
You can then show free space on the volume with one line of code: MessageBox.Show(logicalDisk1.FreeSpace.ToString());
using System;
using System.Management;
tatic void Main(string[] args)
{
WqlObjectQuery wmiquery = new WqlObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DeviceID = 'C:'");
ManagementObjectSearcher wmifind = new ManagementObjectSearcher(wmiquery);
foreach (ManagementObject mobj in wmifind.Get())
{
Console.WriteLine("Description: " + mobj["Description"]);
Console.WriteLine("File system: " + mobj["FileSystem"]);
Console.WriteLine("Free disk space: " + mobj["FreeSpace"]);
Console.WriteLine("Size: " + mobj["Size"]);
}
}
using System.Management;
...
ManagementObject disk = new
ManagementObject("win32_logicaldisk.deviceid="c:"");
disk.Get();
Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes");
Console.WriteLine("Logical Disk FreeSpace = " + disk["FreeSpace"] + "
bytes");
Drive Free Space
There are several ways to get the drive free space:
1. The interop way.
using System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
public static extern bool GetDiskFreeSpaceEx(
string lpDirectoryName,
out UInt64 lpFreeBytesAvailable,
out UInt64 lpTotalNumberOfBytes,
out UInt64 lpTotalNumberOfFreeBytes);
ulong freeBytesAvailable = 0;
ulong totalNumberOfBytes = 0;
ulong totalNumberOfFreeBytes = 0;
GetDiskFreeSpaceEx(
"c:\\",
out freeBytesAvailable,
out totalNumberOfBytes,
out totalNumberOfFreeBytes);
2. The WMI Way
We can use WMI by connecting to the “root\\cimv2” namespace and using the “Win32_LogicalDisk” class. Programming WMI isn’t pretty, but you can use the WMI extensions for VS ‘03 Server Explorer which makes it tolerable. Once installed, you’ll get a list of the management classes in server explorer and you can simply drag and drop the disk volume that you want to pull information from onto your application designer.
You can then show free space on the volume with one line of code: MessageBox.Show(logicalDisk1.FreeSpace.ToString());
using System;
using System.Management;
tatic void Main(string[] args)
{
WqlObjectQuery wmiquery = new WqlObjectQuery("SELECT * FROM Win32_LogicalDisk WHERE DeviceID = 'C:'");
ManagementObjectSearcher wmifind = new ManagementObjectSearcher(wmiquery);
foreach (ManagementObject mobj in wmifind.Get())
{
Console.WriteLine("Description: " + mobj["Description"]);
Console.WriteLine("File system: " + mobj["FileSystem"]);
Console.WriteLine("Free disk space: " + mobj["FreeSpace"]);
Console.WriteLine("Size: " + mobj["Size"]);
}
}
相关文章推荐
- C#如何获取远程磁盘上的剩余空间
- C# 获取磁盘剩余空间
- 如何获取磁盘剩余空间(包括光驱中的光盘)
- C# 获取磁盘空间信息
- [脚本收集]获取磁盘剩余空间
- linux如何查看磁盘剩余空间
- linux如何查看磁盘剩余空间
- QT获取磁盘剩余空间大小
- 获取 iPhone 剩余磁盘空间和全部空间的代码
- C# 获取磁盘的空间大小
- 如何安全地解放C盘剩余磁盘空间?
- 获取电脑磁盘的剩余空间
- Qt linux获取cpu使用率、内存、网络收发速度、磁盘读写速度、磁盘剩余空间等
- 根据文件路径获取剩余磁盘空间
- 获取磁盘剩余空间 Django项目几个主要文件功能的解释
- 获取磁盘剩余空间
- C# 获取磁盘空间或者可用空间
- C# 获取磁盘空间信息
- C#实现获取磁盘空间大小的方法
- 在Windows下通过JConfig获取磁盘剩余空间信息(续)