如何:在視覺工作室運行 C# 程式How to: Run a C# program in Visual Studio

執行程式所需的操作取決於您從什麼開始、程式、應用或服務的類型,以及是否要在調試器下運行它。What you need to do to run a program depends on what you're starting from, what type of program, app, or service it is, and whether you want to run it under the debugger or not. 在最簡單的情況下,當您在 Visual Studio 中打開專案時,請按Ctrl+F5(****無需調試即可開始)或F5(****從調試開始)或按主 Visual Studio 工具列上的綠色箭頭(開始按鈕)來生成並運行該專案。In the simplest case, when you have a project open in Visual Studio, build and run it by pressing Ctrl+F5 (Start without debugging) or F5 (Start with debugging), or press the green arrow (Start Button) on the main Visual Studio toolbar.

顯示開始按鍵的螢幕擷取

從項目開始Starting from a project

如果您有一個 C# 專案 (.csproj檔),則可以執行它(如果它是可執行的程式)。If you have a C# project (.csproj file), then you can run it, if it is a runnable program. 如果專案包含包含方法Main的 C# 檔,並且其輸出是可執行檔 (EXE),則如果生成成功,則最有可能運行該檔。If a project contains a C# file with a Main method, and its output is an executable (EXE), then most likely it will run if it builds successfully.

如果在 Visual Studio 中的項目中已經擁有程式的代碼,請打開該專案。If you already have the code for your program in a project in Visual Studio, open the project. 要打開專案,請雙擊或點擊 Windows 檔案資源管理員中的 .csproj或從 Visual Studio,選擇 「打開專案」,流覽以查找專案 (.csproj) 檔案,然後選擇專案檔。To open the project, double-click or tap on the .csproj from the Windows File Explorer, or from Visual Studio, choose Open a project, browse to find the project (.csproj) file, and choose the project file.

專案在 Visual Studio 中載入後,按Ctrl+F5(****無需除錯即可啟動)或使用 Visual Studio 工具列上的綠色 「開始」 按鈕執行程式。After the projects loads in Visual Studio, press Ctrl+F5 (Start without debugging) or use the green Start button on the Visual Studio toolbar to run the program. 如果有多個專案,則必須將具有Main該方法的專案設置為啟動專案。If there are multiple projects, the one with the Main method must be set as the startup project. 要設置啟動專案,請右鍵單擊專案節點,然後選擇 「設置為啟動專案」。To set the startup project, right-click on a project node, and choose Set as startup project.

設定啟動項目

可視化工作室嘗試構建和運行專案。Visual Studio attempts to build and run your project. 如果存在產生錯誤,請參閱 「輸出」 視窗中的產生輸出和錯誤列表視窗中的錯誤。If there are build errors, you see the build output in the Output window and the errors in the Error List window.

如果生成成功,應用以適合項目類型的方式運行。If the build succeeds, the app runs in a way that's appropriate for the type of project. 控制台應用在終端視窗中運行,Windows 桌面應用在新視窗中啟動,Web 應用在瀏覽器中啟動(由 IIS Express 託管),等等。Console apps run in a terminal window, Windows desktop apps start in a new window, web apps start in the browser (hosted by IIS Express), and so on.

從代碼開始Starting from code

如果要從代碼清單、代碼檔或少量檔開始,請先確保要運行的代碼來自受信任的源,並且是可運行的程式。If you're starting from a code listing, code file, or a small number of files, first make sure the code you want to run is from a trusted source and is a runnable program. 如果它有一個Main方法,它很可能被用作可運行的程式,您可以使用主控台應用範本創建專案,在 Visual Studio 中使用它。If it has a Main method, it is likely intended as a runnable program that you can use the Console App template to create a project to work with it in Visual Studio.

單一檔案的代碼清單Code listing for a single file

啟動 Visual Studio,打開空的 C# 主控台項目,選擇專案中已包含的 .cs 檔中的所有代碼,然後將其刪除。Start Visual Studio, open an empty C# console project, select all the code in the .cs file that's in the project already, and delete it. 然後,將代碼的內容粘貼到 .cs 檔中。Then, paste the contents of your code into the .cs file. 貼上代碼時,請覆蓋或刪除以前存在的代碼。When you paste the code, overwrite or delete the code that was there before. 重新命名檔以符合原始碼。Rename the file to match the original code.

