C#用API來查看系統資訊

來源:互聯網
上載者:User
<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

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.