[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
namespace WMI
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
//ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\"");
//disk.Get();
//Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes");
//Console.ReadLine();
double free = 0;
double used = 0;
double total = 0;
string name = string.Empty;
long GB = 1024 * 1024 * 1024;
try
{
//连接远程计算机
ConnectionOptions co = new ConnectionOptions();
co.Username = "cnsn";
co.Password = "tms!QAZ2";
System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\172.26.118.220\\root\\cimv2", co);
//查询远程计算机
System.Management.ObjectQuery oq = new System.Management.ObjectQuery("SELECT * FROM Win32_LogicalDisk");
ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk");
ManagementObjectCollection queryCollection = objOSDetails.Get();
//MessageBox.Show("nihao");
foreach (ManagementObject mo in queryCollection)
{
//string[] ss = { "" };
//mo.InvokeMethod("Reboot", ss);
//Console.WriteLine(mo.ToString());
//MessageBox.Show(mo["Name"].ToString());
string deviceid = "D:";
//string deviceid = mo["Name"].ToString();
if (mo["Name"].ToString() == deviceid)
{
//MessageBox.Show("nihao");
name = "磁盘名称:" + mo["Name"].ToString();
MessageBox.Show(name);
//硬盘的可用空间
free = Convert.ToDouble(mo["FreeSpace"]) / GB;
MessageBox.Show("硬盘" + deviceid + "可用空间:" + free.ToString() + "GB");
//硬盘的已用空间
used = (Convert.ToDouble(mo["Size"]) - Convert.ToDouble(mo["FreeSpace"])) /GB;
MessageBox.Show("硬盘" + deviceid + "已用空间:" + used.ToString()+ "GB");
//硬盘的总空间
total = Convert.ToDouble(mo["Size"]) / GB;
MessageBox.Show("硬盘" + deviceid + "总空间:" + total.ToString()+"GB");
}
}
}
catch (Exception ee)
{
MessageBox.Show("异常");
}
}
}
}
|