Back to the Download Page
Torna alla Sezione Download
/*
* Created by SharpDevelop.
* User: Salvatore Meschini
* Date: 28/09/2004
* Time: 22.37
*
* (C# Source Code - .NET Framework) - WQL Explorer
* WQL is a SQL-like language useful for WMI queries, with this program you can
* test WQL queries pressing a button!
*/
using System;
using System.Windows.Forms;
using System.Text; // StringBuilder
using System.Management; // WMI
namespace DefaultNamespace
{
///
/// Description of MainForm.
///
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Label EntLabel;
private System.Windows.Forms.MainMenu FormMenu;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.RichTextBox ResultBox;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.Label CodeLabel;
private System.Windows.Forms.ListBox WMIEnt;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.Label ResultLabel;
private System.Windows.Forms.Button ExecuteBtn;
private System.Windows.Forms.RichTextBox WQLCodeBox;
private System.Windows.Forms.MenuItem menuItem10;
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());
}
void RunFromMenu(string Q)
{
WQLCodeBox.Text = Q;
ExecuteBtnClick(this, null);
}
#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.menuItem10 = new System.Windows.Forms.MenuItem();
this.WQLCodeBox = new System.Windows.Forms.RichTextBox();
this.ExecuteBtn = new System.Windows.Forms.Button();
this.ResultLabel = new System.Windows.Forms.Label();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.WMIEnt = new System.Windows.Forms.ListBox();
this.CodeLabel = new System.Windows.Forms.Label();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.ResultBox = new System.Windows.Forms.RichTextBox();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.FormMenu = new System.Windows.Forms.MainMenu();
this.EntLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// menuItem10
//
this.menuItem10.Index = 6;
this.menuItem10.Text = "Exit";
this.menuItem10.Click += new System.EventHandler(this.MenuItem10Click);
//
// WQLCodeBox
//
this.WQLCodeBox.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.WQLCodeBox.Location = new System.Drawing.Point(0, 24);
this.WQLCodeBox.Name = "WQLCodeBox";
this.WQLCodeBox.Size = new System.Drawing.Size(288, 93);
this.WQLCodeBox.TabIndex = 0;
this.WQLCodeBox.Text = "";
//
// ExecuteBtn
//
this.ExecuteBtn.Anchor = System.Windows.Forms.AnchorStyles.None;
this.ExecuteBtn.Location = new System.Drawing.Point(113, 128);
this.ExecuteBtn.Name = "ExecuteBtn";
this.ExecuteBtn.Size = new System.Drawing.Size(74, 29);
this.ExecuteBtn.TabIndex = 4;
this.ExecuteBtn.Text = "Execute";
this.ExecuteBtn.Click += new System.EventHandler(this.ExecuteBtnClick);
//
// ResultLabel
//
this.ResultLabel.Location = new System.Drawing.Point(0, 160);
this.ResultLabel.Name = "ResultLabel";
this.ResultLabel.Size = new System.Drawing.Size(100, 16);
this.ResultLabel.TabIndex = 3;
this.ResultLabel.Text = "Result:";
//
// menuItem8
//
this.menuItem8.Index = 1;
this.menuItem8.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem9});
this.menuItem8.Text = "Help";
//
// menuItem9
//
this.menuItem9.Index = 0;
this.menuItem9.Text = "About";
this.menuItem9.Click += new System.EventHandler(this.MenuItem9Click);
//
// WMIEnt
//
this.WMIEnt.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.WMIEnt.Location = new System.Drawing.Point(296, 24);
this.WMIEnt.Name = "WMIEnt";
this.WMIEnt.Size = new System.Drawing.Size(192, 134);
this.WMIEnt.Sorted = true;
this.WMIEnt.TabIndex = 6;
this.WMIEnt.SelectedValueChanged += new System.EventHandler(this.WMIEntSelectedValueChanged);
//
// CodeLabel
//
this.CodeLabel.Location = new System.Drawing.Point(0, 8);
this.CodeLabel.Name = "CodeLabel";
this.CodeLabel.Size = new System.Drawing.Size(100, 16);
this.CodeLabel.TabIndex = 1;
this.CodeLabel.Text = "WQL Code:";
//
// menuItem4
//
this.menuItem4.Index = 2;
this.menuItem4.Text = "Processor";
this.menuItem4.Click += new System.EventHandler(this.MenuItem4Click);
//
// menuItem5
//
this.menuItem5.Index = 3;
this.menuItem5.Text = "Operating system";
this.menuItem5.Click += new System.EventHandler(this.MenuItem5Click);
//
// menuItem6
//
this.menuItem6.Index = 4;
this.menuItem6.Text = "Running Services";
this.menuItem6.Click += new System.EventHandler(this.MenuItem6Click);
//
// ResultBox
//
this.ResultBox.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.ResultBox.Location = new System.Drawing.Point(0, 176);
this.ResultBox.Name = "ResultBox";
this.ResultBox.Size = new System.Drawing.Size(490, 165);
this.ResultBox.TabIndex = 2;
this.ResultBox.Text = "";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "Size and Freespace";
this.menuItem2.Click += new System.EventHandler(this.MenuItem2Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "Disk properties";
this.menuItem3.Click += new System.EventHandler(this.MenuItem3Click);
//
// menuItem7
//
this.menuItem7.Index = 5;
this.menuItem7.Text = "Environment";
this.menuItem7.Click += new System.EventHandler(this.MenuItem7Click);
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3,
this.menuItem4,
this.menuItem5,
this.menuItem6,
this.menuItem7,
this.menuItem10});
this.menuItem1.Text = "File";
//
// FormMenu
//
this.FormMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem8});
//
// EntLabel
//
this.EntLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.EntLabel.Location = new System.Drawing.Point(296, 8);
this.EntLabel.Name = "EntLabel";
this.EntLabel.Size = new System.Drawing.Size(164, 16);
this.EntLabel.TabIndex = 7;
this.EntLabel.Text = "Win32 WMI Entities:";
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(492, 343);
this.Controls.Add(this.EntLabel);
this.Controls.Add(this.WMIEnt);
this.Controls.Add(this.ExecuteBtn);
this.Controls.Add(this.ResultLabel);
this.Controls.Add(this.ResultBox);
this.Controls.Add(this.CodeLabel);
this.Controls.Add(this.WQLCodeBox);
this.MaximumSize = new System.Drawing.Size(500, 370);
this.Menu = this.FormMenu;
this.MinimumSize = new System.Drawing.Size(500, 370);
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "WQL Explorer - Salvatore Meschini";
this.Load += new System.EventHandler(this.MainFormLoad);
this.ResumeLayout(false);
}
#endregion
void ExecuteBtnClick(object sender, System.EventArgs e)
{
// User must specify a query in WQL language
if (WQLCodeBox.Text.Length != 0)
{
try
{
ResultBox.Clear(); // Throw away old results
SelectQuery MyQuery = new SelectQuery(WQLCodeBox.Text);
ManagementObjectSearcher Results = new ManagementObjectSearcher(MyQuery);
StringBuilder RS = new StringBuilder(); // Fast APPEND routine
// Iterate and add to RS (=ResultString)
foreach(ManagementObject R in Results.Get())
foreach(PropertyData Pta in R.Properties)
{
RS.Append(Pta.Name + " = " + Pta.Value + "\n");
}
ResultBox.Text = RS.ToString(); // Show the results
}
catch (Exception err)
{
ResultBox.Text = err.Message; // Error message
}
}
}
void MenuItem3Click(object sender, System.EventArgs e)
{
RunFromMenu("SELECT * FROM win32_LogicalDisk");
}
void MenuItem2Click(object sender, System.EventArgs e)
{
RunFromMenu("SELECT Caption, FreeSpace, Size, VolumeName FROM win32_LogicalDisk WHERE caption='c:'");
}
void MenuItem4Click(object sender, System.EventArgs e)
{
RunFromMenu("SELECT * FROM Win32_processor");
}
void MenuItem5Click(object sender, System.EventArgs e)
{
RunFromMenu("SELECT Caption, Version, CSDVersion, CSName FROM Win32_OperatingSystem");
}
void MenuItem6Click(object sender, System.EventArgs e)
{
RunFromMenu("SELECT name, description FROM Win32_Service WHERE state='Running'");
}
void MenuItem7Click(object sender, System.EventArgs e)
{
RunFromMenu("SELECT Caption, VariableValue FROM Win32_Environment");
}
void MenuItem9Click(object sender, System.EventArgs e)
{
MessageBox.Show("Salvatore Meschini\nsalvatoremeschini@tiscali.it\nhttp://smeschini.altervista.org", "About", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
void MenuItem10Click(object sender, System.EventArgs e)
{
Close(); // Bye bye!
}
void MainFormLoad(object sender, System.EventArgs e)
{
// System scanning: adds all management classes to a listbox
ManagementClass WMIRoot = new ManagementClass(); // Obtain the root
EnumerationOptions Opt = new EnumerationOptions();
Opt.EnumerateDeep = true; // Depth scan
WMIEnt.BeginUpdate(); // No flickering
foreach(ManagementObject Cls in WMIRoot.GetSubclasses(Opt))
{
if (Cls["__Class"].ToString().StartsWith("Win32"))
WMIEnt.Items.Add(Cls["__Class"]);
}
WMIEnt.EndUpdate();
}
void WMIEntSelectedValueChanged(object sender, System.EventArgs e)
{
if (WMIEnt.SelectedIndex != -1)
{
WQLCodeBox.Text = "SELECT * FROM "+WMIEnt.SelectedItem.ToString();
ExecuteBtnClick(this, null);
}
}
}
}