Visual Basic programmers have been keeping something from us: My Classes. The My Classes are some jolly useful methods that wrap various
However, rejoice, for we C# programmers can use them too. Here’s how:
- From the Solution Explorer, right click on the References node, and select Add Reference. When the dialog appears, scroll down and select Microsoft.VisualBasic.dll.
- In the Using section at the top of your program, add Using Microsoft.VisualBasic.MyServices.
- That’s it.
Now you can make use of the rather nifty My Classes that Visual Basic programmers have been raving about.
(Aside: Do Visual Basic programmers rave? I should ask the person responsible for creating their personas – the internal model we use at Microsoft for describing our customers. Haha! You should see what we think about you, gentle C# programmer. Don’t worry, I’m kidding. We respect you and think you’re awesome. After all, we’re all C# programmers too. Thinking about it, maybe our C# persona is how we all vicariously live our lives – so it’s up to you! Don’t let us down! You do go snowboarding and ride a motorbike, right? And eat pizza? And have a girlfriend/boyfriend? And you weigh about 180lbs, telephone your mother once a week, and cry at sad movies? Do you count in binary on your fingers when you sit on the bus, hoping the person sitting opposite will notice and think you're cool? [By the way: they won't and wouldn't] Good. Suffice to say we have a lot of information about you. I’d say more, but already the MS SpyBot is hovering in the corridor outside my office…)
Anyway, here’s some sample C# code that uses these classes. You’ll notice I didn’t use the My Classes for FileSystem stuff. As C#’ies, we don’t get to use that class, but we can use System.IO.FileSystem, which is also in the VisualBasic.dll.
static void Main(string args)
// Play a sound.
Microsoft.VisualBasic.MyServices.MyAudio myaudio = new MyAudio();
// The case of the filename and path must be correct.
// Display displays on the current time and date.
Microsoft.VisualBasic.MyServices.MyClock myclock = new MyClock();
Console.WriteLine("Current day of the week " + myclock.LocalTime.DayOfWeek);
// Display details about the computer running this code.
Microsoft.VisualBasic.MyServices.MyComputer mycomputer = new MyComputer();
Console.WriteLine("Computer name: " + mycomputer.Name);
Console.WriteLine("Computer connected to network: " + mycomputer.Network.IsAvailable.ToString());
// Duplicate a directory
// Thanks Stephen Toub