在 Visual Studio 2017 中使用 .NET Core SDK 來建置 Visual Basic Hello World 應用程式Build a Visual Basic Hello World application with the .NET Core SDK in Visual Studio 2017

本主題提供使用 Visual Studio 2017 的 Visual Basic,針對簡單 .NET Core 主控台應用程式進行建置、偵錯及發行的逐步指示。This topic provides a step-by-step introduction to building, debugging, and publishing a simple .NET Core console application using Visual Basic in Visual Studio 2017. Visual Studio 2017 提供功能完整的開發環境來建置 .NET Core 應用程式。Visual Studio 2017 provides a full-featured development environment for building .NET Core applications. 只要應用程式沒有平台特定的相依性,應用程式就可以在 .NET Core 的任何目標平台,以及安裝 .NET Core 的任何系統上執行。As long as the application doesn't have platform-specific dependencies, the application can run on any platform that .NET Core targets and on any system that has .NET Core installed.


已安裝「.NET Core 跨平台開發」工作負載的 Visual Studio 2017 (英文)Visual Studio 2017 with the ".NET Core cross-platform development" workload installed. 您可以使用 .NET Core 2.0 開發應用程式。You can develop your app with .NET Core 2.0.

如需詳細資訊,請參閱 Windows 上 .NET Core 的必要條件For more information, see Prerequisites for .NET Core on Windows.

簡單的 Hello World 應用程式A simple Hello World application

請開始建立簡單的 "Hello World" 主控台應用程式。Begin by creating a simple "Hello World" console application. 請依照下列步驟:Follow these steps:

  1. 啟動 Visual Studio 2017。Launch Visual Studio 2017. 從功能表列中選取 [檔案] > [新增] > [專案]。Select File > New > Project from the menu bar. 在 [新增專案]* 對話方塊中,選取後面跟著 [.NET Core] 節點的 [Visual Basic] 節點。In the New Project* dialog, select the Visual Basic node followed by the .NET Core node. 然後選取 [主控台應用程式 (.NET Core)] 專案範本。Then select the Console App (.NET Core) project template. 在 [名稱] 文字方塊中,鍵入 "HelloWorld"。In the Name text box, type "HelloWorld". 選取 [確定] 按鈕。Select the OK button.

    已選取 [主控台應用程式] 的 [新增專案] 對話方塊

  2. Visual Studio 使用範本建立專案。Visual Studio uses the template to create your project. .NET Core 的 Visual Basic 主控台應用程式範本會自動定義一個 Program 類別,該類別具有採用 String 陣列為引數的單一 Main 方法。The Visual Basic Console Application template for .NET Core automatically defines a class, Program, with a single method, Main, that takes a String array as an argument. Main 是應用程式進入點,是執行階段在啟動應用程式時會自動呼叫的方法。Main is the application entry point, the method that's called automatically by the runtime when it launches the application. 在應用程式啟動時所提供的所有命令列引數,都會在 args 陣列中提供。Any command-line arguments supplied when the application is launched are available in the args array.

    Visual Studio 和新的 HelloWorld 專案

    此範本會建立簡單的 "Hello World" 應用程式。The template creates a simple "Hello World" application. 它會呼叫 Console.WriteLine(String) 方法,以在主控台視窗中顯示常值字串 "Hello World!"It calls the Console.WriteLine(String) method to display the literal string "Hello World!" in the console window. 透過使用工具列上的綠色箭頭選取 HelloWorld 按鈕,您可以 [偵錯] 模式執行程式。By selecting the HelloWorld button with the green arrow on the toolbar, you can run the program in Debug mode. 不過,如果您這樣做,主控台視窗將會在簡短顯示之後關閉。If you do, the console window is visible for only a brief time interval before it closes. 這是因為在 Main 方法中的單一陳述式執行完畢之後,Main 方法會立即終止,而應用程式會立即結束。This occurs because the Main method terminates and the application ends as soon as the single statement in the Main method executes.

  3. 若要使應用程式在關閉主控台視窗之前先暫停,請在 Console.WriteLine(String) 方法呼叫之後立即新增下列程式碼:To cause the application to pause before it closes the console window, add the following code immediately after the call to the Console.WriteLine(String) method:

    Console.Write("Press any key to continue...")

    此程式碼會提示使用者按下任意鍵,並暫停程式直到按下按鍵為止。This code prompts the user to press any key and then pauses the program until a key is pressed.

  4. 在功能表列中,選取 [組建] > [組建方案]。On the menu bar, select Build > Build Solution. 這會將您的程式編譯成中繼語言 (IL),而該語言是由 Just-In-Time (JIT) 編譯器轉換為二進位程式碼。This compiles your program into an intermediate language (IL) that's converted into binary code by a just-in-time (JIT) compiler.

  5. 透過使用工具列上的綠色箭頭選取 HelloWorld 按鈕來執行程式。Run the program by selecting the HelloWorld button with the green arrow on the toolbar.

    主控台視窗顯示 Hello World,請按任意鍵以繼續

  6. 按任意鍵以關閉主控台視窗。Press any key to close the console window.

增強 Hello World 應用程式Enhancing the Hello World application

增強您的應用程式以提示使用者輸入其名稱,與日期和時間一起顯示。Enhance your application to prompt the user for his or her name and to display it along with the date and time. 若要修改並測試程式,請執行下列動作︰To modify and test the program, do the following:

  1. 在程式碼視窗中,於 Sub Main(args As String()) 行之後的左括弧後方,以及第一個右括弧之前輸入下列 Visual Basic 程式碼:Enter the following Visual Basic code in the code window immediately after the opening bracket that follows the Sub Main(args As String()) line and before the first closing bracket:

    Console.WriteLine(vbCrLf + "What is your name? ")
    Dim name = Console.ReadLine()
    Dim currentDate = DateTime.Now
    Console.WriteLine($"{vbCrLf}Hello, {name}, on {currentDate:d} at {currentDate:t}")
    Console.Write(vbCrLf + "Press any key to exit... ")

    此程式碼會取代 Main 方法的內容。This code replaces the contents of the Main method.

    含有已更新之 Main 方法的 Visual Studio 程式檔案

    此程式碼會在主控台中顯示「What is your name?」,This code displays "What is your name?" 然後等待使用者輸入字串並按 Enter 鍵。in the console window and waits until the user enters a string followed by the Enter key. 它會將此字串儲存至名為 name 的變數。It stores this string into a variable named name. 此程式碼也會擷取 DateTime.Now 屬性的值,其中包含目前的當地時間,並將它指派至名稱為 currentDate 的變數。It also retrieves the value of the DateTime.Now property, which contains the current local time, and assigns it to a variable named currentDate. 最後,使用字串插值在主控台視窗中顯示這些值。Finally, it uses an interpolated string to display these values in the console window.

  2. 選擇 [建置] > [建置方案] 來編譯程式。Compile the program by choosing Build > Build Solution.

  3. 在 Visual Studio 中以 [偵錯] 模式執行程式,方法為選取工具列上的綠色箭頭,按 F5,或是選擇 [偵錯] > [開始偵錯] 功能表項目。Run the program in Debug mode in Visual Studio by selecting the green arrow on the toolbar, pressing F5, or choosing the Debug > Start Debugging menu item. 輸入名稱並按 Enter 鍵來回應提示。Respond to the prompt by entering a name and pressing the Enter key.


  4. 按任意鍵以關閉主控台視窗。Press any key to close the console window.

您已建立並執行應用程式。You've created and run your application. 若要開發專業的應用程式,請執行一些其他步驟,來針對發行準備應用程式:To develop a professional application, take some additional steps to make your application ready for release:

除了主控台應用程式之外,您也可以使用 Visual Basic、.NET Core 和 Visual Studio 2017 建置 .NET Standard 類別庫。Instead of a console application, you can also build a .NET Standard class library with Visual Basic, .NET Core, and Visual Studio 2017. 如需逐步介紹,請參閱在 Visual Studio 2017 中使用 Visual Basic 和 .NET Core SDK 建置 .NET Standard 程式庫For a step-by-step introduction, see Build a .NET Standard library with Visual Basic and .NET Core SDK in Visual Studio 2017.