打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
用C#获取系统内存
using System;
using System.Management; //此命名空间需要在
//“解决方案资源管理里右键点击”引用“,添加引用,在弹出的
//对话框中找到System.Management

namespace ConsoleApplication1
{
    /// <summary>
    /// Class1 的摘要说明。
    /// </summary>
    class Class1
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine("正在计算系统内存容量,请稍候…..");
            Console.WriteLine("实际内存容量为:"+GetPhisicalMemory().ToString());
            Console.ReadLine();
        }
        private static int GetPhisicalMemory()
        {    
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(); //用于查询一些如系统信息的管理对象
            searcher.Query = new SelectQuery("Win32_PhysicalMemory","",new string[]{"Capacity"});//设置查询条件
            ManagementObjectCollection collection = searcher.Get(); //获取内存容量
            ManagementObjectCollection.ManagementObjectEnumerator em = collection.GetEnumerator();
            
            int capacity = 0;
            while(em.MoveNext())
            {
                ManagementBaseObject baseObj = em.Current;
                if(baseObj.Properties["Capacity"].Value != null)
                {
                    try
                    {
                        capacity += int.Parse(baseObj.Properties["Capacity"].Value.ToString());
                    }
                    catch
                    {
                        Console.WriteLine("有错误发生!","错误信息");
                        return 0;
                    }
                }
            }
            return capacity;
        }    
    }
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
C#中大List的内存分配
Active Directory编程
C# System.DirectoryServices.DirectoryEntry 域用户验证
(教学思路 C#集合一)集合的概述、动态数组ArrayList
StringBuilder的实现与技巧
C#中数组Array、ArrayList、泛型List<T>的比较
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服