Megosztás a következőn keresztül:


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

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 (Visual Basic).

Adding a Reference

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;
    

Example

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...");
        myAudio.Play(@"c:\WINDOWS\Media\chimes.wav");

        // Display time information with the Clock class:
        Clock myClock = new Clock();
        Console.Write("Current day of the week: ");
        Console.WriteLine(myClock.LocalTime.DayOfWeek);
        Console.Write("Current date and time: ");
        Console.WriteLine(myClock.LocalTime);

        // 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.");
        }
        else
        {
            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
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(
    @"C:\original_directory",
    @"C:\copy_of_original_directory");

See Also

Reference

Namespaces (C# Programming Guide)

Using Namespaces (C# Programming Guide)

Concepts

C# Programming Guide