Archive

Posts Tagged ‘.net’

WMI in .Net

It’s quite easy to work with WMI in .net. Follow these steps to get details of Win32_VideoController.

Namespace to use is System.Management.

1. Create a management class object.

Dim MngClass As New ManagementClass("Win32_VideoController")

2. Create a management object class collection instance and fill out this collection likewise…

Dim MngObjCollection As ManagementObjectCollection = MngClass.GetInstances

' Now we need to iterate through this collection object likewise...
For Each MngObj As ManagementObject In MngObjCollection
    Dim PropCollection As PropertyDataCollection = MngObj.Properties
    For Each PropData As PropertyData In PropCollection
       ' Read in properties of video controller.
    Next PropData
Next MngObj

Well we are done, quite easy isn’t it? Do this in C++ and you will be breaking your head and fingers.

Change the name of the above WMI class to something else and it will work in the same way except for properties of the WMI class object since each class has different properties.

Look up MSDN for more details on the properties of WMI classes. As a homework try fetching properties of “Win32_Processor”.

Download a sample application which extensively uses WMI to fetch details of remote machines. There are two exe’s and one dll. Put them in the same directory and run both exe’s. The one with a blue icon is the client application and the one with a red icon is the server application. You can run these applications locally or as remote ones putting the client application in a remote machine. See the power of WMI in full flow.

netstat in .net

netstat is a cool tool to view all active tcpip/udp connections in our computer along with the names of applications who have opened these ports/connections.

So as a demo open up command prompt and type

netstat /a
netstat /b -> displays exes which are using a particular port/connection
netstat /? -> displays helps for this command

I would have a given a screen shot here but for security reasons.

So in MSDN there is an equivalent tool written in .net (cool). You can download it from here, works well!

Categories: .net, C#, VB Tags: , , , ,

StringBuilder

One of the classes in .Net that I like is the StringBuilder class. So what is it? It’s like the StringBuffer class in java.

It’s a mutable string class which does faster operation while concatenation and other string related functions because internally it doesn’t recreate String objects but maintains a big enough buffer to prevent frequent re-allocations to manage concatenations.

I remember using String for concatenation in one of my .Net projects while I started programming in .Net. I knew there should be something like StringBuffer in Java. So while I was performance testing my application I found my application to be real slow. Main reason for this performance hit was String. I was “misusing” this class and doing concatenations with it. I replaced such calls with equivalent StringBuilder calls. Improvement in performance was just mind blowing phew about 20 times faster.

This class is in the System.Text namespace.

An e.g. code snippet in VB.net

Dim CIStr As New System.Text.StringBuilder

' Heavy operations
CIStr.Append( SomeString1 )
CIStr.Append( SomeString2 )
CIStr.Append( SomeString3 )
CIStr.Append( SomeString4 )
CIStr.Append( SomeString5 )

'Use ToString to get internal string
CIStr.ToString()
%d bloggers like this: