Recipe 2.15. Getting System Information


You've heard that .NET provides powerful access to the most essential features of Windows, but the Framework Class Library is huge, and you're not sure where to find the total amount of memory installed on the local system. How can you get system-specific information quickly and easily?


Use the various objects and members of Visual Basic's My namespace. Microsoft introduced this new feature in the 2005 release of the language.


The My namespace was added to Visual Basic to help restore some of the simplicity and accessibility of Visual Basic that was lost in its transition to .NET.

For example, to determine the amount of installed memory on the local system, use the following statement:

 Dim installedMemory As Long = _    My.Computer.Info.TotalPhysicalMemory 

Another useful source of system settings is the System.Windows.Forms. SystemInformation object, which has dozens of informative members.

The My hierarchy makes an incredible number of features available in one easy-to-access place. Table 2-1 includes a small sampling of the information you can obtain from the My namespace.

Table 2-1. A sampling of My features

If you need to access this information…

…use this My namespace member

The command-line arguments used to start the program


The application version number


The set of all forms currently open


Features to read and write clipboard data


The current value of the system timer


The current directory recognized by the application


The location of the user's "My Documents" directory


The directory used to store the user's temporary files


The version of the operating system


Total installed memory


Whether the user's mouse has a scroll wheel installed


The assigned name of the computer


Whether access to Internet or the local network is enabled


The assigned name of the current Windows user


The My namespace collects some of the most useful and (sometimes) complex areas of the .NET Framework Class Libraries and makes them available in a simpler and more ordered format. The My keyword, a new 2005 feature specific to Visual Basic, is at the top of a hierarchy of features that are organized much like .NET namespaces. Each major object node within the hierarchy implements various properties and methods that are relevant to the specific object. In most cases, each method or property can be found somewhere else in the large set of .NET classes, although it may take several source code steps to obtain the information you need from that distant member.

The following list of objects summarizes the features exposed through the My hierarchy:


Provides access to application- and instance-specific settings, such as commandline arguments, and in-effect regional and language settings.


Reports details about the active executing assembly, including its title and version number.


Provides access to trace recording and logging features for desktop and console applications (not ASP.NET).


Exposes information pertaining to the computer running the application. Most of the members of this object are other subordinate objects with their own methods and properties.


Enables you to play system and file-based sounds.


Provides access to features that let you place data on the system clipboard and retrieve data back from that same clipboard in a variety of common and custom data formats.


Provides access to the current system time in a standard or local format.


Provides access to various features that let you manage files and directories on local and remote file systems and that manipulate path strings.


Reports locations of the various special directories, such as the "My Documents" directory.


Reveals information about the local operating system and memory usage.


Reports the current state of the keyboard.


Reports various properties of the installed mouse.


Reports availability of the network connection on the current workstation, and provides features that let you transfer data over that network.


Provides access to the system serial ports.


Provides features that let you manage the keys and values within the Windows registry.


Provides access to all forms defined in the active Windows Forms application.


Provides access to trace recording and logging features for ASP.NET applications only.


Replicates the Active Server Pages "Request" object within an ASP.NET application.


Provides access to application-specific resources, including string, graphic, and binary resource data.


Replicates the Active Server Pages "Response" object within an ASP.NET application.


Provides access to system- and user-focused configuration settings used by the application and automatically stored in application-specific XML configuration files.


Contains authentication and identity details gathered about the current user, either through Windows authentication or some other authentication scheme.


Contains a collection of all XML web services known to the active application. This object is not available in ASP.NET applications; it is only used by Windows Forms, console, and other non-web application types.

See Also

For a full reference of the objects and members included in the My hierarchy, see Visual Basic 2005 in a Nutshell by Tim Patrick, Steven Roman, Ron Petrusha, and Paul Lomaxone, one of O'Reilly's reference works focused on the Visual Basic language.

All members of the My namespace hierarchy are fully documented in the MSDN documentation included with Visual Studio.

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: