《拓文asp.net网站内容管理系统》官方演示站。
 设为首页  
 收藏本站
 作者:黄振安
 QQ:53854651
首 页 开源拓文.NET编程网站编程网页设计图形图像数据库软件工程留言本
    站内搜索:  
您现在的位置: 拓文网络 → .NET编程 → ASP.NET → 运用API函数获取系统信息[正文]

热 门 文 章

相 关 文 章
  • ASP.NET性能优化
  • 在.NET框架应用程序中发送电子邮件
  • .net打包自动安装数据库
  • asp.net开发wap必备:更好的匹配手机设备
  • 用ashx动态生成文件
  • windows 2003下asp.Net站点无法使用用户名+密码的方
  • ASP.NET中17种正则表达式
  • 提高ASP.Net应用程序性能的十大方法
  • 一个通过web.Mail发送邮件的类
  • 在DataGrid里面根据日期的不同显示new图标
  • 运用API函数获取系统信息
     
    [作者:不详    转贴自:不详    点击数:    更新时间:2006-4-6 15:56:05]


    public class SystemInfo : System.Web.UI.Page
        {
            //要添加如下引用
            //using System.Runtime.InteropServices;
            //using System.Text;
            protected System.Web.UI.WebControls.Label WindowsDirectory;
            protected System.Web.UI.WebControls.Label SystemDirectory;
            protected System.Web.UI.WebControls.Label NumberOfProcessors;
            protected System.Web.UI.WebControls.Label ProcessorType;
            protected System.Web.UI.WebControls.Label ProcessorLevel;
            protected System.Web.UI.WebControls.Label OemId;
            protected System.Web.UI.WebControls.Label PageSize;
            protected System.Web.UI.WebControls.Label MemoryLoad;
            protected System.Web.UI.WebControls.Label TotalPhys;
            protected System.Web.UI.WebControls.Label AvailPhys;
            protected System.Web.UI.WebControls.Label TotalPageFile;
            protected System.Web.UI.WebControls.Label AvailPageFile;
            protected System.Web.UI.WebControls.Label TotalVirtual;
            protected System.Web.UI.WebControls.Label AvailVirtual;
            protected System.Web.UI.WebControls.Label Date;
            protected System.Web.UI.WebControls.Label Time;
            protected System.Web.UI.WebControls.Button Button1;

            //在调用API之前,你必须先导入System.Runtime.InteropServices这个名称空间。
            //其中,"DllImport"属性用来从不可控代码中调用一个方法,它指定了DLL的位置,该DLL中包含调用的外部方法;
            //"kernel32"设定了类库名;"public"指明函数的访问类型为公有的;
            //"static"修饰符声明一个静态元素,而该元素属于类型本身而不是指定的对象;
            //"extern"表示该方法将在工程外部执行,同时使用DllImport导入的方法必须使用"extern"修饰符
            [DllImport("kernel32")]
            public static extern void GetWindowsDirectory(StringBuilder WinDir,int count);
            [DllImport("kernel32")]
            public static extern void GetSystemDirectory(StringBuilder SysDir,int count);
            [DllImport("kernel32")]
            public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
            [DllImport("kernel32")]
            public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
            [DllImport("kernel32")]
            public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);

            //定义CPU的信息结构
            [StructLayout(LayoutKind.Sequential)]
            public struct CPU_INFO
            {
                public uint dwOemId;
                public uint dwPageSize;
                public uint lpMinimumApplicationAddress;
                public uint lpMaximumApplicationAddress;
                public uint dwActiveProcessorMask;
                public uint dwNumberOfProcessors;
                public uint dwProcessorType;
                public uint dwAllocationGranularity;
                public uint dwProcessorLevel;
                public uint dwProcessorRevision;
            }
            //定义内存的信息结构
            [StructLayout(LayoutKind.Sequential)]
            public struct MEMORY_INFO
            {
                public uint dwLength;
                public uint dwMemoryLoad;
                public uint dwTotalPhys;
                public uint dwAvailPhys;
                public uint dwTotalPageFile;
                public uint dwAvailPageFile;
                public uint dwTotalVirtual;
                public uint dwAvailVirtual;
            }
            //定义系统时间的信息结构
            [StructLayout(LayoutKind.Sequential)]
            public struct SYSTEMTIME_INFO
            {
                public ushort wYear;
                public ushort wMonth;
                public ushort wDayOfWeek;
                public ushort wDay;
                public ushort wHour;
                public ushort wMinute;
                public ushort wSecond;
                public ushort wMilliseconds;
            }


            private void Page_Load(object sender, System.EventArgs e)
            {
                // Put user code to initialize the page here
            }

            Web Form Designer generated code#region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                //
                // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                //
                InitializeComponent();
                base.OnInit(e);
            }
           
            /**//// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {   
                this.Button1.Click += new System.EventHandler(this.Button1_Click);
                this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            private void Button1_Click(object sender, System.EventArgs e)
            {
                //调用GetWindowsDirectory和GetSystemDirectory函数分别取得Windows路径和系统路径
                const int nChars = 128;
                StringBuilder Buff = new StringBuilder(nChars);
                GetWindowsDirectory(Buff,nChars);
                WindowsDirectory.Text = "Windows路径:"+Buff.ToString();
                GetSystemDirectory(Buff,nChars);
                SystemDirectory.Text = "系统路径:"+Buff.ToString();
                //调用GetSystemInfo函数获取CPU的相关信息
                CPU_INFO CpuInfo;
                CpuInfo = new CPU_INFO();
                GetSystemInfo(ref CpuInfo);
                NumberOfProcessors.Text = "本计算机中有"+CpuInfo.dwNumberOfProcessors.ToString()+"个CPU";
                ProcessorType.Text = "CPU的类型为"+CpuInfo.dwProcessorType.ToString();
                ProcessorLevel.Text = "CPU等级为"+CpuInfo.dwProcessorLevel.ToString();
                OemId.Text = "CPU的OEM ID为"+CpuInfo.dwOemId.ToString();
                PageSize.Text = "CPU中的页面大小为"+CpuInfo.dwPageSize.ToString();
                //调用GlobalMemoryStatus函数获取内存的相关信息
                MEMORY_INFO MemInfo;
                MemInfo = new MEMORY_INFO();
                GlobalMemoryStatus(ref MemInfo);
                MemoryLoad.Text = MemInfo.dwMemoryLoad.ToString()+"%的内存正在使用";
                TotalPhys.Text = "物理内存共有"+MemInfo.dwTotalPhys.ToString()+"字节";
                AvailPhys.Text = "可使用的物理内存有"+MemInfo.dwAvailPhys.ToString()+"字节";
                TotalPageFile.Text = "交换文件总大小为"+MemInfo.dwTotalPageFile.ToString()+"字节";
                AvailPageFile.Text = "尚可交换文件大小为"+MemInfo.dwAvailPageFile.ToString()+"字节";
                TotalVirtual.Text = "总虚拟内存有"+MemInfo.dwTotalVirtual.ToString()+"字节";
                AvailVirtual.Text = "未用虚拟内存有"+MemInfo.dwAvailVirtual.ToString()+"字节";
                //调用GetSystemTime函数获取系统时间信息
                SYSTEMTIME_INFO StInfo;
                StInfo = new SYSTEMTIME_INFO();
                GetSystemTime(ref StInfo);
                Date.Text = StInfo.wYear.ToString()+"年"+StInfo.wMonth.ToString()+"月"+StInfo.wDay.ToString()+"日";
                Time.Text = (StInfo.wHour+8).ToString()+"点"+StInfo.wMinute.ToString()+"分"+StInfo.wSecond.ToString()+"秒";
            }
        }


    首页 | 1 | 2 | 尾页  共 2 页
     
    winner 打印本页 关闭窗口

         文 章 评 论
    评论人 查看该文章的评论 发表时间
       笔 名* : 0 50分 60分 70分 80分 90分 100分
    评论内容 *: (最多1000字。当前字数:0)


    没有相关评论,欢迎您参与!

    - 关于本站 - 网站帮助 - 下载声明 - 友情连接 - 网站地图 -
    Copyright © 2005-2006 myTopWin.Com. All Rights Reserved . [桂ICP备06004496号]