1. Home
  2. Computing & Technology
  3. Visual Basic

Retrieve, Change, or Create Program Information
Programming Visual Studio and WMI

By Dan Mabbutt, About.com

Jun 16 2007

Many programmers embed special debugging code into their programs that helps them quickly diagnose problems in Visual Studio. But this creates a requirement to disable this code when the program isn't running in Visual Studio. There are other ways to manage this problem and entire systems are available just for this. But if you just want to make a few checks inside your code, the Systems.Diagnostics.Debugger.IsAttached flag gives you the info you need.

Here's the code:

If System.Diagnostics.Debugger.IsAttached Then
   MsgBox("Running in Visual Studio!")
Else
   MsgBox("Running as an executable")
End If

You can see it running in the illustration below:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The #IF ... #THEN ... #ELSE conditional compilation statements don't do exactly the same thing. Note that a bin/Debug and a bin/Release directory both exist for your program. If you code:

#If DEBUG Then
   MsgBox("Conditional Compile: Running from Debug!")
#Else
   MsgBox("Conditional Compile: Running from Release")
#End If

The second MsgBox will only display when you run the EXE from the Release directory.

This article wouldn't be complete without mentioning some final, and somewhat low-level, sources of information about your computer: WMI (Windows Management Instrumentation) and ADSI (Active Directory Service Interfaces). Both WMI and ADSI are COM based and date back to much earlier versions of Windows even though they are still at the heart of Windows XP. Since these systems return the kinds of performance information that is normally of interest to system administrators rather than developers, the scripting interface - VBScript - remains a primary way that they're used even today. And that's the approach I took to writing an article about them a couple of years ago: VBScript - The System Administrator's Language - Part 2.

I should mention, however, that Microsoft has now provided the System.Management namespace which gives you managed, .NET access to WMI. But I'm already over my limit in this article so that will have to wait for another time!

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. Retrieve, Change, or Create Program Information

©2009 About.com, a part of The New York Times Company.

All rights reserved.