How to: Use the My Namespace (C# Programming Guide)


Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

The Microsoft.VisualBasic.MyServices namespace (My in Visual Basic) provides easy and intuitive access to a number of .NET Framework classes, enabling you to write code that interacts with the computer, application, settings, resources, and so on. Although originally designed for use with Visual Basic, the MyServices namespace can be used in C# applications.

For more information about using the MyServices namespace from Visual Basic, see Development with My.

Before you can use the MyServices classes in your solution, you must add a reference to the Visual Basic library.

To add a reference to the Visual Basic library

  1. In Solution Explorer, right-click the References node, and select Add Reference.

  2. When the References dialog box appears, scroll down the list, and select Microsoft.VisualBasic.dll.

    You might also want to include the following line in the using section at the start of your program.

    using Microsoft.VisualBasic.Devices;

This example calls various static methods contained in the MyServices namespace. For this code to compile, a reference to Microsoft.VisualBasic.DLL must be added to the project.

using System;
using Microsoft.VisualBasic.Devices;

class TestMyServices
    static void Main()
        // Play a sound with the Audio class:
        Audio myAudio = new Audio();
        Console.WriteLine("Playing sound...");

        // Display time information with the Clock class:
        Clock myClock = new Clock();
        Console.Write("Current day of the week: ");
        Console.Write("Current date and time: ");
        // Display machine information with the Computer class:
        Computer myComputer = new Computer();
        Console.WriteLine("Computer name: " + myComputer.Name);

        if (myComputer.Network.IsAvailable)
            Console.WriteLine("Computer is connected to network.");
            Console.WriteLine("Computer is not connected to network.");

Not all the classes in the MyServices namespace can be called from a C# application: for example, the FileSystemProxy class is not compatible. In this particular case, the static methods that are part of FileSystem, which are also contained in VisualBasic.dll, can be used instead. For example, here is how to use one such method to duplicate a directory:

        // Duplicate a directory

C# Programming Guide
Using Namespaces