共用方式為


HOW TO:使用 My 命名空間 (C# 程式設計手冊)

更新:2007 年 11 月

Microsoft.VisualBasic.MyServices 命名空間 (My) 提供簡單而直覺的存取方式,可讓您存取各種 .NET Framework 類別,並撰寫與電腦、應用程式、設定、資源等互動的程式碼。雖然 MyServices 命名空間原本是設計來搭配 Visual Basic 使用,不過您也可以在 C# 應用程式中使用這個命名空間。

如需從 Visual Basic 使用 MyServices 命名空間的詳細資訊,請參閱使用 My 進行開發

加入參考

在方案中使用 MyServices 類別之前,您必須先加入對 Visual Basic 程式庫的參考。

若要加入對 Visual Basic 程式庫的參考

  1. 在 [方案總管] 中,以滑鼠右鍵按一下 [參考] 節點,然後選取 [加入參考]。

  2. 當 [參考] 對話方塊出現時,向下捲動清單並選取 Microsoft.VisualBasic.dll。

    您可能也要在程式開頭的 using 段落中加入下列這行。

    using Microsoft.VisualBasic.Devices;
    

範例

這個範例會呼叫 MyServices 命名空間中包含的各種靜態方法。若要編譯這段程式碼,必須將對 Microsoft.VisualBasic.DLL 的參考加入至專案中。

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.");
        }
    }
}

C# 應用程式不一定能夠呼叫 MyServices 命名空間中的所有類別,例如 FileSystemProxy 類別就不相容。在這個特殊情況中,可以改用屬於 FileSystem (也包含在 VisualBasic.dll 檔案中) 的靜態方法。例如,下面便示範如何使用這類方法複製目錄:

// Duplicate a directory
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(
    @"C:\original_directory",
    @"C:\copy_of_original_directory");

請參閱

概念

C# 程式設計手冊

參考

命名空間 (C# 程式設計手冊)

使用命名空間 (C# 程式設計手冊)