运用API函数获取系统信息

本文介绍了一个C# Web应用程序,用于获取并展示系统的基本信息,包括Windows路径、系统路径、内存使用情况、CPU信息以及系统时间等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<HTML>
<HEAD>
<title>SystemInfo</title>
<metaname="GENERATOR"Content="MicrosoftVisualStudio.NET7.1">
<metaname="CODE_LANGUAGE"Content="C#">
<metaname="vs_defaultClientScript"content="JavaScript">
<metaname="vs_targetSchema"content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<bodyMS_POSITIONING="GridLayout">
<formid="Form1"method="post"runat="server">
<colgroup>
<colwidth="50%">
<colwidth="50%">
</colgroup>
<tablewidth="100%">
<tr>
<tdbgcolor="#99ccff">Window及系统路径:</td>
<tdbgcolor="#99ccff">系统日期信息:</td>
</tr>
<tr>
<td>
<asp:Labelid="WindowsDirectory"runat="server">WindowsDirectory</asp:Label></td>
<td>
<asp:Labelid="Date"runat="server">Date</asp:Label></td>
</tr>
<tr>
<td>
<asp:Labelid="SystemDirectory"runat="server">SystemDirectoryLabel</asp:Label></td>
<td>
<asp:Labelid="Time"runat="server">Time</asp:Label></td>
</tr>
<tr>
<tdbgcolor="#99ccff">内存信息:</td>
<tdbgcolor="#99ccff">CUP信息:</td>
</tr>
<tr>
<td>
<asp:Labelid="MemoryLoad"runat="server">MemoryLoad</asp:Label></td>
<td>
<asp:Labelid="NumberOfProcessors"runat="server">NumberOfProcessorsLabel</asp:Label></td>
</tr>
<tr>
<td>
<asp:Labelid="TotalPhys"runat="server">TotalPhys</asp:Label></td>
<td>
<asp:Labelid="ProcessorType"runat="server">ProcessorTypeLabel</asp:Label></td>
</tr>
<tr>
<td>
<asp:Labelid="AvailPhys"runat="server">AvailPhys</asp:Label></td>
<td>
<asp:Labelid="ProcessorLevel"runat="server">ProcessorLevelLabel</asp:Label></td>
</tr>
<tr>
<td>
<asp:Labelid="TotalPageFile"runat="server">TotalPageFile</asp:Label></td>
<td>
<asp:Labelid="OemId"runat="server">OemIdLabel</asp:Label></td>
</tr>
<tr>
<td>
<asp:Labelid="AvailPageFile"runat="server">AvailPageFile</asp:Label></td>
<td>
<asp:Labelid="PageSize"runat="server">PageSize</asp:Label></td>
</tr>
<tr>
<td>
<asp:Labelid="TotalVirtual"runat="server">TotalVirtualLabel</asp:Label></td>
<td></td>
</tr>
<tr>
<td>
<asp:Labelid="AvailVirtual"runat="server">AvailVirtualLabel</asp:Label></td>
<td></td>
</tr>
</table>
<asp:Buttonid="Button1"style="Z-INDEX:101;LEFT:752px;POSITION:absolute;TOP:304px"runat="server"
Text
="获取"></asp:Button>
</form>
</body>
</HTML>

publicclassSystemInfo:System.Web.UI.Page
{
//要添加如下引用
//usingSystem.Runtime.InteropServices;
//usingSystem.Text;
protectedSystem.Web.UI.WebControls.LabelWindowsDirectory;
protectedSystem.Web.UI.WebControls.LabelSystemDirectory;
protectedSystem.Web.UI.WebControls.LabelNumberOfProcessors;
protectedSystem.Web.UI.WebControls.LabelProcessorType;
protectedSystem.Web.UI.WebControls.LabelProcessorLevel;
protectedSystem.Web.UI.WebControls.LabelOemId;
protectedSystem.Web.UI.WebControls.LabelPageSize;
protectedSystem.Web.UI.WebControls.LabelMemoryLoad;
protectedSystem.Web.UI.WebControls.LabelTotalPhys;
protectedSystem.Web.UI.WebControls.LabelAvailPhys;
protectedSystem.Web.UI.WebControls.LabelTotalPageFile;
protectedSystem.Web.UI.WebControls.LabelAvailPageFile;
protectedSystem.Web.UI.WebControls.LabelTotalVirtual;
protectedSystem.Web.UI.WebControls.LabelAvailVirtual;
protectedSystem.Web.UI.WebControls.LabelDate;
protectedSystem.Web.UI.WebControls.LabelTime;
protectedSystem.Web.UI.WebControls.ButtonButton1;

//在调用API之前,你必须先导入System.Runtime.InteropServices这个名称空间。
//其中,"DllImport"属性用来从不可控代码中调用一个方法,它指定了DLL的位置,该DLL中包含调用的外部方法;
//"kernel32"设定了类库名;"public"指明函数的访问类型为公有的;
//"static"修饰符声明一个静态元素,而该元素属于类型本身而不是指定的对象;
//"extern"表示该方法将在工程外部执行,同时使用DllImport导入的方法必须使用"extern"修饰符
[DllImport("kernel32")]
publicstaticexternvoidGetWindowsDirectory(StringBuilderWinDir,intcount);
[DllImport(
"kernel32")]
publicstaticexternvoidGetSystemDirectory(StringBuilderSysDir,intcount);
[DllImport(
"kernel32")]
publicstaticexternvoidGetSystemInfo(refCPU_INFOcpuinfo);
[DllImport(
"kernel32")]
publicstaticexternvoidGlobalMemoryStatus(refMEMORY_INFOmeminfo);
[DllImport(
"kernel32")]
publicstaticexternvoidGetSystemTime(refSYSTEMTIME_INFOstinfo);

//定义CPU的信息结构
[StructLayout(LayoutKind.Sequential)]
publicstructCPU_INFO
{
publicuintdwOemId;
publicuintdwPageSize;
publicuintlpMinimumApplicationAddress;
publicuintlpMaximumApplicationAddress;
publicuintdwActiveProcessorMask;
publicuintdwNumberOfProcessors;
publicuintdwProcessorType;
publicuintdwAllocationGranularity;
publicuintdwProcessorLevel;
publicuintdwProcessorRevision;
}

//定义内存的信息结构
[StructLayout(LayoutKind.Sequential)]
publicstructMEMORY_INFO
{
publicuintdwLength;
publicuintdwMemoryLoad;
publicuintdwTotalPhys;
publicuintdwAvailPhys;
publicuintdwTotalPageFile;
publicuintdwAvailPageFile;
publicuintdwTotalVirtual;
publicuintdwAvailVirtual;
}

//定义系统时间的信息结构
[StructLayout(LayoutKind.Sequential)]
publicstructSYSTEMTIME_INFO
{
publicushortwYear;
publicushortwMonth;
publicushortwDayOfWeek;
publicushortwDay;
publicushortwHour;
publicushortwMinute;
publicushortwSecond;
publicushortwMilliseconds;
}



privatevoidPage_Load(objectsender,System.EventArgse)
{
//Putusercodetoinitializethepagehere
}


WebFormDesignergeneratedcode#regionWebFormDesignergeneratedcode
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:ThiscallisrequiredbytheASP.NETWebFormDesigner.
//
InitializeComponent();
base.OnInit(e);
}


/**////<summary>
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///</summary>

privatevoidInitializeComponent()
{
this.Button1.Click+=newSystem.EventHandler(this.Button1_Click);
this.Load+=newSystem.EventHandler(this.Page_Load);

}

#endregion


privatevoidButton1_Click(objectsender,System.EventArgse)
{
//调用GetWindowsDirectory和GetSystemDirectory函数分别取得Windows路径和系统路径
constintnChars=128;
StringBuilderBuff
=newStringBuilder(nChars);
GetWindowsDirectory(Buff,nChars);
WindowsDirectory.Text
="Windows路径:"+Buff.ToString();
GetSystemDirectory(Buff,nChars);
SystemDirectory.Text
="系统路径:"+Buff.ToString();
//调用GetSystemInfo函数获取CPU的相关信息
CPU_INFOCpuInfo;
CpuInfo
=newCPU_INFO();
GetSystemInfo(
refCpuInfo);
NumberOfProcessors.Text
="本计算机中有"+CpuInfo.dwNumberOfProcessors.ToString()+"个CPU";
ProcessorType.Text
="CPU的类型为"+CpuInfo.dwProcessorType.ToString();
ProcessorLevel.Text
="CPU等级为"+CpuInfo.dwProcessorLevel.ToString();
OemId.Text
="CPU的OEMID为"+CpuInfo.dwOemId.ToString();
PageSize.Text
="CPU中的页面大小为"+CpuInfo.dwPageSize.ToString();
//调用GlobalMemoryStatus函数获取内存的相关信息
MEMORY_INFOMemInfo;
MemInfo
=newMEMORY_INFO();
GlobalMemoryStatus(
refMemInfo);
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_INFOStInfo;
StInfo
=newSYSTEMTIME_INFO();
GetSystemTime(
refStInfo);
Date.Text
=StInfo.wYear.ToString()+""+StInfo.wMonth.ToString()+""+StInfo.wDay.ToString()+"";
Time.Text
=(StInfo.wHour+8).ToString()+""+StInfo.wMinute.ToString()+""+StInfo.wSecond.ToString()+"";
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值