使用VMI獲取服務器端物理內存、CPU

 ConnectionOptions options =
           new ConnectionOptions();
            options.Username = "administrator";
            options.Password = "********";
 
 
            // Make a connection to a remote computer.
            // Replace the "FullComputerName" section of the
            // string "\\\\FullComputerName\\root\\cimv2" with
            // the full computer name or IP address of the
            // remote computer.
            ManagementScope scope =
                new ManagementScope(
                "\\\\FullComputerName\\root\\cimv2", options);
            scope.Connect();
 
            //Query system for Operating System information
            ObjectQuery query = new ObjectQuery(
                "select * from Win32_Processor");
            ManagementObjectSearcher searcher =
                new ManagementObjectSearcher(scope, query);
 
            ManagementObjectCollection queryCollection = searcher.Get();
            foreach (ManagementObject m in queryCollection)
            {
                Console.WriteLine("CPU : {0}",
                    m["LoadPercentage"].ToString());   //cpu 使用率
            }
 
            double totalMem = 0;
            string strMsg = "";
            ObjectQuery query1 = new ObjectQuery(
               "Select TotalPhysicalMemory from Win32_LogicalMemoryConfiguration");
            ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query1);
            int i = 0;
            ManagementObjectCollection queryCollection1 = search.Get();
            foreach (ManagementObject info in queryCollection1)
            {
                i = i + 1;
                totalMem += Convert.ToDouble(info["TotalPhysicalMemory"].ToString()) / 1024;
                strMsg += string.Format("物理內存({0}):大小:{1}MB", i, Convert.ToDouble(info["TotalPhysicalMemory"].ToString()) / 1024);
            }
            strMsg += string.Format("總物理內存的大小:{0}MB <br/>", totalMem);
            Console.WriteLine(strMsg);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章