.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 { }
    }

附图:
在这里插入图片描述在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u014715882/article/details/83096155
今日推荐