zoukankan      html  css  js  c++  java
  • 运用API函数获取系统信息(转)

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


    namespace WindowsApplication10
    {
     
    /// <summary>
     
    /// Form1 的摘要说明。
     
    /// </summary>

     public class Form1 : System.Windows.Forms.Form
     
    {
      
    private System.Windows.Forms.GroupBox groupBox1;
      
    private System.Windows.Forms.GroupBox groupBox2;
      
    private System.Windows.Forms.GroupBox groupBox3;
      
    private System.Windows.Forms.GroupBox groupBox4;
      
    private System.Windows.Forms.Button button1;
      
    private System.Windows.Forms.Label AvailVirtual;
      
    private System.Windows.Forms.Label TotalVirtual;
      
    private System.Windows.Forms.Label AvailPageFile;
      
    private System.Windows.Forms.Label TotalPageFile;
      
    private System.Windows.Forms.Label AvailPhys;
      
    private System.Windows.Forms.Label TotalPhys;
      
    private System.Windows.Forms.Label MemoryLoad;
      
    private System.Windows.Forms.Label PageSize;
      
    private System.Windows.Forms.Label OemId;
      
    private System.Windows.Forms.Label ProcessorLevel;
      
    private System.Windows.Forms.Label ProcessorType;
      
    private System.Windows.Forms.Label NumberOfProcessors;
      
    private System.Windows.Forms.Label SystemDirectory;
      
    private System.Windows.Forms.Label WindowsDirectory;
      
    private System.Windows.Forms.Label Time;
      
    private System.Windows.Forms.Label Date;
      
    /// <summary>
      
    /// 必需的设计器变量。
      
    /// </summary>

      private System.ComponentModel.Container components = null;
      [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);

      [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; 
      }


      
    public Form1()
      
    {
       
    //
       
    // Windows 窗体设计器支持所必需的
       
    //
       InitializeComponent();

       
    //
       
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
       
    //
      }


      
    /// <summary>
      
    /// 清理所有正在使用的资源。
      
    /// </summary>

      protected override void Dispose( bool disposing )
      
    {
       
    if( disposing )
       
    {
        
    if (components != null
        
    {
         components.Dispose();
        }

       }

       
    base.Dispose( disposing );
      }


      
    Windows 窗体设计器生成的代码

      
    /// <summary>
      
    /// 应用程序的主入口点。
      
    /// </summary>

      [STAThread]
      
    static void Main() 
      
    {
       Application.Run(
    new Form1());
      }


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


      }

     }


  • 相关阅读:
    有向图的连通性
    P6149 [USACO20FEB]Triangles S
    P6148 [USACO20FEB]Swapity Swapity Swap S
    RabbitMQ六中工作模式-主题模式
    turbine 集群聚合监控
    RabbitMQ六中工作模式-路由模式
    RabbitMQ六中工作模式-发布和订阅模式
    RabbitMQ六中工作模式-工作模式
    RabbitMQ六中工作模式-简单模式
    feign 声明式客户端接口
  • 原文地址:https://www.cnblogs.com/jhtchina/p/334855.html
Copyright © 2011-2022 走看看