(Print this page)

Computers inventory from a .Net application
Published date: Sunday, June 26, 2011
On: Moer and Éric Moreau's web site

Lately, I was asked by a client if it was possible to create a kind of inventory of all computers on the network (all of these computers are already running an in-house application). I replied that nothing was impossible! Isn’t it true? This client doesn’t have one of these applications that can automatically do it for you.

He was mostly interested by CPU, the RAM, the hard disk, Windows version, and Office version. I started looking around and found many bits of information here and there but nothing complete.

By using the My namespace, the registry, WMI, and the folders system, I have been able to pull together all the info the client wanted (and more).

Downloadable demo

This month, the code I have created is in both VB and C#. The solution was created using Visual Studio 2010 but the very same code is working in Visual Studio 2005 and 2008.

The code is really straight forward. A bunch of computer properties and information are displayed into a listbox control.

Figure 1: The running demo application

A special reference

To be able to access some system information, I had to use WMI (Windows Management Instrumentation). To be able to access it, you need to add a reference to System.Management.

The My namespace

Visual Basic developers are very lucky to have a direct access to many computer’s properties using the My namespace. For example, My.Computer.Name gives you the name of the computer.

C# developers can also have access to these features. First they need to add a reference to the Microsoft.VisualBasic DLL. Second, they need to declare a variable like this:

Microsoft.VisualBasic.Devices.Computer myComputer = new Microsoft.VisualBasic.Devices.Computer();

Once it is done, you can simply use myComputer.Name to return exactly the same as in VB.

Retrieving information from the My namespace was the easiest part to get info like the name, the OS, the memory, and the screens.

WMI

WMI (Windows Management Instrumentation) is a set of extensions provided by the Windows OS which provides you a great deal of information on many components.

It is not easy to work with WMI classes as the object model is not unified.

For example, I have used WMI to find the fixed hard drive attached to the computer, their total and available size using this code:

Public Shared ReadOnly Property DiskSpace() As List(Of String)
    Get
        Dim arrInfo As New List(Of String)
        Dim Searcher As ManagementObjectSearcher
        Searcher = New ManagementObjectSearcher("SELECT * FROM Win32_LogicalDisk")
        For Each mgmtDisk As ManagementObject In Searcher.Get
            If Convert.ToString(mgmtDisk("MediaType")) = "12" Then
                arrInfo.Add(
                        mgmtDisk.Properties("DeviceID").Value.ToString +
                        " Size=" + readableLength(Convert.ToInt64(mgmtDisk.Properties("Size").Value)) +
                        " Free=" + readableLength(Convert.ToInt64(mgmtDisk.Properties("FreeSpace").Value)))
            End If
        Next

        Return arrInfo
    End Get
End Property

What this code does is to get a list of all logical disk and filter them for media type 12 (fixed hard disk). Once one is found, it checks the DeviceID (which returns something like C:), the Size and Free properties.

If you need more information on the Win32_LogicialDisk class, refer to http://msdn.microsoft.com/en-us/library/aa394173(v=vs.85).aspx.

Installed .Net Framework versions

I haven’t found an easy list of all .Net Framework versions installed. To get this list, we have to dig into the folders (normally available from C:\Windows\Microsoft.NET\Framework).

To get the list of all installed .Net Framework versions, check the “Framework” region from the cSystemInfo class.

Office applications version

This time, the required information was hidden into the registry! If you check the “Office” region of the cSystemInfo class, you will find the code to get the latest version of the major Office products that are installed. Notice that you will find an enumeration of office versions:

Enum Version
    Version95 = 7
    Version97 = 8
    Version2000 = 9
    Version2002 = 10
    Version2003 = 11
    Version2007 = 12
    Version2010 = 14
End Enum

Do you see what is missing here? 13. Apparently this number was skipped because Microsoft suffers Triskaidekaphobia.

Conclusion

This is not a complex mechanism to implement into your applications. If your system administrators don’t have an inventory containing this information, I would suggest that you start collecting this information into a database right now. When you will want to upgrade your old applications, you will be able to quickly find if the requirements are met by all computers.


(Print this page)