msdn :Win32_Processor,CIM_OperatingSystem,Win32_LogicalMemoryConfiguration  主要是前两个 MSDN中可以查看相关的属性


protected string[] GetServerCPUAndMem(string ip, string userName,string password)
    {
        ConnectionOptions conOptions = new ConnectionOptions();
        conOptions.Username = userName;
        conOptions.Password = password;

        string path = "\\\\"+ip+"\\root\\cimv2";
        ManagementScope ms = new ManagementScope(path);
        ms.Options = conOptions;
        ms.Connect();


        string cpuPercent = "";//cpu序列号 \\192.0.0.184\root\cimv2:Win32_Processor


        ManagementClass cimobject = new ManagementClass(ms, new ManagementPath(path+":Win32_Processor"), null);
        ManagementObjectCollection moc = cimobject.GetInstances();
        foreach (ManagementObject mo in moc)
        {
            cpuPercent = mo.Properties["LoadPercentage"].Value.ToString();
        }
        cpuPercent = cpuPercent + "%";


        ObjectQuery oq = new ObjectQuery("Select * from CIM_OperatingSystem");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(ms, oq);
        UInt64 TotalVisibleMemorySize = 0 ;
        UInt64 FreePhysicalMemory=0;
        foreach (ManagementObject queryObj in searcher.Get())
        {
            TotalVisibleMemorySize = (UInt64)queryObj["TotalVisibleMemorySize"];
            FreePhysicalMemory = (UInt64)queryObj["FreePhysicalMemory"];        
        }
        decimal di = Convert.ToDecimal(TotalVisibleMemorySize-FreePhysicalMemory) / TotalVisibleMemorySize;

        string memPercent="";
        try
        {
            memPercent = Convert.ToString(di * 100).Split('.')[0];
        }
        catch
        {
            memPercent = Convert.ToString(di * 100);
        }
        memPercent = memPercent + "%";

        string[] result=new string[2];
        result[0]=cpuPercent;
        result[1]=memPercent;

      
        return result;
      
    }

Posted on 2008-06-30 18:33  hesen  阅读(753)  评论(0编辑  收藏  举报