vba 通过 Visual Basic 提取硬件序列号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21503405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:41:20  来源:igfitidea点击:

Pulling Hardware Serial Number via Visual Basic?

vb.netvisual-studio-2010vbavb6hardware

提问by Aaron Brewer

I am writing an application in Visual Basic that pulls basic information about the computer and outputs the data onto a form. Currently, I am trying to pull the serial number for the machine I would be using. For example, pulling a serial number of a laptop from the BIOS. I have looked around the internet and haven't really found how to do this in Visual Basic without using WMI or C. Is there a way to do this in Visual Basic?

我正在用 Visual Basic 编写一个应用程序,它提取有关计算机的基本信息并将数据输出到表单上。目前,我正在尝试提取我将使用的机器的序列号。例如,从 BIOS 中提取笔记本电脑的序列号。我已经环顾互联网,并没有真正找到如何在不使用 WMI 或 C 的情况下在 Visual Basic 中执行此操作。有没有办法在 Visual Basic 中执行此操作?

Below is what I have currently in the form, so you can get an idea of what I am trying to do:

以下是我目前在表格中的内容,因此您可以了解我正在尝试做什么:

        TextBoxComputerName.Text = Environment.MachineName
        TextBoxOSVersion.Text = System.Environment.OSVersion.ToString
        TextBoxOSFullName.Text = My.Computer.Info.OSFullName
        TextBoxCurrentUser.Text = System.Environment.UserName
        TextBox64Bit.Text = System.Environment.Is64BitOperatingSystem
        TextBoxSystemDirectory.Text = System.Environment.SystemDirectory
        TextBoxDomain.Text = System.Environment.UserDomainName
        ' CHECK SERIAL NUMBER HERE.

Thank you all so much!

非常感谢大家!

回答by ???ěxě?

This will work for you just great! First add reference to System.Management and then make sure to import it at the top of your class as well. I did this on a form load event, but you can put it anywhere...

这对你很有用!首先添加对 System.Management 的引用,然后确保在类的顶部也将其导入。我在表单加载事件上做了这个,但你可以把它放在任何地方......

  Imports System.Management

  Dim q As New SelectQuery("Win32_bios")
  Dim search As New ManagementObjectSearcher(q)
  Dim info As New ManagementObject

  For Each info In search.Get
    MessageBox.Show("Serial Number: " & info("serialnumber").ToString & vbNewLine & vbNewLine & "Bios Version: " & info("version").ToString)
  Next

You can declare a string first if you would like and then set it to: info("serialnumber").ToString and the set that to you txtSerial.Text = your declared string

如果您愿意,您可以先声明一个字符串,然后将其设置为: info("serialnumber").ToString 并将其设置为您 txtSerial.Text = 您声明的字符串

Here is what I get...

这是我得到的...

My outcome

我的结果

回答by David Candy

This is VBScript but should be pastable into VB6.

这是 VBScript 但应该可以粘贴到 VB6 中。

You do know this field is blank on many computers?

你知道这个字段在很多计算机上都是空白的吗?

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery("Select * From Win32_BIOS")

For Each objItem in colItems
    msgbox objItem.SerialNumber
Next

From a command prompt (but I don't think home editions get the console program wmic)

从命令提示符(但我认为家庭版没有控制台程序 wmic)

wmic bios get /format:list

or

或者

wmic bios get serialnumber /format:list

回答by panpernicek

Try to use Treek's Licecnsing Library. It has class for generating hardware serial.

尝试使用 Treek 的 Licensing 库。它具有用于生成硬件串行的类。

http://msdn.treekslicensinglibrary.com/html/f2bfa10c-d5d9-25ac-39c2-46e2393c0fbe.htm

http://msdn.treekslicensinglibrary.com/html/f2bfa10c-d5d9-25ac-39c2-46e2393c0fbe.htm

回答by wallyeye

I found a way to get to this a bit backwards in VBA, using the FileSystemObject. You will need to set a reference to the Windows Scripting Runtime.

我找到了一种在 VBA 中稍微倒退的方法,使用FileSystemObject. 您将需要设置对 Windows 脚本运行时的引用。

Option Explicit

Public Sub GetHDSerial()

    Dim objFSO          As FileSystemObject
    Dim objFolder       As Folder

    Dim strComputer     As String

    strComputer = "myComputer"
    Set objFSO = New FileSystemObject
    Set objFolder = objFSO.GetFolder("\" & strComputer & "\c$")
    Debug.Print Hex(objFolder.Drive.SerialNumber)

    Set objFSO = Nothing
    Set objFolder = Nothing

End Sub

This does not account for multiple physical drives, which wasn't a problem in my environment.

这不考虑多个物理驱动器,这在我的环境中不是问题。