教學課程:使用 .NET CLI 安裝並使用 .NET 全域工具

本文適用於: ✔️ .NET Core 2.1 SDK 與更新版本

此教學課程將教導您如何安裝並使用全域工具。 您可以使用您在此系列的第一個教學課程中建立的工具。

必要條件

使用此工具作為全域工具

  1. microsoft.botsay 專案資料夾中執行 dotnet tool install 命令,從套件安裝工具:

    dotnet tool install --global --add-source ./nupkg microsoft.botsay
    

    --global 參數會指示 .NET CLI,在預設位置安裝工具二進位檔,以自動新增至 PATH 環境變數。

    --add-source 參數會指示 .NET CLI,暫時使用 ./nupkg 目錄作為 NuGet 套件的額外來源摘要。 您為套件提供了唯一的名稱,以確保只能在 ./nupkg 目錄中 (而不是在 Nuget.org 網站上) 找到它。

    輸出會顯示用來呼叫工具和已安裝版本的命令:

    You can invoke the tool using the following command: botsay
    Tool 'microsoft.botsay' (version '1.0.0') was successfully installed.
    

    注意

    根據預設,要安裝的 .NET 二進位檔架構代表目前執行的 OS 架構。 若要指定不同的 OS 架構,請參閱 dotnet tool install, --arch option

  2. 叫用工具:

    botsay hello from the bot
    

    注意

    如果此命令失敗,您可能需要開啟新的終端以重新整理 PATH。

  3. 執行 dotnet tool uninstall 命令來移除工具:

    dotnet tool uninstall -g microsoft.botsay
    

使用此工具作為安裝在自訂位置的全域工具

  1. 從套件安裝工具。

    在 Windows 上:

    dotnet tool install --tool-path c:\dotnet-tools --add-source ./nupkg microsoft.botsay
    

    在 Linux 或 MacOS 上:

    dotnet tool install --tool-path ~/bin --add-source ./nupkg microsoft.botsay
    

    --tool-path 參數會指示 .NET CLI,在指定的位置安裝工具二進位檔。 如果目錄不存在,則會建立它。 此目錄不會自動新增至 PATH 環境變數。

    輸出會顯示用來呼叫工具和已安裝版本的命令:

    You can invoke the tool using the following command: botsay
    Tool 'microsoft.botsay' (version '1.0.0') was successfully installed.
    
  2. 叫用工具:

    在 Windows 上:

    c:\dotnet-tools\botsay hello from the bot
    

    在 Linux 或 macOS 上:

    ~/bin/botsay hello from the bot
    
  3. 執行 dotnet tool uninstall 命令來移除工具:

    在 Windows 上:

    dotnet tool uninstall --tool-path c:\dotnet-tools microsoft.botsay
    

    在 Linux 或 macOS 上:

    dotnet tool uninstall --tool-path ~/bin microsoft.botsay
    

疑難排解

如果您在依照教學課程進行時收到錯誤訊息,請參閱針對 .NET 工具使用方式問題進行疑難排解 (部分機器翻譯)。

下一步

在此教學課程中,您已安裝並使用工具作為全域工具。 如需如何安裝並使用全域工具的詳細資訊,請參閱管理全域工具。 若要安裝並使用與本機工具相同的工具,請前進到下一個教學課程。