---
title: "在.Net中用C#调用Windows系统信息的代码"
author: "foible"
date: "2008-02-17T17:25:00Z"
tags: []
slug: "net-c-windows"
draft: false
lastmod: "2013-01-14T08:58:31Z"
id: 1769173
url: "https://foible.iteye.com/blog/1769173"
---

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Text;  
using System.Windows.Forms;  
using System.Runtime.InteropServices;

namespace WinShutdown  
{  
public partial class WinInfo : Form  
{  
//定义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 ;  
}

\[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);

public WinInfo()  
{  
InitializeComponent();  
tmrTime.Start();  
}  
private void tmrTime\_Tick(object sender, 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);  
lblDate.Text = StInfo.wYear.ToString() + "年" + StInfo.wMonth.ToString() + "月" + StInfo.wDay.ToString() + "日";  
lblTime.Text = (StInfo.wHour + 8).ToString() + "点" + StInfo.wMinute.ToString() + "分" + StInfo.wSecond.ToString() + "秒";  
}

}  
}

PS:  
.Text = 前面的变量我都是用Lable的控件来代替.用于显示在From里...注意:在From里最好用个trimer控件来监听这些Lable控件, 所以我有了trimer控件中的唯一一个事件Tick...

希望对.Net学习的朋友提供一帮助!当然,这些资料都是从网上下载的,我只做了归纳而已...