`

运用API函数获取系统信息

    博客分类:
  • C#
阅读更多
<HTML>
    <HEAD>
        <title>SystemInfo</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <colgroup>
                <col width="50%">
                <col width="50%">
            </colgroup>
            <table width="100%">
                <tr>
                    <td bgcolor="#99ccff">Window及系统路径:</td>
                    <td bgcolor="#99ccff">系统日期信息:</td>
                </tr>
                <tr>
                    <td>
                        <asp:Label id="WindowsDirectory" runat="server">WindowsDirectory</asp:Label></td>
                    <td>
                        <asp:Label id="Date" runat="server">Date</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        <asp:Label id="SystemDirectory" runat="server">SystemDirectoryLabel</asp:Label></td>
                    <td>
                        <asp:Label id="Time" runat="server">Time</asp:Label></td>
                </tr>
                <tr>
                    <td bgcolor="#99ccff">内存信息:</td>
                    <td bgcolor="#99ccff">CUP信息:</td>
                </tr>
                <tr>
                    <td>
                        <asp:Label id="MemoryLoad" runat="server">MemoryLoad</asp:Label></td>
                    <td>
                        <asp:Label id="NumberOfProcessors" runat="server">NumberOfProcessorsLabel</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        <asp:Label id="TotalPhys" runat="server">TotalPhys</asp:Label></td>
                    <td>
                        <asp:Label id="ProcessorType" runat="server">ProcessorTypeLabel</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        <asp:Label id="AvailPhys" runat="server">AvailPhys</asp:Label></td>
                    <td>
                        <asp:Label id="ProcessorLevel" runat="server">ProcessorLevelLabel</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        <asp:Label id="TotalPageFile" runat="server">TotalPageFile</asp:Label></td>
                    <td>
                        <asp:Label id="OemId" runat="server">OemIdLabel</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        <asp:Label id="AvailPageFile" runat="server">AvailPageFile</asp:Label></td>
                    <td>
                        <asp:Label id="PageSize" runat="server">PageSize</asp:Label></td>
                </tr>
                <tr>
                    <td>
                        <asp:Label id="TotalVirtual" runat="server">TotalVirtualLabel</asp:Label></td>
                    <td></td>
                </tr>
                <tr>
                    <td>
                        <asp:Label id="AvailVirtual" runat="server">AvailVirtualLabel</asp:Label></td>
                    <td></td>
                </tr>
            </table>
            <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 752px; POSITION: absolute; TOP: 304px" runat="server"
                Text="获取"></asp:Button>
        </form>
    </body>
</HTML>



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()+"秒";
        }

分享到:
评论

相关推荐

    C#运用API函数获取系统信息

    C#运用API函数获取系统信息

    在Visual C#中运用API函数获取系统信息

    在Visual C#中运用API函数获取系统信息

    asp.net知识库

    在Asp.net中如何用SQLDMO来获取SQL Server中的对象信息 使用Relations建立表之间的关系并却使用PagedDataSource类对DataList进行分页 通过作业,定时同步两个数据库 SQLSERVER高级注入技巧 利用反射实现ASP.NET控件和...

    获得鼠标处颜色RGB值

    用VB2010做的 主要运用API函数获得鼠标指针处的颜色值 无代码 仅供学习交流之用 学习中

    基于iFIX高速公路隧道监控系统的设计与实现

    为降低开发难度、提高开发效率,提出在组态软件脚本语言环境下运用API函数实现系统的通信设计。该系统以iFIX组态软件为平台,开发上位机监控系统软件,系统硬件部分搭建了以光纤作为传输介质的PLC冗余环网。运行结果...

    jnativehook_java键盘钩子_hookapi_jnativehook_

    钩子函数运用,结合java语言获取键盘、鼠标响应监控信息。

    Windows任务管理器的设计与实现

    据国外研究公司统计,在2008年全球个人计算机用户...在的设计过程中,通过调用Windows API函数而获得任务、进程、线程模块,以及系统资源使用情况等信息。最后在Windows10系统上进行测试,实现了进程管理的基本功能。

    软件工程工资管理系统

    工资管理信息系统对企业加强工资管理有着重要的作用,就一般的大型企业来说,它的设计内容非常复杂而且繁多,比如拥有工资计算功能,工资统计功能,报表输出功能,而且设计的模块也很多,比如工资管理模块,工资...

    java-servlet-api.doc

    你可以通过使用HttpSessionBindingListener接口获得这些信息。当你的应用存储数据到Session中,或从Session中清除数据,Servlet都会通过HttpSessionBindingListener检杳什么类被绑定或被取消绑定。这个接口的方法会...

    中文版Excel.2007高级VBA编程宝典.part1

     10.11.4 了解更多有关API 函数的信息  第11章 VBA编程示例和技巧  11.1 处理单元格区域  11.1.1 复制单元格区域  11.1.2 移动单元格区域  11.1.3 复制大小可变的单元格区域  11.1.4 选中或者识别各种类型的...

    UML State Machine Wizard VC Addin 7.0

    &gt; 提供了大量用于在Windows平台下模拟嵌入式应用程序的API函数和调试工具。通过运用这些工具,您可以不费劲地找出内存泄露 / 内存覆盖的“臭虫”、判断内存是否分配合理、检测程序在限定内存下的运行情况和为后期...

    MySQL中文参考手册

    o 4.1 怎样获得MySQL o 4.2 MySQL支持的操作系统 o 4.3 使用MySQL哪个版本 o 4.4 怎样和何时发布更新版本 o 4.5 安装布局 o 4.6 安装MySQL二进制代码分发 + 4.6.1 Linux RPM注意事项 + 4.6.2 构造客户程序 ...

    如何做简单VB外挂。

    需要vb api函数: findwindow ←寻找窗口列表中第一个符合指定条件的顶级窗口 getwindowthreadprocessid ←获取与指定窗口关联在一起的一个进程和线程标识符 --------------------------------------------------...

    非常实用的License管理程序

    1、GsLicPKey.exe 获取应用软件安装主机信息作为公钥 2、GsLicense.exe 引入步骤1产生的公钥文件,并对License进行授权 3、LicParser.dll 为C/S的对接的API,其函数原型如下: int CheckLicense(char *iFileName,...

    MySQL中文参考手册.chm

    4.6.2 构造客户程序 4.6.3 系统特定的问题 4.6.3.1 Linux 注意事项 4.6.3.2 HP-UX 注意事项 4.7 安装 MySQL源代码分发 4.7.1 快速安装概述 4.7.2 运用补丁 4.7.3 典型的...

    Visual C++2010开发权威指南(共三部分).part1.rar

    第6章 计算机测控系统概述 267 6.1 Visual C++ 2010 SDI开发简介 267 6.1.1 建立应用程序基本框架 267 6.1.2 处理视图 267 6.1.3 处理文档 271 6.1.4 串行化处理 274 6.1.5 sdi应用程序编程思路 275 6.2 Visual C++ ...

    POSCMS免费版 v3.2.0 bulid1118.zip

    自定义URL一直是POSCMS系统的一个亮点,能够DIY出各种格式的URL,并支持函数与自定义运用到标签中,增强了自定义URL的灵活性。 个性化栏目URL、内容URL、子内容URL、单页URL、会员空间URL等 支持自动生成伪静态...

Global site tag (gtag.js) - Google Analytics