Saturday, February 04, 2006

My Namespace:New feature in VB.NET 2005

The My namespace in Visual Basic 2005 provides a quick and easy method for accessing some of the deeper areas of functionality in the .NET Framework without preventing you from using the Framework directly in any way you choose.My exposes several distinct classes which organize a variety of functions into one of seven general areas: My.Application, My.Computer, My.Forms, My.Resources, My.Settings, My.User, and My.WebServices.In general, the classes within My provide easy access to information in one of two categories, either the underlying .NET Framework or elements of the current project. My.Application, My.Computer, and My.User are all focused on Framework functionality, while My.Forms, My.Resources, My.Settings, and My.WebServices all deal with the contents of your current project. As I walk through each of these classes, starting with those that are focused on the underlying Framework.

Example :

The My.Computer.FileSystem classes provide a very simple API for working with and inquiring about files; this should banish any lingering yearning for the FileSystemObject library. The following code uses the FileSystem class to copy all of the pictures from the current user's My Pictures folder to a new folder (C:\Desktop Wallpaper), displaying a progress bar if the file copy takes more than a few moments to complete:

Dim myPics As String = My.Computer.FileSystem.SpecialDirectories.MyPictures
My.Computer.FileSystem .CopyDirectory( _
myPics, "C:\Desktop Wallpaper", _
FileIO.UIOption.AllDialogs, _
FileIO.UICancelOption.DoNothing)