You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VB.NET中获取已安装内存的完整信息

在VB.NET 2017中获取已安装内存完整信息

嘿,在VB.NET 2017里想拿到已安装内存的完整细节?我平时都是用WMI(Windows Management Instrumentation)来搞定这个,它能挖出单条内存的容量、制造商、型号、频率这些超详细的信息,比只看总内存有用多了。下面给你一步步来:

第一步:添加必要的引用

因为System.Management不是项目默认引用的,所以得先加上:

  • 右键你的VB.NET 2017项目 → 选择「添加」→「引用」
  • 在弹出的引用管理器里,找到System.Management并勾选,点击「确定」

第二步:编写代码实现查询

下面的代码可以获取每个内存模块的详细信息,还会输出总物理内存:

Imports System.Management

Module MemoryInfoTool
    Sub Main()
        GetMemoryModuleDetails()
        Console.ReadLine()
    End Sub

    Private Sub GetMemoryModuleDetails()
        ' 用WMI查询Win32_PhysicalMemory,这个类存储了物理内存模块的所有关键信息
        Dim wmiSearcher As New ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMemory")
        Dim memoryModules As ManagementObjectCollection = wmiSearcher.Get()

        Console.WriteLine("=== 已安装内存模块详细信息 ===")
        Console.WriteLine("-----------------------------")

        For Each module As ManagementObject In memoryModules
            ' 处理可能的空值,避免报错
            Dim capacity = If(module("Capacity") IsNot Nothing, 
                             $"{Math.Round(CLng(module("Capacity")) / 1024 / 1024 / 1024, 2)} GB", 
                             "未知容量")
            Dim manufacturer = If(module("Manufacturer") IsNot Nothing, 
                                 module("Manufacturer").ToString().Trim(), 
                                 "未知制造商")
            Dim partNumber = If(module("PartNumber") IsNot Nothing, 
                               module("PartNumber").ToString().Trim(), 
                               "未知部件号")
            Dim speed = If(module("Speed") IsNot Nothing, 
                          $"{module("Speed")} MHz", 
                          "未知频率")
            Dim memoryType = GetMemoryTypeText(CInt(module("MemoryType")))
            Dim formFactor = GetFormFactorText(CInt(module("FormFactor")))

            Console.WriteLine($"插槽位置:{module("DeviceLocator")}")
            Console.WriteLine($"容量:{capacity}")
            Console.WriteLine($"制造商:{manufacturer}")
            Console.WriteLine($"部件号:{partNumber}")
            Console.WriteLine($"频率:{speed}")
            Console.WriteLine($"内存类型:{memoryType}")
            Console.WriteLine($"外形规格:{formFactor}")
            Console.WriteLine("-----------------------------")
        Next

        ' 快速获取总物理内存(近似值)
        Dim totalMemGB = Math.Round(Environment.TotalPhysicalMemory / 1024 / 1024 / 1024, 2)
        Console.WriteLine($"系统总物理内存:{totalMemGB} GB")
    End Sub

    ' 把WMI返回的内存类型代码转成可读文本
    Private Function GetMemoryTypeText(typeCode As Integer) As String
        Return typeCode.ToString() switch {
            20 -> "DDR",
            21 -> "DDR2",
            24 -> "DDR3",
            26 -> "DDR4",
            17 -> "SDRAM",
            Else -> $"未知类型(代码:{typeCode})"
        }
    End Function

    ' 把外形规格代码转成可读文本
    Private Function GetFormFactorText(factorCode As Integer) As String
        Return factorCode.ToString() switch {
            8 -> "DIMM",
            12 -> "SODIMM",
            28 -> "DDR4 DIMM",
            29 -> "DDR4 SODIMM",
            Else -> $"未知规格(代码:{factorCode})"
        }
    End Function
End Module

关键说明

  • Win32_PhysicalMemory类:这是WMI里专门存储物理内存模块信息的类,除了代码里用到的属性,还有比如SerialNumber(序列号)、ConfiguredVoltage(电压)等属性,你可以根据需求添加。
  • 空值处理:有些老内存或者系统可能不会返回某些属性值,所以一定要用If判断避免空引用异常。
  • 权限问题:如果运行程序后看不到内存数据,试试右键程序→「以管理员身份运行」,部分WMI查询需要管理员权限才能访问完整信息。

内容的提问来源于stack exchange,提问作者Daniel Stone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:49:21