vb.net - 如何使用vb.net获取电池(笔记本电脑)信息?

标签 vb.net visual-studio-2010 visual-studio operating-system system

如何使用vb.net获取电池(笔记本电脑)信息? 我想获取有关电池的制造详细信息和完整详细信息:

  • 电池设备名称
  • 电池预计时间
  • 电池粒度信息
  • 电池信息
  • 电池制造日期
  • 电池制造商名称
  • 电池序列号
  • 电池温度
  • 电池唯一ID

最佳答案

您可以从 Win32_Battery 类中获取所有这些 - http://msdn.microsoft.com/en-us/library/windows/desktop/aa394074(v=vs.85).aspx

class Win32_Battery : CIM_Battery
{
  uint16   Availability;
  uint32   BatteryRechargeTime;
  uint16   BatteryStatus;
  string   Caption;
  uint16   Chemistry;
  uint32   ConfigManagerErrorCode;
  boolean  ConfigManagerUserConfig;
  string   CreationClassName;
  string   Description;
  uint32   DesignCapacity;
  uint64   DesignVoltage;
  string   DeviceID;
  boolean  ErrorCleared;
  string   ErrorDescription;
  uint16   EstimatedChargeRemaining;
  uint32   EstimatedRunTime;
  uint32   ExpectedBatteryLife;
  uint32   ExpectedLife;
  uint32   FullChargeCapacity;
  datetime InstallDate;
  uint32   LastErrorCode;
  uint32   MaxRechargeTime;
  string   Name;
  string   PNPDeviceID;
  uint16   PowerManagementCapabilities[];
  boolean  PowerManagementSupported;
  string   SmartBatteryVersion;
  string   Status;
  uint16   StatusInfo;
  string   SystemCreationClassName;
  string   SystemName;
  uint32   TimeOnBattery;
  uint32   TimeToFullCharge;
};

Win32_电池属性:http://include.wutils.com/wmi/ROOT%5Ccimv2/CIM_ManagedSystemElement/CIM_LogicalElement/CIM_LogicalDevice/CIM_Battery/Win32_Battery.html

电池序列号

 Dim results As String = ""
        Dim batteryReturn As Management.ManagementObjectCollection
        Dim batterySearch As Management.ManagementObjectSearcher
        batterySearch = New Management.ManagementObjectSearcher("root\cimv2", "Select * from Win32_Battery")
        batteryReturn = batterySearch.Get

        For Each baseobject In batteryReturn
            results += baseobject("DeviceID")
        Next

        Messagebox.show(results, "Battery serial:")

示例代码从这里 http://msdn.microsoft.com/en-us/library/system.windows.forms.systeminformation.powerstatus.aspx#Y0

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Reflection
Imports System.Windows.Forms

Public Class SystemInfoBrowserForm
    Inherits System.Windows.Forms.Form

    Private listBox1 As System.Windows.Forms.ListBox
    Private textBox1 As System.Windows.Forms.TextBox  

    Public Sub New()
        Me.SuspendLayout()
        InitForm()

        ' Add each property of the SystemInformation class to the list box. 
        Dim t As Type = GetType(System.Windows.Forms.SystemInformation)
        Dim pi As PropertyInfo() = t.GetProperties()
        Dim i As Integer 
        For i = 0 To pi.Length - 1
            listBox1.Items.Add(pi(i).Name)
        Next i
        textBox1.Text = "The SystemInformation class has " + pi.Length.ToString() + " properties." + ControlChars.CrLf

        ' Configure the list item selected handler for the list box to invoke a  
        ' method that displays the value of each property. 
        AddHandler listBox1.SelectedIndexChanged, AddressOf listBox1_SelectedIndexChanged

        Me.ResumeLayout(False)
    End Sub     

    Private Sub listBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
        ' Return if no list item is selected. 
        If listBox1.SelectedIndex = - 1 Then 
            Return 
        End If          
        ' Get the property name from the list item. 
        Dim propname As String = listBox1.Text

        If propname = "PowerStatus" Then 
            ' Cycle and display the values of each property of the PowerStatus property.
            textBox1.Text += ControlChars.CrLf + "The value of the PowerStatus property is:" 
            Dim t As Type = GetType(System.Windows.Forms.PowerStatus)
            Dim pi As PropertyInfo() = t.GetProperties()
            Dim i As Integer 
            For i = 0 To pi.Length - 1
                Dim propval As Object = pi(i).GetValue(SystemInformation.PowerStatus, Nothing)
                textBox1.Text += ControlChars.CrLf + "    PowerStatus." + pi(i).Name + " is: " + propval.ToString()
            Next i
        Else 
            ' Display the value of the selected property of the SystemInformation type. 
            Dim t As Type = GetType(System.Windows.Forms.SystemInformation)
            Dim pi As PropertyInfo() = t.GetProperties()
            Dim prop As PropertyInfo = Nothing 
            Dim i As Integer 
            For i = 0 To pi.Length - 1
                If pi(i).Name = propname Then
                    prop = pi(i)
                    Exit For 
                End If 
            Next i
            Dim propval As Object = prop.GetValue(Nothing, Nothing)
            textBox1.Text += ControlChars.CrLf + "The value of the " + propname + " property is: " + propval.ToString()
        End If 
    End Sub     

    Private Sub InitForm()
        ' Initialize the form settings 
        Me.listBox1 = New System.Windows.Forms.ListBox()
        Me.textBox1 = New System.Windows.Forms.TextBox()
        Me.listBox1.Anchor = CType(System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
        Me.listBox1.Location = New System.Drawing.Point(8, 16)
        Me.listBox1.Size = New System.Drawing.Size(172, 496)
        Me.listBox1.TabIndex = 0
        Me.textBox1.Anchor = CType(System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
        Me.textBox1.Location = New System.Drawing.Point(188, 16)
        Me.textBox1.Multiline = True 
        Me.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.textBox1.Size = New System.Drawing.Size(420, 496)
        Me.textBox1.TabIndex = 1
        Me.ClientSize = New System.Drawing.Size(616, 525)
        Me.Controls.Add(Me.textBox1)
        Me.Controls.Add(Me.listBox1)
        Me.Text = "Select a SystemInformation property to get the value of" 
    End Sub

    <STAThread()>  _
    Shared Sub Main()
        Application.Run(New SystemInfoBrowserForm())
    End Sub 

End Class

关于vb.net - 如何使用vb.net获取电池(笔记本电脑)信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12297301/

相关文章:

visual-studio-2010 - 是否可以在 Visual Studio 中重命名项目,使其文件夹名称也重命名?

c++ - 发送 WM_SETTEXT 时如何避免 EN_CHANGE 通知?

wpf - 名称 "YesNo"在命名空间 "clr-Namespace:WPF_Tutorial.WPFTutorials.Converter"中不存在

c# - 正确处理自动属性的 C# 到 VB.Net 的转换实用程序?

c# - 添加 Controller 时 Visual Studio 崩溃

c - 使用 fgets 只读取文件的一部分

c# - 使用 C# 和 .NET Framework 进行屏幕抓取、网页抓取、网页收集、网页数据提取等

asp.net - 错误 : String or binary data would be truncated. 语句已终止

vb.net - := mean in VB? 是什么

c++ - 在 VC++ 2010 中,项目依赖项并不意味着链接?