【问题标题】:Get Operating system,disk space of connected computers windows获取操作系统、已连接计算机的磁盘空间 windows
【发布时间】:2018-10-25 04:58:10
【问题描述】:

使用 c# 我想将所连接机器的操作系统名称、磁盘空间、CPU 使用率获取到我的服务器。 我可以使用以下代码获取连接的机器名称

foreach (DirectoryEntry computers in root.Children)
{
   foreach (DirectoryEntry computer in computers.Children)
   {                 
     if (computer.Name != "Schema")
     {                           
       string value = computer.Name;                    
     }
   }
}

但不知道要获取操作系统名称磁盘空间CPU使用率连接的机器

【问题讨论】:

标签: c# .net windows cpu-usage diskspace


【解决方案1】:

操作系统名称

添加对 Microsoft.VisualBasic 的 .NET 引用。然后调用:

new Microsoft.VisualBasic.Devices.ComputerInfo().OSFullName

来自MSDN

此属性返回有关操作系统的详细信息 如果 Windows Management Instrumentation (WMI) 安装在 电脑。否则,此属性返回与 My.Computer.Info.OSPlatform 属性,提供的细节较少 WMI 无法提供的信息。WMI 无法提供的信息。

磁盘空间

    private  long GetTotalFreeSpace(string driveName)
    {
        foreach (DriveInfo drive in DriveInfo.GetDrives())
        {
            if (drive.IsReady && drive.Name == driveName)
            {
                return drive.TotalSize;
            }
        }
        return -1;
    }

CPU 使用率

您可以使用System.Diagnostics 中的PerformanceCounter 类。

像这样初始化:

PerformanceCounter cpuCounter;

cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");

这样吃:

public string getCurrentCpuUsage(){
            return cpuCounter.NextValue()+"%";
}

参考文献

How to get OS name?

How to get disk space?

How to get CPU usage?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 2019-11-01
    • 2011-02-19
    相关资源
    最近更新 更多