mardi, novembre 15, 2005

The My namespace

The My Namespace

Visual studio 2005 helps really people to be lazy, its simplicity returns the feasibility of some tasks very quikly, with only one line of code we do much things.

The My namespace includes the following classes, each of which includes a number of useful members:
- Application
- Computer
- Forms
- Resources
- Settings
- User

For example, to play an audio file in Visual Basic 2005, rather than using DirectX or Win32 API calls, you could write this simple single line of code :
My.Computer.Audio.Play("C:\Beep.wav")

Or, to play a system sound, you might write code like this : My.Computer.Audio.PlaySystemSound(SystemSounds.Asterisk)

In addition, My namespace supplies functionality that otherwise requires substantial code. For example, the following code demonstrates how you can use simple code to ping a computer :
If My.Computer.Network.IsAvailable Then
If My.Computer.Network.Ping("http://www.microsoft.com") Then
MsgBox("Microsoft's site is available.")
End If
End If

Working with the file system has never been easier. My.Computer.FileSystem provides a flat, stateless, discoverable, and easy-to-use way to perform common file system operations. File system operations such as copying, deleting, moving, and renaming files and folders are at the core of My.Computer.FileSystem. For example, developers frequently require file, folder, and drive properties (such as a file's size, encoding, and so on). Using My.Computer.FileSystem, commonly referenced file, folder, and drive properties can be obtained easily. For example, to display the size of each drive in your system, you might write code like the following:
For Each drv As DriveInfo In My.Computer.FileSystem.Drives
If drv.IsReady Then Debug.WriteLine(String.Format( _ "{0}:\ {1:N0}", drv.Name, drv.TotalSize))
End If
Next

The My.User class provides information about the current user, including Name and IsInRole properties. You could use code like the following to display the current user's information and whether the user is an administrator :
MsgBox(My.User.Identity.Name & ":" & _ My.User.IsInRole("Administrators"))

If you wanted to determine the current user's application data folder, you could use simple code like the following :
MsgBox( _ My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData)

The My namespace also adds functionality that returns many of the RAD features of Visual Basic 6 to the .NET development platform. For example, Visual Basic developers historically accessed forms by name, relying on the runtime engine to maintain a collection of all the available form classes. Using the new My.Forms collection, developers can write code to open and interact with an instance of a form class created as part of a solution, like the following :
My.Forms.HelpForm.Show()

In addition, the My namespace includes a number of other dynamically created collections, including Forms, WebServices, Resources, and Settings. The My.WebServices collection allows developers to easily call Web Service methods taking advantage of IntelliSense and strong typing, and makes the call simpler as well. For example, imagine a Web Service named Numbers that has already been declared in a project. Calling the NumToStr method of the Web Service couldn't get much easier than this :
MsgBox(My.WebServices.Numbers.NumToStr(123.45))

The My namespace has been devised to make it simpler for Visual Basic developers to accomplish their goals with less work, and with less searching through multiple namespaces. You'll find that many arduous tasks in previous releases are now only a single reference away.

From MSDN

Aucun commentaire: