Visual Studio Hacks [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Visual Studio Hacks [Electronic resources] - نسخه متنی

Andrew Lockhart

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Hack 76. Enumerate Processes, Drives, Shares, and More

Using WMI (Windows Management Instrumentation),
you can discover information about a system's
drives, processes, printers, file shares, and much more. Install and
use the WMI extensions for the Visual Studio Server
Explorer.

To access WMI from the Server Explorer,
you will first need to download and install the WMI extensions for
Server Explorer from http://msdn.microsoft.com/library/default.asp?url=/downloads/list/wmi.asp.
Versions are currently available for Visual Studio .NET 2002 and
Visual Studio .NET 2003.


You will need to have the appropriate permission on either your local
machine or the remote server to access WMI classes. The permissions
you need vary based on the WMI class you are trying to access.

After you have installed these extensions, you will see two new
nodes, Management Classes and Management Events, in the Server
Explorer, as seen in Figure 9-24.


Figure 9-24. Server ExplorerWMI nodes


9.5.1. Adding Classes


You
can also see in Figure 9-24 a long list of the things you can interface
with through WMI. You can add even more classes by right-clicking on
the Management Classes node and choosing Add
Classes. You will see the dialog shown in Figure 9-25.


Figure 9-25. Add Classes dialog

From this dialog, you can select any other classes that you want. All
you have to do is select the class from the Available Classes list
and then click the Add button to move it to the Selected Classes
side. When you click OK, the new class will be shown in the Server
Explorer.


9.5.2. Creating Managed Classes


The real benefit of having WMI classes in
the Server Explorer is that you can then create strongly typed
managed classes to interact with these WMI classes. Using WMI from
.NET is not always easy or intuitive, but using a couple of functions
available through the Server Explorer makes it much easier. To create
a managed class for a WMI class, you simply need to right-click on
the class and click Generate Managed Class, as shown in Figure 9-26.


Figure 9-26. Choosing Generate Managed Class

After you click Generate Managed Class, a new class will be added to
your project. In the case of processes, the name of the class will be
Win32_Process.CS.
This file is a complete managed wrapper for the
Process
WMI class, including all methods, properties, and even custom
collections.

You can then use this class through your code to directly interface
with WMI. Here is an example of getting a list of all the processes currently
running on a machine:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
//The namespace that the Server Explorer added to the project
using ServerExplorerExamples.ROOT.CIMV2;
namespace ServerExplorerExamples
{
public class WMIExample : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView processView;
private System.Windows.Forms.ColumnHeader ProcessName;
private System.ComponentModel.Container components = null;
public WMIExample( )
{
InitializeComponent( );
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose( );
}
}
base.Dispose( disposing );
}
private void InitializeComponent( )
{
this.processView = new System.Windows.Forms.ListView( );
this.ProcessName =
new System.Windows.Forms.ColumnHeader( );
this.SuspendLayout( );
this.processView.Columns.AddRange(
new System.Windows.Forms.ColumnHeader[ ] {
this.ProcessName});
this.processView.Location =
new System.Drawing.Point(8, 8);
this.processView.Name = "processView";
this.processView.Size =
new System.Drawing.Size(240, 160);
this.processView.TabIndex = 0;
this.processView.View =
System.Windows.Forms.View.Details;
this.ProcessName.Text = "Process Name";
this.ProcessName.Width = 236;
this.AutoScaleBaseSize =
new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.processView);
this.Name = "WMIExample";
this.Text = "WMIExample";
this.Load +=
new System.EventHandler(this.WMIExample_Load);
this.ResumeLayout(false);
}
private void WMIExample_Load(
object sender,
System.EventArgs e)
{
//Loop through the ProcessCollection and add a row
//to the processView listview
foreach(Process p in Process.GetInstances( ))
{
processView.Items.Add(p.Name);
}
}
}
}

The preceding code is a simple form that will loop through the
process class and for each process add an item to the
ListView. When this code is run, you will see a
form like the one shown in Figure 9-27.


Figure 9-27. List of processes currently running

You could go on to include more information about each of the
processes in the ListView or let the user select a
process and see more information about that process. Using the
methods available on the Process class, you could also allow the user
to set the priority of the process, set the owner, or terminate the
process.


You may have noticed that there is also a set of Server Explorer
extensions for Active Directory. While these extensions exist, they
will work only with Visual Studio .NET 2002 and only if Visual Studio
.NET 2003 is not installed on the same machine.
For that reason they will not be covered in this book.

This is just a small sample of what can be done using WMI with the
Server Explorer. If you find yourself with the need to work with WMI,
you should definitely make use of the WMI extensions. They make what
would otherwise be a tedious task quite easy.


/ 172