幾個檔案的代碼清單Code listings for a few files

啟動 Visual Studio,打開空的 C# 主控台項目,選擇專案中已包含的 .cs 檔中的所有代碼,然後將其刪除。Start Visual Studio, open an empty C# console project, select all the code in the .cs file that's in the project already, and delete it. 然後,將第一個代碼文件的內容粘貼到 .cs 檔中。Then, paste the contents of the first code file into the .cs file. 重新命名檔以符合原始碼。Rename the file to match the original code.

對於第二個檔,右鍵單擊解決方案資源管理器中的專案節點以打開專案的快捷方式功能表,然後選擇 「新增>現有專案(或使用鍵群組Shift+Alt+A), 然後選擇代碼檔。For a second file, right-click on the project node in Solution Explorer to open the shortcut menu for the project, and choose Add > Existing Item (or use the key combination Shift+Alt+A), and select the code files.

磁碟上的多個檔案Multiple files on disk

  1. 建立適當類型的新專案(如果您不確定,請使用 C#控制台應用)。Create a new project of the appropriate type (use C# Console App if you're not sure).

  2. 右鍵單擊專案節點,se添加 > 現有專案以選擇檔並將其導入專案。Right-click on the project node, se Add > Existing Item to select the files and import them into your project.

從資料夾開始Starting from a folder

當您使用包含許多檔的資料夾時,首先查看是否有專案或解決方案。When you're working with a folder of many files, first see if there's a project or solution. 如果程式是使用 Visual Studio 創建的,則應查找專案檔或解決方案檔。If the program was created with Visual Studio, you should find a project file or a solution file. 尋找具有 .csproj副檔名或 .sln 副檔名的檔案,並在 Windows 檔案資源管理器中按兩下其中一個在 Visual Studio 中打開這些檔。Look for files with the .csproj extension or .sln extension and in the Windows File Explorer, double-click on one of them to open them in Visual Studio. 請參考從視覺化工作室解決方案或項目開始See Starting from a Visual Studio solution or project.

如果您沒有專案檔(例如代碼是在另一個開發環境中開發的,則使用 Visual Studio 中的Open 資料夾方法打開頂級資料夾。If you don't have a project file, such as if the code was developed in another development environment, then open the top-level folder by using the Open folder method in Visual Studio. 請參考開發沒有項目或解決方案的代碼See Develop code without projects or solutions.

從 GitHub 或 Azure 開發人員計劃儲存函式庫開始Starting from a GitHub or Azure DevOps repo

如果要運行的代碼在 GitHub 或 Azure DevOps 儲存庫中,則可以使用 Visual Studio 直接從儲存庫打開專案。If the code you want to run is in GitHub or in an Azure DevOps repo, you can use Visual Studio to open the project directly from the repo. 請參考從回購中開啟項目See Open a project from a repo.

執行程式Run the program

要啟動程式,請按主 Visual Studio 工具列上的綠色箭頭(開始按鈕),或按F5Ctrl+F5來執行該程式。To start the program, press the green arrow (Start button) on the main Visual Studio toolbar, or press F5 or Ctrl+F5 to run the program. 使用 「開始」 按鈕時,它將在除錯器下運行。When you use the Start button, it runs under the debugger. Visual Studio 嘗試在專案中生成代碼並運行它。Visual Studio attempts to build the code in your project and run it. 如果成功,太好了!If that succeeds, great! 但是,如果沒有,繼續閱讀一些關於如何成功構建它的想法。But if not, continue reading for some ideas on how to get it to build successfully.

疑難排解Troubleshooting

您的代碼可能有錯誤,但如果代碼正確,但僅取決於其他程式集或 NuGet 包,或者編寫到針對不同版本的 .NET,則可能可以輕鬆修復它。Your code might have errors, but if the code is correct, but just depends on some other assemblies or NuGet packages, or was written to target a different version of .NET, you might be able to easily fix it.

新增參考Add references

要正確生成,代碼必須正確,並且已設置對庫或其他依賴項的正確引用。To build properly, the code must be correct and have the right references set up to libraries or other dependencies. 您可以查看紅色波浪線和錯誤列表,查看程式是否有任何錯誤,甚至在編譯和運行它之前也是如此。You can look at the red squiggly lines and at the Error List to see if the program has any errors, even before you compile and run it. 如果您看到與未解析名稱相關的錯誤,則可能需要添加引用或 using 指令,或者兩者兼而有之。If you're seeing errors related to unresolved names, you probably need to add a reference or a using directive, or both. 如果代碼引用任何程式集或 NuGet 包,則需要在專案中添加這些引用。If the code references any assemblies or NuGet packages, you need to add those references in the project.

Visual Studio 會嘗試説明您識別缺少的引用。Visual Studio tries to help you identify missing references. 當名稱未解析時,編輯器中將顯示一個燈泡圖示。When a name is unresolved, a light bulb icon appears in the editor. 如果單擊燈泡,可以看到有關如何解決此問題的一些建議。If you click the light bulb, you can see some suggestions on how to fix the issue. 修復方法可能是:Fixes might be to:

  • 新增 using 指令add a using directive
  • 新增對程式集的參考或add a reference to an assembly, or
  • 安裝 NuGet 包。install a NuGet package.

缺少使用指令Missing using directive

例如,在下面的螢幕中,您可以選擇新增到using System;代碼檔的開頭以解析未解析的名稱: ConsoleFor example, in the following screen, you can choose to add using System; to the start of the code file to resolve the unresolved name Console:

新增使用指令的燈泡螢幕擷取

遺失程式集參照Missing assembly reference

.NET 引用可以以程式集或 NuGet 套件的形式進行。.NET references can be in the form of assemblies or NuGet packages. 通常,如果您找到原始程式碼,發行者或作者將解釋需要哪些程式集以及代碼所依賴的包。Usually, if you find source code, the publisher or author will explain what assemblies are required and what packages the code depends on. 要手動添加對專案的引用,請右鍵單擊解決方案資源管理器中的 「引用」 節點,選擇 「添加引用」並查找所需的程式集。To add a reference to a project manually, right-click on the References node in the Solution Explorer, choose Add Reference, and locate the required assembly.

新增參考選單的螢幕擷取

您可以使用引用管理員,按照"添加或刪除引用"中的說明查找程式集並添加引用。You can find assemblies and add references by following the instructions in Add or remove references by using the reference manager.

缺少 NuGet 套件Missing NuGet package

如果 Visual Studio 偵測到缺少的 NuGet 套件,則會出現一個燈泡,並為您提供安裝它的選項:If Visual Studio detects a missing NuGet package, a light bulb appears and gives you the option to install it:

要安裝套件的燈泡螢幕截圖

如果這不能解決問題,並且 Visual Studio 找不到包,請嘗試連線搜尋它。If that doesn't solve the issue and Visual Studio can't locate the package, try searching for it online. 請參考在視覺化工作室中安裝與使用 NuGet 套件See Install and use a NuGet package in Visual Studio.

使用正確的版本 .NETUse the right version of .NET

由於 .NET 框架的不同版本具有某種程度的向後相容性,因此較新的框架可能會運行為舊框架編寫的代碼,而無需進行任何修改。Because different versions of the .NET Framework have some degree of backward compatibility, a newer framework might run code written for an older framework without any modifications. 但是,有時您需要針對特定框架。But, sometimes you need to target a specific framework. 如果尚未安裝 .NET 框架或 .NET Core 的特定版本,則可能需要安裝該版本。You might need to install a specific version of the .NET Framework or .NET Core, if it's not already installed. 請參閱修改視覺工作室See Modify Visual Studio.

要改變目標框架,請參閱變更目標框架To change the target framework, see Change the target framework. 關於詳細資訊,請參閱故障排除.NET 框架定位錯誤For more information, see Troubleshooting .NET Framework targeting errors.

後續步驟Next steps

通過閱讀「歡迎瀏覽視覺工作室 IDE」來探索視覺工作室開發環境。Explore the Visual Studio development environment by reading Welcome to the Visual Studio IDE.

另請參閱See also

建立您的第一個 C# 應用程式Create your first C# app