C#实战小技巧(十五):获取计算机系统信息和性能信息

1.操作系统版本

		public string GetOSVersion()
        {
            string os = string.Empty;
            try
            {
                if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 1)
                {
                    os = "Windows 7";
                }
                else if (Environment.OSVersion.Version.CompareTo(new Version("10.0")) >= 0)
                {
                    os = "Windows 10";
                }
                else if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 1)
                {
                    os = "Windows XP";
                }
                else if (Environment.OSVersion.Version.CompareTo(new Version("6.2")) >= 0)
                {
                    if (Environment.OSVersion.Version.CompareTo(new Version("6.3")) >= 0)
                    {
                        os = "Windows 8.1";
                    }
                    else
                    {
                        os = "Windows 8";
                    }
                }
                else if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor == 0)
                {
                    os = "Windows Vista";
                }
                else if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 2)
                {
                    os = "Windows 2003";
                }
                else if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor == 0)
                {
                    os = "Windows 2000";
                }
                else if (Environment.OSVersion.Platform == PlatformID.Win32Windows && Environment.OSVersion.Version.Minor == 10 && Environment.OSVersion.Version.Revision.ToString() == "2222A")
                {
                    os = "Windows 98 第二版";
                }
                else if (Environment.OSVersion.Platform == PlatformID.Win32Windows && Environment.OSVersion.Version.Minor == 10 && Environment.OSVersion.Version.Revision.ToString() != "2222A")
                {
                    os = "Windows 98";
                }
                else if (Environment.OSVersion.Platform == PlatformID.Unix)
                {
                    os = "Unix";
                }
            }
            catch (Exception ex)
            {
                LOG.Error(new StringBuilder("获取操作系统版本出错:").Append(ex.Message).ToString());
            }

            return os;
        }

2.系统类型(32位 or 64位)

		public string GetSystemType()
        {
            if (Environment.Is64BitOperatingSystem)
            {
                return "64 位操作系统";
            }
            else
            {
                return "32 位操作系统";
            }
        }

3.内核数量

		public int GetCPUCount()
        {
            return Environment.ProcessorCount;
        }

4.处理器

		public string GetProcessorName()
        {
            //Win32_PhysicalMemory;Win32_Keyboard;Win32_ComputerSystem;Win32_OperatingSystem
            try
            {
                ManagementClass mos = new ManagementClass("Win32_Processor");
                foreach (ManagementObject mo in mos.GetInstances())
                {
                    if (mo["Name"] != null)
                    {
                        return mo["Name"].ToString();
                    }

                    //PropertyDataCollection pdc = mo.Properties;
                    //foreach (PropertyData pd in pdc)
                    //{
                    //    if ("Name" == pd.Name)
                    //    {
                    //        return pd.Value.ToString();
                    //    }
                    //}
                }
            }
            catch (Exception ex)
            {
                LOG.Error(new StringBuilder("获取处理器名称失败:").Append(ex.Message).ToString());
            }
            
            return string.Empty;
        }

5.开机后运行时间

		public string GetRunningTime()
        {
            string result = string.Empty;
            try
            {
                int uptime = Environment.TickCount & Int32.MaxValue;
                TimeSpan ts = new TimeSpan(Convert.ToInt64(Convert.ToInt64(uptime) * 10000));
                result = new StringBuilder(ts.Days.ToString()).Append("天 ").Append(ts.Hours).Append(":").
                    Append(ts.Minutes).Append(":").Append(ts.Seconds).ToString();
            }
            catch (Exception ex)
            {
                LOG.Error(new StringBuilder("获取开机时间失败:").Append(ex.Message).ToString());
            }

            return result;
        }

6.CPU使用率

		public float GetUsageOfCPU()
        {
        	this.pcCpuLoad = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            this.pcCpuLoad.MachineName = ".";
            this.pcCpuLoad.NextValue();
 
            return this.pcCpuLoad.NextValue();
        }

7.物理内存

		public long GetPhysicalMemory()
        {
            ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
            ManagementObjectCollection moc = mc.GetInstances();
            foreach (ManagementObject mo in moc)
            {
                if (mo["TotalPhysicalMemory"] != null)
                {
                    return long.Parse(mo["TotalPhysicalMemory"].ToString());
                }
            }
            
			return 0;
        }

8.可用内存

		public long GetAvailableMemory()
        {
            long availablebytes = 0;
            try
            {
                ManagementClass mos = new ManagementClass("Win32_OperatingSystem");
                foreach (ManagementObject mo in mos.GetInstances())
                {
                    if (mo["FreePhysicalMemory"] != null)
                    {
                        availablebytes = 1024 * long.Parse(mo["FreePhysicalMemory"].ToString());
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                LOG.Error(new StringBuilder("获取可用内存失败:").Append(ex.Message).ToString());
            }

            return availablebytes;
        }

9.进程数量、线程数量、句柄数量

		public void GetProcessCount(out int processCount, out int threadCount, out int handleCount) 
        {
            Process[] processes = Process.GetProcesses();
            processCount = processes.Count();
            foreach (Process pro in processes)
            {
                threadCount += pro.Threads.Count;
                handleCount += pro.HandleCount;
            }
        }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mars Coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值