C#下获取计算机的系统资源信息

www.wenhui.org 11/5/2002 CSharp vs Java

磁盘状况

新建一个Windows程序,加入TreeView并添加引用System.Management;

在Form1_Load里加入下面代码:

private void Form1_Load(object sender, System.EventArgs e)
{
    const int Movable = 2;
    const int LocalDisk = 3;
    const int CD = 5;

    string type = "";
    TreeNode node = new TreeNode("我的计算机", 3, 3);
    ConnectionOptions co = new ConnectionOptions();
    co.Username = 用户名;
    co.Password = 密码;
    System.Management.ManagementScope ms = new System.Management.ManagementScope("\\\\" + stringMachineName + "\\root\\cimv2", co);
    oq = new System.Management.ObjectQuery("select * from win32_logicaldisk");
    ManagementObjectSearcher query = new ManagementObjectSearcher(ms, oq);
    ManagementObjectCollection queryCollection = query.Get();
    foreach (ManagementObject mo in queryCollection)
    {
        switch (int.Parse(mo["DriveType"].ToString()))
        {
            case Movable:
                type = "移动设备";
                break;
            case LocalDisk:
                type = "本地磁盘";
                break;
            case CD:
                type = "CD驱动器";
                break;
            default:
                break;
        }
        TreeNode node1 = new TreeNode(type + "(" + mo["Name"].ToString() + " 剩余空间:" + mo["FreeSpace"].ToString() + ")");
        node.Nodes.Add(node1);
    }
    this.treeView1.Nodes.Add(node);
}

其他的资源信息适当的改变query进行查询

原作者:痕迹
来 源:本站
共有192位读者阅读过此文
Contributors: FHL