Recipe 2.15. Getting System Information


Problem

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?

Solution

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

Discussion

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

My.Application.CommandLineArgs

The application version number

My.Application.Info.Version

The set of all forms currently open

My.Application.OpenForms

Features to read and write clipboard data

My.Computer.Clipboard

The current value of the system timer

My.Computer.Clock.TickCount

The current directory recognized by the application

My.Computer.FileSystem.CurrentDirectory

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

My.Computer.FileSystem.SpecialDirectories.MyDocuments

The directory used to store the user's temporary files

My.Computer.FileSystem.SpecialDirectories.Temp

The version of the operating system

My.Computer.Info.OSVersion

Total installed memory

My.Computer.Info.TotalPhysicalMemory

Whether the user's mouse has a scroll wheel installed

My.Computer.Mouse.WheelExists

The assigned name of the computer

My.Computer.Name

Whether access to Internet or the local network is enabled

My.Computer.Network.IsAvailable

The assigned name of the current Windows user

My.User.Name


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:


My.Application

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


My.Application.Info

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


My.Application.Log

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


My.Computer

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.


My.Computer.Audio

Enables you to play system and file-based sounds.


My.Computer.Clipboard

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.


My.Computer.Clock

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


My.Computer.FileSystem

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


My.Computer.FileSystem.SpecialDirectories

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


My.Computer.Info

Reveals information about the local operating system and memory usage.


My.Computer.Keyboard

Reports the current state of the keyboard.


My.Computer.Mouse

Reports various properties of the installed mouse.


My.Computer.Network

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


My.Computer.Ports

Provides access to the system serial ports.


My.Computer.Registry

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


My.Forms

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


My.Log

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


My.Request

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


My.Resources

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


My.Response

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


My.Settings

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


My.User

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


My.WebServices

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

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