C# 获取当前所在磁盘总量 获取当前所在磁盘使用量 获取当前所在磁盘剩余空间信息 获取当前所在磁盘当前应用程序使用量
时间: 2023-06-30 16:26:36 浏览: 74
您可以使用 System.IO.DriveInfo 类来获取关于磁盘的信息。以下是获取您所需信息的示例代码:
```csharp
// 获取当前应用程序所在的磁盘
string currentDriveName = System.IO.Path.GetPathRoot(System.Reflection.Assembly.GetExecutingAssembly().Location);
System.IO.DriveInfo currentDrive = new System.IO.DriveInfo(currentDriveName);
// 获取磁盘总量
long totalSizeInBytes = currentDrive.TotalSize;
// 获取磁盘使用量
long usedSizeInBytes = currentDrive.TotalSize - currentDrive.AvailableFreeSpace;
// 获取磁盘剩余空间信息
long freeSpaceInBytes = currentDrive.AvailableFreeSpace;
// 获取当前应用程序使用量
long currentAppSizeInBytes = 0;
foreach (var process in System.Diagnostics.Process.GetProcesses())
{
try
{
currentAppSizeInBytes += process.WorkingSet64;
}
catch (System.ComponentModel.Win32Exception)
{
// 忽略访问权限不足的进程
}
}
// 将结果输出到控制台
Console.WriteLine("Total size: {0} bytes", totalSizeInBytes);
Console.WriteLine("Used size: {0} bytes", usedSizeInBytes);
Console.WriteLine("Free space: {0} bytes", freeSpaceInBytes);
Console.WriteLine("Current app size: {0} bytes", currentAppSizeInBytes);
```