WMI vb.net编码帮助 [英] Help with WMI vb.net coding

查看:82
本文介绍了WMI vb.net编码帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,正在从事一个旨在远程获取机器信息的项目.我在使用代码创建的文本框中获取机器名称时遇到了问题.我也有wmi部分的问题.

我的总体目标是单击一个文本框中的文本作为计算机名称,并提取特定的wmi信息.我现在只尝试使用os名称.

我的代码:

I am new to programming and i m working on a project to pull machine information remotly. I have been having issues with getting the machine name in the textbox i created work with the code. i m also having issues with the wmi part as well.

My over all goal is to have on button click the text from one textbox act as the computer name and pull specific wmi information. I have been trying just the os name for now.

my code:

  Private Sub Search_Button_Header_Click(sender As System.Object, e As System.EventArgs) Handles Search_Button_Header.Click
      strComputer = Me.Machine_Name_Textbox_Header.Text
      If strComputer = "" Then
          strComputer = "localhost"
      End If

      Dim scope As ManagementScope = _
          New ManagementScope("\\" & strComputer & "\root\cimv2")

      scope.Connect()

      Dim query As ObjectQuery = _
          New ObjectQuery("SELECT * FROM Win32_OperatingSystem")
      Dim mos As ManagementObjectSearcher = _
          New ManagementObjectSearcher(scope, query)
      Dim queryCollection As ManagementObjectCollection = mos.Get

      For Each mo As ManagementObject In queryCollection
          Dim OsName As String = mo("name")

          OSTextBox.Text = mos.Get("OsName")
      Next

End Sub


我搜索并发现其他人正在做相同的事情,但每个人都有不同的方式.任何帮助都将非常有用.


I have searched and found others that are doing the same thing but every one of them have different ways. Any help would be great.

推荐答案

尝试以下代码:
Try this code :
'To use this class you must add a reference
'to System.Management from the Project | References
'menu
Imports System.Management

Public Class clsWMI
    Private objOS As ManagementObjectSearcher
    Private objCS As ManagementObjectSearcher
    Private objMgmt As ManagementObject
    Private m_strComputerName As String
    Private m_strManufacturer As String
    Private m_StrModel As String
    Private m_strOSName As String
    Private m_strOSVersion As String
    Private m_strSystemType As String
    Private m_strTPM As String
    Private m_strWindowsDir As String
    
    Public Sub New()
        objOS = New ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem")
        objCS = New ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem")
        For Each objMgmt In objOS.Get
           
            m_strOSName = objMgmt("name").ToString()
            m_strOSVersion = objMgmt("version").ToString()
            m_strComputerName = objMgmt("csname").ToString()
            m_strWindowsDir = objMgmt("windowsdirectory").ToString()
        Next
        For Each objMgmt In objCS.Get
            m_strManufacturer = objMgmt("manufacturer").ToString()
            m_StrModel = objMgmt("model").ToString()
            m_strSystemType = objMgmt("systemtype").ToString
            m_strTPM = objMgmt("totalphysicalmemory").ToString()
        Next
    End Sub
    Public ReadOnly Property ComputerName()
        Get
            ComputerName = m_strComputerName
        End Get
    End Property
    Public ReadOnly Property Manufacturer()
        Get
            Manufacturer = m_strManufacturer
        End Get
    End Property
    Public ReadOnly Property Model()
        Get
            Model = m_StrModel
        End Get
    End Property
    Public ReadOnly Property OsName()
        Get
            OsName = m_strOSName
        End Get
    End Property
    Public ReadOnly Property OSVersion()
        Get
            OSVersion = m_strOSVersion
        End Get
    End Property
    Public ReadOnly Property SystemType()
        Get
            SystemType = m_strSystemType
        End Get
    End Property
    Public ReadOnly Property TotalPhysicalMemory()
        Get
            TotalPhysicalMemory = m_strTPM
        End Get
    End Property
    Public ReadOnly Property WindowsDirectory()
        Get
            WindowsDirectory = m_strWindowsDir
        End Get
    End Property
End Class


使用方法:


How to use :

 Dim objWMI As New clsWMI()
With objWMI
      Debug.WriteLine("Computer Name = " & .ComputerName)
      Debug.WriteLine("Computer Manufacturer = " & .Manufacturer)
      Debug.WriteLine("Computer Model = " & .Model)
      Debug.WriteLine("OS Name = " & .OsName)
      Debug.WriteLine("OS Version = " & .OSVersion)
      Debug.WriteLine("System Type = " & .SystemType)
      Debug.WriteLine("Total Physical Memory = " & .TotalPhysicalMemory)
      Debug.WriteLine("Windows Directory = " & .WindowsDirectory)
End With


希望它能解决您的问题.:)


I hope it will solve your problem.:)


这篇关于WMI vb.net编码帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