C#调用系统API函数实例

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication4
{
    class Program
    {
        //API函数的声明如下:
        [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);

        //引入winmm.dll库文件
         [ DllImport( "Winmm.dll", CharSet=CharSet.Auto )]
          public static extern int waveOutGetNumDevs();

          [ DllImport( "Winmm.dll", CharSet=CharSet.Auto )]
          public static extern int midiOutGetNumDevs();


        //定义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;
        }

        static void Main(string[] args)
        {
         
            //调用GetWindowsDirectory和GetSystemDirectory函数分别取得Windows路径和系统路径
            const int nChars = 128;
            StringBuilder Buff = new StringBuilder(nChars);
            GetWindowsDirectory(Buff, nChars);
            Console.WriteLine( "Windows路径:" + Buff.ToString());
            GetSystemDirectory(Buff, nChars);
            Console.WriteLine("系统路径:" + Buff.ToString());
           
            //调用GetSystemInfo函数获取CPU的相关信息
            CPU_INFO CpuInfo;
            CpuInfo = new CPU_INFO();
            GetSystemInfo(ref CpuInfo);
            Console.WriteLine( "本计算机中有" + CpuInfo.dwNumberOfProcessors.ToString() + "个CPU");
            Console.WriteLine("CPU的类型为" + CpuInfo.dwProcessorType.ToString());
            Console.WriteLine("CPU等级为" + CpuInfo.dwProcessorLevel.ToString());
            Console.WriteLine("CPU的OEM ID为" + CpuInfo.dwOemId.ToString());
            Console.WriteLine("CPU中的页面大小为" + CpuInfo.dwPageSize.ToString());
            
            //调用GlobalMemoryStatus函数获取内存的相关信息
            MEMORY_INFO MemInfo;
            MemInfo = new MEMORY_INFO();
            GlobalMemoryStatus(ref MemInfo);
            Console.WriteLine(MemInfo.dwMemoryLoad.ToString() + "%的内存正在使用") ;
            Console.WriteLine("物理内存共有" + MemInfo.dwTotalPhys.ToString() + "字节") ;
            Console.WriteLine("可使用的物理内存有" + MemInfo.dwAvailPhys.ToString() + "字节") ;
            Console.WriteLine( "交换文件总大小为" + MemInfo.dwTotalPageFile.ToString() + "字节") ;
            Console.WriteLine( "尚可交换文件大小为" + MemInfo.dwAvailPageFile.ToString() + "字节") ;
            Console.WriteLine( "总虚拟内存有" + MemInfo.dwTotalVirtual.ToString() + "字节") ;
            Console.WriteLine("未用虚拟内存有" + MemInfo.dwAvailVirtual.ToString() + "字节");
          

            //调用GetSystemTime函数获取系统时间信息
            SYSTEMTIME_INFO StInfo;
            StInfo = new SYSTEMTIME_INFO();
            GetSystemTime(ref StInfo);
             Console.WriteLine(StInfo.wYear.ToString() + "年" + StInfo.wMonth.ToString() + "月" + StInfo.wDay.ToString() + "日");
            Console.WriteLine((StInfo.wHour + 8).ToString() + "点" + StInfo.wMinute.ToString() + "分" + StInfo.wSecond.ToString() + "秒");

            //返回系统版本
            Console.WriteLine(System.Environment.OSVersion.Version.ToString());

            //判断系统是否装有声卡
            if (waveOutGetNumDevs() > 0 && midiOutGetNumDevs() > 0)
            {
                Console.WriteLine("系统检测到您当前机器已安装声卡!");
            }
            else
            {
                Console.WriteLine("系统检测到您当前机器未安装声卡!");
            }

        }
    }
}

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论 1

打赏作者

andylaudotnet

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值