1. 程式人生 > >.Net獲取作業系統常用引數方法

.Net獲取作業系統常用引數方法

本篇文章主要記錄一些作業系統常用引數的獲取方法,例如系統版本、磁碟空間、記憶體大小等

1. 獲取硬碟資訊:

    /// <summary>
    /// 獲取硬碟資訊
    /// </summary>
    /// <returns></returns>
    public void SetHardDiskInfo()
    {
        var GSDiskName = GlobalInfo.GSInstallPath.Substring(0, 1);
        DriveInfo[] drives = DriveInfo.GetDrives();
        StringBuilder sys = new StringBuilder();
        StringBuilder gs = new StringBuilder();
        foreach (DriveInfo drive in drives)
        {
            if (drive.IsReady)
            {
                if (drive.Name.Contains("C"))
                {
                    var val1 = (double)drive.TotalSize / 1024 / 1024 / 1024;
                    var val2 = (double)drive.TotalFreeSpace / 1024 / 1024 / 1024;
                    sys.AppendFormat("{0}  {2}/{3} GB  ({1}%可用)",
                        "系統盤 " + drive.Name.Substring(0,2),
                        string.Format("{0:N0}", val2 / val1 * 100),
                        (long)val2,
                        (long)val1);
                    if (val2 <= 5 || val2 / val1 <= 0.1)
                    {
                        richTextBox1.Text = sys.ToString();
                        richTextBox1.SelectionStart = richTextBox1.Find("(");
                        richTextBox1.SelectionLength = 7;
                        richTextBox1.SelectionColor = Color.Red;
                    }
                    else if ((5 < val2 && val2 <= 10) || (0.1 < val2 / val1 && val2 / val1 <= 0.2))
                    {
                        richTextBox1.Text = sys.ToString();
                        richTextBox1.SelectionStart = richTextBox1.Find("(");
                        richTextBox1.SelectionLength = 7;
                        richTextBox1.SelectionColor = Color.Orange;
                    }
                    else
                    {
                        richTextBox1.Text = sys.ToString();
                        richTextBox1.SelectionStart = richTextBox1.Find("(");
                        richTextBox1.SelectionLength = 7;
                        richTextBox1.SelectionColor = Color.Green;
                    }
                }
                if (drive.Name.Contains(GSDiskName))
                {
                    var val1 = (double)drive.TotalSize / 1024 / 1024 / 1024;
                    var val2 = (double)drive.TotalFreeSpace / 1024 / 1024 / 1024;
                    gs.AppendFormat("{0}  {2}/{3} GB ({1}%可用)",
                        "GS所在盤 " + drive.Name.Substring(0,2),
                        string.Format("{0:N0}", val2 / val1 * 100),
                        (long)val2,
                        (long)val1);
                    if (val2 <= 5 || val2 / val1 <= 0.1)
                    {
                        richTextBox2.Text = gs.ToString();
                        richTextBox2.SelectionStart = richTextBox2.Find("(");
                        richTextBox2.SelectionLength = 7;
                        richTextBox2.SelectionColor = Color.Red;
                    }
                    else if ((5 < val2 && val2 <= 10) || (0.1 < val2 / val1 && val2 / val1 <= 0.2))
                    {
                        richTextBox2.Text = gs.ToString();
                        richTextBox2.SelectionStart = richTextBox2.Find("(");
                        richTextBox2.SelectionLength = 7;
                        richTextBox2.SelectionColor = Color.Yellow;
                    }
                    else
                    {
                        richTextBox2.Text = gs.ToString();
                        richTextBox2.SelectionStart = richTextBox2.Find("(");
                        richTextBox2.SelectionLength = 7;
                        richTextBox2.SelectionColor = Color.Green;
                    }
                }
            }
        }
    }

2. 獲取系統名稱資訊:

    //Windows系統名稱
    private static string GetOSFriendlyName()
    {
        string result = string.Empty;
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem");
        foreach (ManagementObject os in searcher.Get())
        {
            result = os["Caption"].ToString();
            break;
        }
        return result;
    }

3. 獲取系統詳細版本資訊:

    //Windows系統詳細版本
    private static string GetOSVersion()
    {
        string result = string.Empty;
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT Version FROM Win32_OperatingSystem");
        foreach (ManagementObject os in searcher.Get())
        {
            result = os["Version"].ToString();
            break;
        }
        return result;
    }

4. 獲取系統記憶體資訊:

    //獲取系統記憶體
    private static string GetPhisicalMemory()
    {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(); //用於查詢一些如系統資訊的管理物件 
        searcher.Query = new SelectQuery("Win32_PhysicalMemory", "", new string[] { "Capacity" });//設定查詢條件 
        ManagementObjectCollection collection = searcher.Get(); //獲取記憶體容量 
        ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator();

        long capacity=0 ;
        while (em.MoveNext())
        {
            ManagementBaseObject baseObj = em.Current;
            if (baseObj.Properties["Capacity"].Value != null)
            {
                try
                {
                    capacity += Convert.ToInt64(baseObj.Properties["Capacity"].Value.ToString())/1024/1024/1024;
                }
                catch(Exception e)
                {
                    Console.WriteLine("有錯誤發生!", "錯誤資訊");
                    return "";
                }
            }
        }
        return capacity.ToString() + "GB";
    }

5. 展示計算機詳情:

這塊是呼叫了系統本身的系統資訊功能,具體介面如附圖

    //展示計算機詳情
    private void Detail_Click(object sender, EventArgs e)
    {
        const string cRegKeySysInfo = @"SOFTWARE\Microsoft\Shared Tools\MSINFO";
        const string cRegValSysInfo = "PATH";
        const string cRegKeySysInfoLoc = @"SOFTWARE\Microsoft\Shared Tools Location";
        const string cRegValSysInfoLoc = "MSINFO";
        try
        {
            string fileName;
            //不同版本的windows系統,登錄檔存放的位置不同,共有兩處
            //第一處
            RegistryKey getRegKey = Registry.LocalMachine;
            RegistryKey getSubKey = getRegKey.OpenSubKey(cRegKeySysInfo);
            if (getSubKey == null)
            {
                //第二處
                getSubKey = getRegKey.OpenSubKey(cRegKeySysInfoLoc);
                fileName = getSubKey.GetValue(cRegValSysInfoLoc).ToString() + @"\MSINFO32.EXE";
            }
            else
            {
                fileName = getSubKey.GetValue(cRegValSysInfo).ToString();
            }
            //呼叫外部可執行程式
            Process newPro = new Process();
            newPro.StartInfo.FileName = fileName;
            newPro.Start();
        }
        catch { }
    }

附圖:
在這裡插入圖片描述在這裡插入圖片描述