现在的位置: 首页 > 综合 > 正文

运用API函数获取系统信息

2011年06月22日 ⁄ 综合 ⁄ 共 7558字 ⁄ 字号 评论关闭
 
<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

抱歉!评论已关闭.