C# 如何检查可用磁盘空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1412395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check for available disk space?
提问by Bryan Denny
I need a way to check available disk space on a remoteWindows server before copying files to that server. Using this method I can check to see if the primary server is full and if it is, then I'll copy the files to a secondary server.
在将文件复制到该服务器之前,我需要一种方法来检查远程Windows 服务器上的可用磁盘空间。使用这种方法,我可以检查主服务器是否已满,如果已满,我会将文件复制到辅助服务器。
How can I check for available disk space using C#/ASP.net 2.0?
如何使用 C#/ASP.net 2.0 检查可用磁盘空间?
回答by Cleiton
You can use WMI, see this related question:
您可以使用 WMI,请参阅此相关问题:
回答by George Stocker
You can check it by doing the following:
您可以通过执行以下操作来检查它:
Add the System.Management.dll
as a reference to your project.
添加System.Management.dll
作为对您的项目的引用。
Use the following code to get the diskspace:
使用以下代码获取磁盘空间:
using System;
using System.Management;
public string GetFreeSpace();
{
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
disk.Get();
string freespace = disk["FreeSpace"];
return freespace;
}
There are a myriad of ways to do it, I'd check the System.Managementnamespace for more ways.
有无数种方法可以做到这一点,我会检查System.Management命名空间以获取更多方法。
Here's one such way from that page:
这是该页面的一种方式:
public void GetDiskspace()
{
ConnectionOptions options = new ConnectionOptions();
ManagementScope scope = new ManagementScope("\\localhost\root\cimv2",
options);
scope.Connect();
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
SelectQuery query1 = new SelectQuery("Select * from Win32_LogicalDisk");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(scope, query1);
ManagementObjectCollection queryCollection1 = searcher1.Get();
foreach (ManagementObject m in queryCollection)
{
// Display the remote computer information
Console.WriteLine("Computer Name : {0}", m["csname"]);
Console.WriteLine("Windows Directory : {0}", m["WindowsDirectory"]);
Console.WriteLine("Operating System: {0}", m["Caption"]);
Console.WriteLine("Version: {0}", m["Version"]);
Console.WriteLine("Manufacturer : {0}", m["Manufacturer"]);
Console.WriteLine();
}
foreach (ManagementObject mo in queryCollection1)
{
// Display Logical Disks information
Console.WriteLine(" Disk Name : {0}", mo["Name"]);
Console.WriteLine(" Disk Size : {0}", mo["Size"]);
Console.WriteLine(" FreeSpace : {0}", mo["FreeSpace"]);
Console.WriteLine(" Disk DeviceID : {0}", mo["DeviceID"]);
Console.WriteLine(" Disk VolumeName : {0}", mo["VolumeName"]);
Console.WriteLine(" Disk SystemName : {0}", mo["SystemName"]);
Console.WriteLine("Disk VolumeSerialNumber : {0}", mo["VolumeSerialNumber"]);
Console.WriteLine();
}
string line;
line = Console.ReadLine();
}
回答by Jason Irwin
You can use the DriveInfo class
您可以使用 DriveInfo 类
DriveInfo[] oDrvs = DriveInfo.GetDrives();
foreach (var Drv in oDrvs) {
if (Drv.IsReady) {
Console.WriteLine(Drv.Name + " " + Drv.AvailableFreeSpace.ToString);
}
}
回答by Bryan Denny
This seems to be an option from the System.IO:
这似乎是 System.IO 的一个选项:
DriveInfo c = new DriveInfo("C");
long cAvailableSpace = c.AvailableFreeSpace;
回答by anishMarokey
by using this code
通过使用此代码
static void Main()
{
try
{
DriveInfo driveInfo = new DriveInfo(@"C:");
long FreeSpace = driveInfo.AvailableFreeSpace;
}
catch (System.IO.IOException errorMesage)
{
Console.WriteLine(errorMesage);
}
}
IF you are getting the error 'The device is not ready' .i.e your device is not ready . If you are trying this code for a CD drive without CD you will get the same error : )
如果您收到错误“设备未准备好”。即您的设备未准备好。如果您在没有 CD 的 CD 驱动器上尝试此代码,您将收到相同的错误:)