Section 2.2. Get Application Information


2.2. Get Application Information

The My.Application object provides a wealth of information right at your fingertips. Getting this information is as easy as retrieving a property.


Note: Using the My.Application object, you can get information about the current version of your application, where it's located, and what parameters were used to start it.

2.2.1. How do I do that?

The information in the My.Application object comes in handy in a variety of situations. Here are two examples:

  • You want to get the exact version number. This could be useful if you want to build a dynamic About box, or check with a web service to make sure you have the latest version of an assembly.

  • You want to record some diagnostic details. This becomes important if a problem is occurring at a client site and you need to log some general information about the application that's running.

To create a straightforward example, you can use the code in Example 2-1 in a console application. It retrieves all of these details and displays a complete report in a console window.

Example 2-1. Retrieving information from My.Application
' Find out what parameters were used to start the application. Console.Write("Command line parameters: ") For Each Arg As String In My.Application.CommandLineArgs     Console.Write(Arg & " ") Next Console.WriteLine( ) Console.WriteLine( )      ' Find out some information about the assembly where this code is located. ' This information comes from metadata (attributes in your code). Console.WriteLine("Company: " & My.Application.Info.CompanyName) Console.WriteLine("Description: " & My.Application.Info.Description) Console.WriteLine("Located in: " & My.Application.Info.DirectoryPath) Console.WriteLine("Copyright: " & My.Application.Info.Copyright) Console.WriteLine("Trademark: " & My.Application.Info.Trademark) Console.WriteLine("Name: " & My.Application.Info.AssemblyName) Console.WriteLine("Product: " & My.Application.Info.ProductName) Console.WriteLine("Title: " & My.Application.Info.Title) Console.WriteLine("Version: " & My.Application.Info.Version.ToString( )) Console.WriteLine( )


Tip: Visual Studio 2005 includes a Quick Console window that acts as a lightweight version of the normal command-line window. In some cases, this window is a little buggy. If you have trouble running a sample console application and seeing its output, just disable this feature. To do so, select Tools Options, make sure the "Show all settings" checkbox is checked, and select the Debugging General tab. Then turn off "Redirect all console output to the Quick Console window."

Before you test this code, it makes sense to set up your environment to ensure that you will see meaningful data. For example, you might want to tell Visual Studio to supply some command-line parameters when it launches the application. To do this, double-click the My Project icon in the Solution Explorer. Then, choose the Debug tab and look for the "Command line parameters" text box. For example, you could add three parameters by specifying the command line /a /b /c.

If you want to set information such as the assembly author, product, version, and so on, you need to add special attributes to the AssemblyInfo.vb file, which isn't shown in the Solution Explorer. To access it, you need to select Solution Show All Files. You'll find the AssemblyInfo.vb file under the My Projects node. Here's a typical set of tags that you might enter:

<Assembly: AssemblyVersion("1.0.0.0")>  <Assembly: AssemblyCompany("Prosetech")>  <Assembly: AssemblyDescription("Utility that tests My.Application")>  <Assembly: AssemblyCopyright("(C) Matthew MacDonald")>  <Assembly: AssemblyTrademark("(R) Prosetech")>  <Assembly: AssemblyTitle("Test App")>  <Assembly: AssemblyProduct("Test App")>

All of this information is embedded in your compiled assembly as metadata.

Now you can run the test application. Here's an example of the output you'll see:


Note: New in VB 2005 is the ability to add application information in a special dialog box. To use this feature, double-click the My Project item in the Solution Explorer, select the Assembly tab, and click the Assembly Information button.
Command line parameters: /a /b /c      Company: Prosetech Description: Utility that tests My.Application Located in: C:\Code\VBNotebook\1.08\ApplicationInfo\bin Copyright: (C) Matthew MacDonald Trademark: (R) Prosetech Name: ApplicationInfo.exe Product: Test App Title: Test App Version: 1.0.0.0

2.2.2. What about...

...getting more detailed diagnostic information? The My.Computer.Info object also provides a dash of diagnostic details with two useful properties. LoadedAssemblies provides a collection with all the assemblies that are currently loaded (and available to your application). You can also examine their version and publisher information. StackTrace provides a snapshot of the current stack, which reflects where you are in your code. For example, if your Main( ) method calls a method named A( ) that then calls method B( ), you'll see three of your methods on the stackB( ), A( ), and Main( )in reverse order.

Here's the code you can add to start looking at this information:

Console.WriteLine("Currently loaded assemblies") For Each Assm As System.Reflection.Assembly In _   My.Application.Info.LoadedAssemblies     Console.WriteLine(Assm.GetName( ).Name) Next Console.WriteLine( )      Console.WriteLine("Current stack trace: " & My.Application.Info.StackTrace) Console.WriteLine( )



Visual Basic 2005(c) A Developer's Notebook
Visual Basic 2005: A Developers Notebook
ISBN: 0596007264
EAN: 2147483647
Year: 2006
Pages: 123

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net