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

如何用代码获取远程电脑的Windows版本(VS2013 VB开发)

获取远程Windows版本的VB实现方案

嗨,针对你用Visual Studio 2013 + VB开发局域网IP扫描工具的需求,我给你整理两个在局域网环境下可行的实现方法,都是实际项目里常用的:

方法一:WMI查询(推荐)

WMI(Windows Management Instrumentation)是Windows系统自带的管理接口,能很方便地获取远程机器的系统信息,这也是最通用的方案。不过需要满足几个前提:

  • 远程机器开启了Windows Management Instrumentation服务
  • 防火墙允许WMI连接(默认开放135端口及动态RPC端口)
  • 你有远程机器的管理员权限(或者至少有WMI查询权限)

下面是VB.NET的代码示例,记得先在项目里引用System.Management程序集:

Imports System.Management

Public Function GetRemoteWindowsVersion(ByVal machineIdentifier As String) As String
    Try
        ' 设置连接选项
        Dim connOptions As New ConnectionOptions()
        connOptions.Impersonation = ImpersonationLevel.Impersonate
        connOptions.Authentication = AuthenticationLevel.Default
        ' 可以在这里指定远程机器的用户名密码,如果当前账号没有权限的话
        ' connOptions.Username = "admin"
        ' connOptions.Password = "password"

        ' 创建管理范围
        Dim scope As New ManagementScope($"\{machineIdentifier}\root\cimv2", connOptions)
        scope.Connect()

        ' 查询操作系统信息
        Dim query As New ObjectQuery("SELECT Caption, Version FROM Win32_OperatingSystem")
        Dim searcher As New ManagementObjectSearcher(scope, query)
        Dim queryCollection As ManagementObjectCollection = searcher.Get()

        For Each mObj As ManagementObject In queryCollection
            ' Caption是友好名称,比如"Microsoft Windows 7 Professional"
            Dim osName As String = mObj("Caption").ToString().Trim()
            ' Version是版本号,比如6.1.7601对应Win7 SP1
            Dim osVersion As String = mObj("Version").ToString().Trim()
            Return $"系统版本:{osName} ({osVersion})"
        Next
    Catch ex As Exception
        Return $"查询失败:{ex.Message}"
    End Try
    Return "未找到系统信息"
End Function

你可以直接调用这个方法,传入IP地址或者计算机名称,就能返回对应的Windows版本信息。如果需要把版本号映射成更简洁的名称(比如把6.1映射成Win7),可以自己加个小的映射逻辑:

  • 5.0 → Windows 2000
  • 5.1 → Windows XP
  • 6.0 → Windows Vista/Server 2008
  • 6.1 → Windows 7/Server 2008 R2
  • 6.2 → Windows 8/Server 2012
  • 6.3 → Windows 8.1/Server 2012 R2
  • 10.0 → Windows 10/11/Server 2016及以后

方法二:远程注册表查询(备选)

如果WMI被限制了,还可以尝试通过远程注册表来获取信息,但这个要求远程机器开启了Remote Registry服务,而且权限要求更高。代码示例如下:

Imports Microsoft.Win32

Public Function GetRemoteWindowsVersionViaRegistry(ByVal machineIdentifier As String) As String
    Try
        ' 打开远程机器的HKEY_LOCAL_MACHINE根键
        Using remoteKey As RegistryKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineIdentifier)
            ' 定位到系统版本信息的注册表路径
            Using versionKey As RegistryKey = remoteKey.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion")
                If versionKey IsNot Nothing Then
                    Dim productName As String = versionKey.GetValue("ProductName").ToString().Trim()
                    Dim currentVersion As String = versionKey.GetValue("CurrentVersion").ToString().Trim()
                    Return $"系统版本:{productName} ({currentVersion})"
                End If
            End Using
        End Using
    Catch ex As Exception
        Return $"查询失败:{ex.Message}"
    End Try
    Return "未找到系统信息"
End Function

注意事项

  • 权限问题:很多局域网内的普通用户账号没有远程WMI或注册表的访问权限,建议提示用户使用管理员账号,或者提前在远程机器上配置好权限。
  • 异常处理:一定要加上异常捕获,因为网络波动、服务未开启、权限不足等情况都可能导致查询失败,给用户清晰的错误提示会更友好。
  • 防火墙配置:如果查询失败,先检查远程机器的防火墙是否允许对应的服务连接,WMI需要开放135端口和RPC动态端口,远程注册表需要开放445端口。

内容的提问来源于stack exchange,提问作者E R

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:26:11