My Links

Archives

Using "My" in C# (and other managed languages) (from: Gaurav Khanna)

VB 2005 is introducing the concept of My that allows the developer to write code which can get details pertaining to currently logged in user - for instance, the MyComputerInfo class allows you to get details like your machine's name, total physical and virtual memory, available memory, etc. Likewise, MyAudio is another class that provides you functionality to play audio on your system.

My does not give you any new functionality - it simply saves the developer from delving deep inside the FCL to get the same information which My will give you in a single namespace. However, this functionality is available to VB 2005. What about other managed languages? Can't they use it?

And the answer is yes! They can. All you have to do is set a reference to Microsoft.VisualBasic.dll assembly and use the Microsoft.VisualBasic.MyServices namespace. Below is sample C# source code that illustrates the use of My in C#:

#region Using directivesusing System;using System.Collections.Generic;using System.Text;using Microsoft.VisualBasic.MyServices; // this contains the "My" classes#endregionnamespace MyCSDemo{    class Program    {        static void Main(string[] args)        {            MyComputerInfo refMCInfo = new MyComputerInfo();            ulong ulMB = 1024*1024;            Console.WriteLine("Machine Name: {0}", refMCInfo.MachineName);            Console.WriteLine("Physical Memory: {0} MB", refMCInfo.TotalPhysicalMemory / ulMB);            Console.WriteLine("OS: {0} Version: {1}", refMCInfo.OSFullName, refMCInfo.OSVersion);        }    }}

posted on Wednesday, December 29, 2004 11:18 AM

sales@tourneylogic.com Copyright © Tourney Logic