WMI VBScript 轉 VB.Net 範例(讀CPU資料及溫度) |
|
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
在此篇
【發表】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 發送簡訊給我 |
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 { ///吃軟也吃硬 dllee.ktop.com.tw StatPlus 系統資源監測器 @ KTOP OpenPLC - IEC 61131-3 geOShell XP Like 中文版
------
http://www.ViewMove.com |
qoo1234
版主 發表:256 回覆:1167 積分:659 註冊:2003-02-24 發送簡訊給我 |
其他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 發送簡訊給我 |
感謝 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 發送簡訊給我 |
|
dllee
站務副站長 發表:321 回覆:2519 積分:1711 註冊:2002-04-15 發送簡訊給我 |
引言: 上面兩個程式差在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 發送簡訊給我 |
引言:引言: 上面兩個程式差在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 發送簡訊給我 |
引言: 我個人也很喜歡用 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 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |