The My Namespace and Assemblies

The 'My' Namespace and Assemblies

The .NET Framework, with its thousands of classes, contains a lot of packaged logic that I can use in my own programs. But I don't have all of the many assemblies and their classes memorized (yet), and it takes time to wander around the FCL documentation. With so many classes available, I sometimes shudder when I think of the effort it will take me to find just the right class or feature I need to accomplish some development task.

Fortunately, I'm not the only one who thinks this way; Microsoft agrees with me. Historically, Visual Basic programmers were sheltered from the complexities of Windows application development. Not that they needed to be; we all know that Visual Basic developers are generally a cut above the rest. But there was "the Visual Basic motto" to contend with: Make Windows Development Fast and Easy. And calling some esoteric method deep within the bowels of the System namespace just to get a minor piece of data is neither easy nor fast.

To bring back some semblance of the pleasant experience previously available in Visual Basic development, Microsoft introduced the My pretend namespace in its 2005 release of the language. The My pretend namespace collects a lot of useful features from all around the Framework Class Library, and organizes them in a much smaller hierarchy for simple and direct access. I briefly mentioned My in Chapter 1, but it's a good time to take a closer look at what it does.

The My pretend namespace looks a lot like other namespaces, such as System, System.Reflection, or System.Windows.Forms. But it's not really a namespaceit's pretend! For one thing, you can't use the Imports keyword to create a shortcut to branches within its hierarchy. Also, some sections of the hierarchy are dynamic; they change as your source code changes. Table 5-1 lists the major nodes of the hierarchy.

Table 5-1. Major Nodes in the My Namespace Hierarchy


Available Features


Information about the current application, including culture settings and the deployment method.


Further details about the application and its assembly, including the name and version.


Allows you to generate trace and logging output to registered logging destinations. Only used with client applications.


Provides access to general resources located on the local computer.


Plays named and system sounds through the computer's speakers.


Retrieves data from the system clipboard, and lets you add your own data to the clipboard in a variety of predefined and custom formats.


Gets the current system date and time dished up in a variety of ways.


Tools to examine and manipulate files and directories on local or networked file systems.


References to special Windows folders such as "My Documents," "Desktop," and "Temp."


Provides information about the installed operating system and other local system resources.


Exposes the current state of the keyboard and its keys.


Makes available a few properties of the local computer's mouse.


Reports on network availability, and provides features to interact with that network.


Lets you interact with the system's serial ports.


Reads and writes keys and values in the registry.


Presents a dynamic collection of all forms defined in the application. This node is only available in Windows Forms applications.


Allows you to generate trace and logging output to registered logging destinations. Only used with ASP.NET applications.


This object is similar to the older Active Server Pages Request object. It is only available in ASP.NET applications.


Provides dynamic access to application-specific or locale-specific resources included with the application.


This object is similar to the older Active Server Pages Response object. It is only available in ASP.NET applications.


Provides dynamic access to the new settings system included with Visual Basic 2005.


Identifies the current Windows user, including authentication information.


Presents a collection of available Web Services for use in the application. This node is not available in ASP.NET applications.

The My namespace includes a lot of features you will use regularly, including access to the version number of the application. Instead of typing System.Reflection.whatever to get to the version number's "major" component, you can now just type:


Need a list of assemblies, but you're too lazy to type the word "Reflection?" Try:


Need to know the time right now in England?


Can you communicate over the local area network?


Who is running this computer anyway?


There isn't much in the My namespace that you can't already do with standard FCL libraries. There are even a few parts of My that are repeats of features already included in the Visual Basic language, although with some enhancements. For instance, Visual Basic includes a Kill command that lets you delete files. The new My.Computer.FileSystem.DeleteFile method also removes files, but it includes new options, including one that lets you send the file to the Recycle Bin instead of just losing it forever.

Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application
Start-to-Finish Visual Basic 2005: Learn Visual Basic 2005 as You Design and Develop a Complete Application
ISBN: 0321398009
EAN: 2147483647
Year: 2006
Pages: 247
Authors: Tim Patrick © 2008-2017.
If you may any questions please contact us: