1. 程式人生 > >c#獲取系統資訊的幾種方式

c#獲取系統資訊的幾種方式

1.Environment

這個沒什麼好說的,最簡單的方法

 //提供有關當前環境和平臺的資訊以及操作它們的方法。此類不能被繼承。

        //獲取或設定當前工作目錄的完全限定路徑。
        public static string CurrentDirectory { get; set; }
        //獲取當前計算機上的處理器數。
        public static string CommandLine { get; }
        //獲取或設定程序的退出程式碼
        public static int ExitCode { get; set; }
        //獲取系統啟動後經過的毫秒數。
        public static int TickCount { get; }
        //獲取作業系統的記憶體頁的位元組數
        public static int SystemPageSize { get; }
        //獲取為此環境定義的換行字串。
        public static string NewLine { get; }
        //獲取一個 System.Version 物件,該物件描述公共語言執行時的主版本、次版本、內部版本和修訂號。   
        public static long WorkingSet { get; }
        //獲取包含當前平臺識別符號和版本號的 System.OperatingSystem 物件。
        public static OperatingSystem OSVersion { get; }
        //獲取當前的堆疊跟蹤資訊。
        public static string StackTrace { get; }
        //確定當前程序是否為 64 位程序。
        public static bool Is64BitProcess { get; }
        //確定當前作業系統是否為 64 位作業系統。
        public static bool Is64BitOperatingSystem { get; }
        //獲取一個值,該值指示當前的應用程式域是否正在解除安裝或者公共語言執行時 (CLR) 是否正在關閉。    
        public static bool HasShutdownStarted { get; }
        //獲取當前已登入到 Windows 作業系統的人員的使用者名稱。
        public static string UserName { get; }
        //獲取一個值,用以指示當前程序是否在使用者互動模式中執行。
        public static bool UserInteractive { get; }
        //獲取與當前使用者關聯的網路域名。
        public static string UserDomainName { get; }
        //獲取當前託管執行緒的唯一識別符號。
        public static int CurrentManagedThreadId { get; }
        //獲取系統目錄的完全限定路徑。
        public static string SystemDirectory { get; }
        //獲取此本地計算機的 NetBIOS 名稱。
        public static string MachineName { get; }
        //終止此程序併為基礎作業系統提供指定的退出程式碼。
        public static void Exit(int exitCode);
        //將嵌入到指定字串中的每個環境變數的名稱替換為該變數的值的等效字串,然後返回結果字串。    
        public static string ExpandEnvironmentVariables(string name);
        //向 Windows 的應用程式事件日誌寫入訊息後立即終止程序,然後在發往 Microsoft 的錯誤報告中加入該訊息和異常資訊。
        public static void FailFast(string message, Exception exception);
        //向 Windows 的應用程式事件日誌寫入訊息後立即終止程序,然後在發往 Microsoft 的錯誤報告中加入該訊息。
        public static void FailFast(string message);
        //返回包含當前程序的命令列引數的字串陣列。
        public static string[] GetCommandLineArgs();
        //從當前程序或者從當前使用者或本地計算機的 Windows 作業系統登錄檔項檢索環境變數的值。
        public static string GetEnvironmentVariable(string variable, EnvironmentVariableTarget target);
        //從當前程序檢索環境變數的值。
        public static string GetEnvironmentVariable(string variable);
        //從當前程序或者從當前使用者或本地計算機的 Windows 作業系統登錄檔項檢索所有環境變數名及其值。
        public static IDictionary GetEnvironmentVariables(EnvironmentVariableTarget target);
        //從當前程序檢索所有環境變數名及其值。
        public static IDictionary GetEnvironmentVariables();
        //獲取由指定列舉標識的系統特殊資料夾的路徑,並使用用於訪問特殊資料夾的指定選項。
        public static string GetFolderPath(SpecialFolder folder, SpecialFolderOption option);
        //獲取由指定列舉標識的系統特殊資料夾的路徑。
        public static string GetFolderPath(SpecialFolder folder);
        //返回包含當前計算機中的邏輯驅動器名稱的字串陣列。
        public static string[] GetLogicalDrives();
        //建立、修改或刪除當前程序中或者為當前使用者或本地計算機保留的 Windows 作業系統登錄檔項中儲存的環境變數。
        public static void SetEnvironmentVariable(string variable, string value, EnvironmentVariableTarget target);
        //建立、修改或刪除當前程序中儲存的環境變數。
        public static void SetEnvironmentVariable(string variable, string value);

2.wmi

例程

需要using System.Management;

如果提示找不到去引用那新增System.Management;

            //cpu佔用率實時
            string cpuInfo = "";
            while (true)
            {
                ManagementClass mc = new ManagementClass("Win32_PerfFormattedData_PerfOS_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                try
                {
                    foreach (ManagementObject mo in moc)
                    {

                        cpuInfo = mo["PercentProcessorTime"].ToString() + "  ";
                        Console.Write(cpuInfo);
                    }
                    //會打印出9個數,因為我是總的佔有率+8個獨自的執行緒

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
                Console.Write("\n");
               // Thread.Sleep(300);
            }
             //可用記憶體
            string cpuInfo = ""; 
            while (true)
            {
                ManagementClass mc = new ManagementClass("Win32_PerfFormattedData_PerfOS_Memory");
                ManagementObjectCollection moc = mc.GetInstances();
                try
                {
                    foreach (ManagementObject mo in moc)
                    {

                        cpuInfo = mo["AvailableMBytes"].ToString() + "  ";
                        Console.Write(cpuInfo);
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
                Console.Write("\n");
               // Thread.Sleep(300);
            }
                //全部記憶體
                ManagementClass mc = new                         
                ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                try
                {
                    foreach (ManagementObject mo in moc)
                    {
                        Console.Write((((UInt64)mo["TotalPhysicalMemory"]) / Math.Pow(2, 20)).ToString());
                    }

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }

實際上難找的一批。

3.kernel32.dll

....垃圾

4.PerformanceCounter

需要using System.Diagnostics;

例程

//獲取cpu的實時佔比
            PerformanceCounter counter = new PerformanceCounter("Processor",  "% Processor Time","_Total");
            while (true)
            {
                Thread.Sleep(300);
                Console.WriteLine(counter.NextValue());
            }

用了3個引數例項化,

// 引數:
        //   categoryName:
        //     此效能計數器關聯的效能計數器類別(效能物件)的名稱。
        //
        //   counterName:
        //     效能計數器的名稱。
        //
        //   instanceName:
        //     效能計數器類別例項的名稱,或者為空字串 ("")(如果該類別包含單個例項)。

這3個可以這麼獲取

第一個引數

  //獲取所有類名
            foreach (var item in PerformanceCounterCategory.GetCategories())
            {
                Console.WriteLine(item.CategoryName);
            }

//這裡是獲取類下的例項名稱
            PerformanceCounterCategory category = new PerformanceCounterCategory();
            category.CategoryName = "Processor";//處理器類
            foreach (var item in category.GetInstanceNames())
            {
                Console.WriteLine(item);
            }

//這裡是獲取類下的計數器名稱
            PerformanceCounterCategory category = new PerformanceCounterCategory();
            category.CategoryName = "Processor";//處理器類
            foreach (var item in category.GetCounters("0"))//因為不止一個所以不能空,引數在三裡獲取
            {
                Console.WriteLine(item.CounterName);
            }

如果就一個例項的話是在三裡沒有東西打印出來的,直接空著就行。

例如

  //獲取實時可用記憶體
            PerformanceCounter counter = new PerformanceCounter("Memory", "Available MBytes");
            while (true)
            {
                Thread.Sleep(300);
                Console.WriteLine(counter.NextValue());
            }