C# 获取当前电脑CPU型号,显卡名称,数量,内存条数量,大小,硬盘大小

//获取CPU名称
        private void button1_Click(object sender, EventArgs e)
        {
            string CPUName = "";
            ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_Processor");//Win32_Processor  CPU处理器
            foreach (ManagementObject mo in mos.Get())
            {
                CPUName = mo["Name"].ToString();
            }
            mos.Dispose();
            label1.Text = CPUName;
        }

        //获取显卡数量,及显卡名称
        private void button2_Click(object sender, EventArgs e)
        {
            string DisplayName = "";
            ManagementClass m = new ManagementClass("Win32_VideoController");
            ManagementObjectCollection mn = m.GetInstances();
            DisplayName = "显卡数量:" + mn.Count.ToString() + "  ";
            ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_VideoController");//Win32_VideoController 显卡
            int count = 0;
            foreach (ManagementObject mo in mos.Get())
            {
                count++;
                DisplayName += "第" + count.ToString() + "张显卡名称:" + mo["Name"].ToString() + "   ";
            }
            mn.Dispose();
            m.Dispose();
            label2.Text = DisplayName;
        }

        //内存条数量,及大小
        private void button3_Click(object sender, EventArgs e)
        {
            string PhysicalMemory = "";
            ManagementClass m = new ManagementClass("Win32_PhysicalMemory");//内存条
            ManagementObjectCollection mn = m.GetInstances();
            PhysicalMemory = "物理内存条数量:" + mn.Count.ToString() + "  ";
            double capacity = 0.0;
            int count = 0;
            foreach (ManagementObject mo1 in mn)
            {
                count++;
                capacity = ((Math.Round(Int64.Parse(mo1.Properties["Capacity"].Value.ToString()) / 1024 / 1024 / 1024.0, 1)));
                PhysicalMemory += "第" + count.ToString() + "张内存条大小:" + capacity.ToString() + "G   ";
            }
            mn.Dispose();
            m.Dispose();
            label3.Text = PhysicalMemory;
        }

        //硬盘大小
        private void button4_Click(object sender, EventArgs e)
        {
            string DiskDrive = "硬盘为:";
            ManagementClass m = new ManagementClass("win32_DiskDrive");//硬盘
            ManagementObjectCollection mn = m.GetInstances();
            double capacity = 0.0;
            foreach (ManagementObject mo1 in mn)
            {
                capacity += Int64.Parse(mo1.Properties["Size"].Value.ToString()) / 1024 / 1024/ 1024;
            }
            mn.Dispose();
            m.Dispose();
            label4.Text = DiskDrive + capacity.ToString();
        }

可以参考链接 https://www.cnblogs.com/champaign/p/9129712.html

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,可以通过使用Windows API函数来获取当前电脑接入的扫码枪设备名称。具体步骤如下: 1. 引入Windows API函数 ```csharp using System.Runtime.InteropServices; ``` 2. 定义Windows API函数 ```csharp [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetKeyboardLayout(uint idThread); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern uint MapVirtualKeyEx(uint uCode, uint uMapType, IntPtr dwhkl); ``` 3. 定义获取设备名称的方法 ```csharp public static string GetScannerDeviceName() { uint processId; IntPtr foregroundWindow = GetForegroundWindow(); GetWindowThreadProcessId(foregroundWindow, out processId); uint hkl = (uint)GetKeyboardLayout(processId); StringBuilder deviceName = new StringBuilder(1024); if (GetKeyboardLayoutName(deviceName) != 0) { string deviceNameString = deviceName.ToString(); uint scanCode = MapVirtualKeyEx((uint)(Keys.RMenu), 0, (IntPtr)hkl); if (scanCode != 0) { deviceNameString += $"_{scanCode.ToString()}"; return deviceNameString; } } return null; } ``` 在上述代码中,我们首先获取当前激活窗口的进程ID,然后通过进程ID获取当前激活窗口的键盘布局,进而获取扫码枪的设备名称。 需要注意的是,上述代码只适用于键盘模拟扫码枪输入的情况,如果扫码枪通过其他方式(如串口)输入数据,则需要使用其他方法来获取设备名称

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值