全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:8780
推到 Plurk!
推到 Facebook!

WMI VBScript 轉 VB.Net 範例(讀CPU資料及溫度)

 
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-03-19 23:05:48 IP:211.76.xxx.xxx 未訂閱
在此篇 【發表】StatPlus v2.2.4.1117 遠端信箱/信箱檢查修正版 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=59104 的最後有提到可以使用 Windows 系統的 WMI 來讀取硬體的一些資料, 而在網路上最容易找到的是 VBScript 還沒想到如何轉 BCB, 先用 VB.NET 試轉一下,才發現 VBScript 與 VB.NET 還是不能直接用的 < > 不過,總算是試出來了 < > 我是用 #Develop http://www.icsharpcode.net/opensource/sd/ 來開發的, 不想下載的,可以參考以下的原始碼:
'
' Created by SharpDevelop.
' User: Lee, Dong-Liang  http://dllee.ktop.com.tw
' Date: 2005/3/19
' Time: 09:50 PM
' 
' This is a test program for testing WMI objects in VB.NET in #Develop
'
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms    Namespace testScript
   
   Public Class MainForm
      Inherits System.Windows.Forms.Form
      Private label1 As System.Windows.Forms.Label
      Private button1 As System.Windows.Forms.Button
      
      Public Shared Sub Main
         Dim fMainForm As New MainForm
         fMainForm.ShowDialog()
      End Sub
      
      Public Sub New()
         MyBase.New
         '
         ' The Me.InitializeComponent call is required for Windows Forms designer support.
         '
         Me.InitializeComponent
         '
         ' TODO : Add constructor code after InitializeComponents
         '
      End Sub
      
      #Region " Windows Forms Designer generated code "
      ' This method is required for Windows Forms designer support.
      ' Do not change the method contents inside the source code editor. The Forms designer might
      ' not be able to load this method if it was changed manually.
      Private Sub InitializeComponent()
         Me.button1 = New System.Windows.Forms.Button
         Me.label1 = New System.Windows.Forms.Label
         Me.SuspendLayout
         '
         'button1
         '
         Me.button1.Location = New System.Drawing.Point(280, 152)
         Me.button1.Name = "button1"
         Me.button1.Size = New System.Drawing.Size(112, 48)
         Me.button1.TabIndex = 1
         Me.button1.Text = "Read Temperature"
         AddHandler Me.button1.Click, AddressOf Me.Button1Click
         '
         'label1
         '
         Me.label1.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.label1.Location = New System.Drawing.Point(8, 8)
         Me.label1.Name = "label1"
         Me.label1.Size = New System.Drawing.Size(392, 480)
         Me.label1.TabIndex = 0
         Me.label1.Text = "label1"
         '
         'MainForm
         '
         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
         Me.ClientSize = New System.Drawing.Size(408, 494)
         Me.Controls.Add(Me.button1)
         Me.Controls.Add(Me.label1)
         Me.Name = "MainForm"
         Me.Text = "WMI VB.NET testing http://dllee.ktop.com.tw"
         AddHandler Load, AddressOf Me.MainFormLoad
         Me.ResumeLayout(false)
      End Sub
      #End Region
      
      Private Sub MainFormLoad(sender As System.Object, e As System.EventArgs)
      ' ref: MicroSoft Script Center : Enumerating Processor Information
      ' ref: http://www.microsoft.com/technet/scriptcenter/scripts/hardware/basic/hwbavb03.mspx
         Dim p_objSet As Object
         Dim objItem As Object 
         p_objSet =GetObject("winmgmts:" _
             & "{impersonationLevel=impersonate}!\\.\root\cimv2"). _
             ExecQuery("Select * from Win32_Processor")
         For Each objItem in p_objSet
            label1.Text = "Address Width: " & objItem.AddressWidth & vbCrLf & _
                 "Architecture: " & objItem.Architecture & vbCrLf & _
                 "Availability: " & objItem.Availability & vbCrLf & _
                 "CPU Status: " & objItem.CpuStatus & vbCrLf & _
                 "Current Clock Speed: " & objItem.CurrentClockSpeed & vbCrLf & _
                 "Data Width: " & objItem.DataWidth & vbCrLf & _
                 "Description: " & objItem.Description & vbCrLf & _
                 "Device ID: " & objItem.DeviceID & vbCrLf & _
                 "External Clock: " & objItem.ExtClock & vbCrLf & _
                 "Family: " & objItem.Family & vbCrLf & _
                 "L2 Cache Size: " & objItem.L2CacheSize & vbCrLf & _
                 "L2 Cache Speed: " & objItem.L2CacheSpeed & vbCrLf & _
                 "Level: " & objItem.Level & vbCrLf & _
                 "Load Percentage: " & objItem.LoadPercentage & vbCrLf & _
                 "Manufacturer: " & objItem.Manufacturer & vbCrLf & _
                 "Maximum Clock Speed: " & objItem.MaxClockSpeed & vbCrLf & _
                 "Name: " & objItem.Name & vbCrLf & _
                 "PNP Device ID: " & objItem.PNPDeviceID & vbCrLf & _
                 "Processor ID: " & objItem.ProcessorId & vbCrLf & _
                 "Processor Type: " & objItem.ProcessorType & vbCrLf & _
                 "Revision: " & objItem.Revision & vbCrLf & _
                 "Role: " & objItem.Role & vbCrLf & _
                 "Socket Designation: " & objItem.SocketDesignation & vbCrLf & _
                 "Status Information: " & objItem.StatusInfo & vbCrLf & _
                 "Stepping: " & objItem.Stepping & vbCrLf & _
                 "Unique Id: " & objItem.UniqueId & vbCrLf & _
                 "Upgrade Method: " & objItem.UpgradeMethod & vbCrLf & _
                 "Version: " & objItem.Version & vbCrLf & _
                 "Voltage Caps: " & objItem.VoltageCaps
         Next
      End Sub
      
      Private Sub Button1Click(sender As System.Object, e As System.EventArgs)
      ' ref: Samurize - System Information - Scripts & Plugins
      ' ref: ACPI_CPUTemp by Punkt
      ' ref: http://www.samurize.com/modules/mydownloads/viewcat.php?cid=6
         Dim PFU As Object
         Dim U As Object 
                  
         PFU =GetObject("winmgmts:" _
             & "{impersonationLevel=impersonate}!\\.\root\wmi"). _
             ExecQuery("Select * from MSAcpi_ThermalZoneTemperature")
            For Each U in PFU
               label1.Text = "CurrentTemperature(RawData): " & U.CurrentTemperature & vbCrLf & _
                    "Temperature(C):     " & ((U.CurrentTemperature - 2732) / 10)      
            Next
        End Sub
      
   End Class
End Namespace
改天再來寫一個 C# 吧 < href="http://free.greenworld.com.tw/~dllee/" target="blank">吃軟也吃硬 dllee.ktop.com.tw StatPlus 系統資源監測器 @ KTOP OpenPLC - IEC 61131-3 geOShell XP Like 中文版
------
http://www.ViewMove.com
附加檔案:67256_testScript.zip
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-03-21 23:41:34 IP:211.76.xxx.xxx 未訂閱
C# 版本測試完成,還真是與 VB.NET 不一樣 < > 已更新下載檔案,不想下載請參考以下原始碼吧 < >
/*
 * Created by SharpDevelop.
 * User: Lee, Dong-Liang  http://dllee.ktop.com.tw
 * Date: 2005/3/21
 * Time: 10:13 PM
 *
 * This is a test program for testing WMI objects in VB.NET in #Develop
 * 
 * Ref: http://www.vbforums.com/showpost.php?p=1465671&postcount=1
 * Ref: http://dobon.net/vb/dotnet/system/wmi_os.html
 */
using System;
using System.Management;
using System.Drawing;
using System.Windows.Forms;    namespace testScriptInCS
{
        /// 
        /// Description of MainForm.
        /// 
        public class MainForm : System.Windows.Forms.Form
        {
                private System.Windows.Forms.Label label1;
                private System.Windows.Forms.Button button1;
                public MainForm()
                {
                        //
                        // The InitializeComponent() call is required for Windows Forms designer support.
                        //
                        InitializeComponent();
                        
                        //
                        // TODO: Add constructor code after the InitializeComponent() call.
                        //
                }
                
                [STAThread]
                public static void Main(string[] args)
                {
                        Application.Run(new MainForm());
                }
                
                #region Windows Forms Designer generated code
                /// 
                /// This method is required for Windows Forms designer support.
                /// Do not change the method contents inside the source code editor. The Forms designer might
                /// not be able to load this method if it was changed manually.
                /// 
                private void InitializeComponent() {
                        this.button1 = new System.Windows.Forms.Button();
                        this.label1 = new System.Windows.Forms.Label();
                        this.SuspendLayout();
                        // 
                        // button1
                        // 
                        this.button1.Location = new System.Drawing.Point(336, 216);
                        this.button1.Name = "button1";
                        this.button1.Size = new System.Drawing.Size(112, 48);
                        this.button1.TabIndex = 1;
                        this.button1.Text = "Read Temperature";
                        this.button1.Click += new System.EventHandler(this.Button1Click);
                        // 
                        // label1
                        // 
                        this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                                                | System.Windows.Forms.AnchorStyles.Left) 
                                                | System.Windows.Forms.AnchorStyles.Right)));
                        this.label1.Location = new System.Drawing.Point(8, 8);
                        this.label1.Name = "label1";
                        this.label1.Size = new System.Drawing.Size(464, 400);
                        this.label1.TabIndex = 0;
                        this.label1.Text = "label1";
                        // 
                        // MainForm
                        // 
                        this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
                        this.ClientSize = new System.Drawing.Size(480, 414);
                        this.Controls.Add(this.button1);
                        this.Controls.Add(this.label1);
                        this.Name = "MainForm";
                        this.Text = "WMI C# testing http://dllee.ktop.com.tw";
                        this.Load += new System.EventHandler(this.MainFormLoad);
                        this.ResumeLayout(false);
                }
                #endregion
                void MainFormLoad(object sender, System.EventArgs e)
                {
                        string strsearch=string.Format(@"Select * from Win32_Processor"); 
                         System.Management.ManagementObjectSearcher p_objSet=new ManagementObjectSearcher();
                        p_objSet.Scope=new ManagementScope(@"root\cimv2");
                        p_objSet.Query=new ObjectQuery(strsearch);
                        
                        if((p_objSet.Get().Count<1) || (p_objSet.Get()==null))
                        {
                                this.label1.Text="No Processor Information";
                        }
                        else
                        {
                                foreach(System.Management.ManagementObject objItem in p_objSet.Get())
                                {
                                        this.label1.Text=
                                                string.Format("Address Width: {0}\n",objItem["AddressWidth"].ToString())
                                        +        string.Format("Architecture: {0}\n",objItem["Architecture"].ToString())
                                        +        string.Format("Availability: {0}\n",objItem["Availability"].ToString())
                                        +        string.Format("CPU Status: {0}\n",objItem["CpuStatus"].ToString())
                                        +        string.Format("Current Clock Speed: {0}\n",objItem["CurrentClockSpeed"].ToString())
                                        +        string.Format("Data Width: {0}\n",objItem["DataWidth"].ToString())
                                        +        string.Format("Description: {0}\n",objItem["Description"].ToString())
                                        +        string.Format("Device ID: {0}\n",objItem["DeviceID"].ToString())
                                        +        string.Format("External Clock: {0}\n",objItem["ExtClock"].ToString())
                                        +        string.Format("Family: {0}\n",objItem["Family"].ToString())
                                        +        string.Format("L2 Cache Size: {0}\n",objItem["L2CacheSize"].ToString())
                                        // +        string.Format("L2 Cache Speed: {0}\n",objItem["L2CacheSpeed"].ToString())
                                        +        string.Format("Level: {0}\n",objItem["Level"].ToString())
                                        +        string.Format("Load Percentage: {0}\n",objItem["LoadPercentage"].ToString())
                                        +        string.Format("Manufacturer: {0}\n",objItem["Manufacturer"].ToString())
                                        +        string.Format("Maximum Clock Speed: {0}\n",objItem["MaxClockSpeed"].ToString())
                                        +        string.Format("Name: {0}\n",objItem["Name"].ToString())
                                        // +        string.Format("PNP Device ID: {0}\n",objItem["PNPDeviceID"].ToString())
                                        +        string.Format("Processor ID: {0}\n",objItem["ProcessorId"].ToString())
                                        +        string.Format("Processor Type: {0}\n",objItem["ProcessorType"].ToString())
                                        +        string.Format("Revision: {0}\n",objItem["Revision"].ToString())
                                        +        string.Format("Role: {0}\n",objItem["Role"].ToString())
                                        +        string.Format("Socket Designation: {0}\n",objItem["SocketDesignation"].ToString())
                                        +        string.Format("Status Information: {0}\n",objItem["StatusInfo"].ToString())
                                        +        string.Format("Stepping: {0}\n",objItem["Stepping"].ToString())
                                        // +        string.Format("Unique Id: {0}\n",objItem["UniqueId"].ToString())
                                        +        string.Format("Upgrade Method: {0}\n",objItem["UpgradeMethod"].ToString())
                                        +        string.Format("Version: {0}\n",objItem["Version"].ToString());
                                        // +        string.Format("Voltage Caps: {0}\n",objItem["VoltageCaps"].ToString());
                                }
                        }
                }
                
                void Button1Click(object sender, System.EventArgs e)
                {
                        string strsearch=string.Format(@"Select * from MSAcpi_ThermalZoneTemperature"); 
                         System.Management.ManagementObjectSearcher p_objSet=new ManagementObjectSearcher();
                        p_objSet.Scope=new ManagementScope(@"root\wmi");
                        p_objSet.Query=new ObjectQuery(strsearch);
                        
                        if((p_objSet.Get().Count<1) || (p_objSet.Get()==null))
                        {
                                this.label1.Text="Temperature Information";
                        }
                        else
                        {
                                foreach(System.Management.ManagementObject objItem in p_objSet.Get())
                                {
                                        int RawData=Convert.ToInt32(objItem["CurrentTemperature"]);
                                        this.label1.Text=
                                                string.Format("CurrentTemperature(RawData): {0}\n",RawData)
                                        +        string.Format("Temperature(C):              {0}\n",(RawData-2732)/10);
                                }
                        }
                }
                
        }
}
吃軟也吃硬 dllee.ktop.com.tw StatPlus 系統資源監測器 @ KTOP OpenPLC - IEC 61131-3 geOShell XP Like 中文版
------
http://www.ViewMove.com
qoo1234
版主


發表:256
回覆:1167
積分:659
註冊:2003-02-24

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-03-22 02:35:09 IP:220.131.xxx.xxx 未訂閱
其他WMI相關範例: http://www.csharphelp.com/archives3/archive585.html http://www.csharphelp.com/archives3/files/archive585/Code.zip 解壓縮後,使用#Develop 匯入專案即可編譯, Files-->Import Project (支援   1.Visual Studio.NET 7 / 2003 Solutions   2.Visual Studio.NET 7 / 2003 C# and VB.NET Projects  匯入到 #Develop ) 
 
網海無涯,唯學是岸! 找對焦點,就會產生方向^_^
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-03-22 09:27:57 IP:220.139.xxx.xxx 未訂閱
感謝 Qoo1234 版主提供的資料,昨天能寫完 C# 的版本,還是參考了您的大作 SharpDevelop Bible 才順利完成的,因為我自己試不出如何字串轉整數  昨天寫完再試一次溫度改變是否讀值改變,發現,在我的電腦( > 所以不是每一台電腦都可以用。 < href="http://free.greenworld.com.tw/~dllee/" target="blank">吃軟也吃硬 dllee.ktop.com.tw StatPlus 系統資源監測器 @ KTOP OpenPLC - IEC 61131-3 geOShell XP Like 中文版
------
http://www.ViewMove.com
qoo1234
版主


發表:256
回覆:1167
積分:659
註冊:2003-02-24

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-03-24 01:27:51 IP:220.131.xxx.xxx 未訂閱
上面兩個程式差在Microsoft.VisualBasic有專屬GetObject, C#不能直接用 < src="http://delphi.ktop.com.tw/download/upload\33223_qoo.gif">網海無涯,唯學是岸! 找對焦點,就會產生方向^_^
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-03-24 11:31:16 IP:220.139.xxx.xxx 未訂閱
引言: 上面兩個程式差在Microsoft.VisualBasic有專屬GetObject, C#不能直接用 < src="http://delphi.ktop.com.tw/download/upload\33223_qoo.gif">網海無涯,唯學是岸! 找對焦點,就會產生方向^_^
是呀,這也花了一些時間才 try 出 在用到 > 不論是 > < href="http://free.greenworld.com.tw/~dllee/" target="blank">吃軟也吃硬 dllee.ktop.com.tw StatPlus 系統資源監測器 @ KTOP OpenPLC - IEC 61131-3 geOShell XP Like 中文版
------
http://www.ViewMove.com
Skyer
高階會員


發表:43
回覆:111
積分:120
註冊:2002-04-04

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-03-26 12:01:22 IP:220.139.xxx.xxx 未訂閱
引言:
引言: 上面兩個程式差在Microsoft.VisualBasic有專屬GetObject, C#不能直接用 < src="http://delphi.ktop.com.tw/download/upload\33223_qoo.gif">網海無涯,唯學是岸! 找對焦點,就會產生方向^_^
是呀,這也花了一些時間才 try 出 在用到 > 不論是 > < face="Verdana, Arial, Helvetica"> 因為 Delphi 原開發者 (Delphi 1 ~ 3) 後來被 Microsoft 挖過去作 C# 為什麼可以在 C# 看到 Delphi 的影子就是如此 ^^ 我個人也很喜歡用 C# 寫程式 ^^ 題外話^^bb 為什麼 Delphi 的 Object 都是動態在 Heap 中 Create 的呢 ? 是因為原本 Andrew 打算為 Delphi 加上 Garbage Collect 功能.. 但後來不知什麼原因就沒作下去了.. 這點在 .NET Framework 被實作了.. -- Regards, Skyer 發表人 - Skyer 於 2005/03/26 12:07:11
------
--
Regards,
Skyer
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-03-27 22:44:08 IP:211.76.xxx.xxx 未訂閱
引言: 我個人也很喜歡用 C# 寫程式 ^^
我也喜歡玩 C#... 目前還不知道如何用 C# 寫較大型的程式,只在玩玩的階段 < href="http://free.greenworld.com.tw/~dllee/" target="blank">吃軟也吃硬 dllee.ktop.com.tw StatPlus 系統資源監測器 @ KTOP OpenPLC - IEC 61131-3 geOShell XP Like 中文版
------
http://www.ViewMove.com
LiYZ
一般會員


發表:0
回覆:1
積分:0
註冊:2006-08-17

發送簡訊給我
#9 引用回覆 回覆 發表時間:2006-08-17 15:21:10 IP:59.42.xxx.xxx 未訂閱

请问,怎样能够即时更新WMI取得的CPU温度啊,如果有人知道,希望能够尽快回复!

系統時間:2024-04-19 2:05:15
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!