This tutorial shows how to publish a console app so that other users can run it. Publishing creates the set of files that are needed to run an application. To deploy the files, copy them to the target machine.
The .NET CLI is used to publish the app, so you can follow this tutorial with a code editor other than Visual Studio Code if you prefer.
The available mssql plugin for Visual Studio Code, is no help, as it is a management plug-in. Lately, an early release of a plugin to handle sqlproj projects was released for Azure Data Studio. Visual Studio and Xamarin Develop rich native apps for iOS, Android, macOS, and Windows with C# in Visual Studio. Leverage Xamarin.Forms to build native and performant cross-platform user interfaces across platforms and plug into the.NET ecosystem taking advantage of.NET Standard libraries to share code and the NuGet ecosystem. The Visual Studio family of products has built-in support for working with Docker on Linux, macOS, and Windows. Easily configure your application for Docker, then step through your code line-by-line as it runs in a Docker container. Work with Docker in Visual Studio. Dotnet, A Visual Studio Code extension. A modern interface to the dotnet cli for Visual Studio Code. This extension provides access to the dotnet cli by using the Quick Pick UI. Open the command palette by pressing Shift + ⌘ + P or F1; Start typing dotnet and choose your desired commmand; Features.
Prerequisites
- This tutorial works with the console app that you create in Create a .NET console application using Visual Studio Code.
Publish the app
Start Visual Studio Code.
Open the HelloWorld project folder that you created in Create a .NET console application using Visual Studio Code.
Choose View > Terminal from the main menu.
The terminal opens in the HelloWorld folder.
Run the following command:
The default build configuration is Debug, so this command specifies the Release build configuration. The output from the Release build configuration has minimal symbolic debug information and is fully optimized.
The command output is similar to the following example:
Inspect the files
By default, the publishing process creates a framework-dependent deployment, which is a type of deployment where the published application runs on a machine that has the .NET runtime installed. To run the published app you can use the executable file or run the dotnet HelloWorld.dll
command from a command prompt.
In the following steps, you'll look at the files created by the publish process.
Select the Explorer in the left navigation bar.
Expand bin/Release/net5.0/publish.
As the image shows, the published output includes the following files:
HelloWorld.deps.json
This is the application's runtime dependencies file. It defines the .NET components and the libraries (including the dynamic link library that contains your application) needed to run the app. For more information, see Runtime configuration files.
HelloWorld.dll
This is the framework-dependent deployment version of the application. To execute this dynamic link library, enter
dotnet HelloWorld.dll
at a command prompt. This method of running the app works on any platform that has the .NET runtime installed.HelloWorld.exe (HelloWorld on Linux, not created on macOS.)
This is the framework-dependent executable version of the application. The file is operating-system-specific.
HelloWorld.pdb (optional for deployment)
This is the debug symbols file. You aren't required to deploy this file along with your application, although you should save it in the event that you need to debug the published version of your application.
HelloWorld.runtimeconfig.json
This is the application's run-time configuration file. It identifies the version of .NET that your application was built to run on. You can also add configuration options to it. For more information, see .NET run-time configuration settings.
Run the published app
In Explorer, right-click the publish folder (Ctrl-click on macOS), and select Open in Terminal.
On Windows or Linux, run the app by using the executable.
On Windows, enter
.HelloWorld.exe
and press Enter.On Linux, enter
./HelloWorld
and press Enter.Enter a name in response to the prompt, and press any key to exit.
On any platform, run the app by using the
dotnet
command:Enter
dotnet HelloWorld.dll
and press Enter.Enter a name in response to the prompt, and press any key to exit.
Additional resources
Next steps
In this tutorial, you published a console app. In the next tutorial, you create a class library.
-->This tutorial shows how to create and run a .NET console application by using Visual Studio Code and the .NET CLI. Project tasks, such as creating, compiling, and running a project are done by using the .NET CLI. You can follow this tutorial with a different code editor and run commands in a terminal if you prefer.
Prerequisites
- Visual Studio Code with the C# extension installed. For information about how to install extensions on Visual Studio Code, see VS Code Extension Marketplace.
- The .NET 5.0 SDK or later
Create the app
Create a .NET console app project named 'HelloWorld'.
Start Visual Studio Code.
Select File > Open Folder (File > Open... on macOS) from the main menu.
In the Open Folder dialog, create a HelloWorld folder and click Select Folder (Open on macOS).
The folder name becomes the project name and the namespace name by default. You'll add code later in the tutorial that assumes the project namespace is
HelloWorld
.Open the Terminal in Visual Studio Code by selecting View > Terminal from the main menu.
The Terminal opens with the command prompt in the HelloWorld folder.
In the Terminal, enter the following command:
The template creates a simple 'Hello World' application. It calls the Console.WriteLine(String) method to display 'Hello World!' in the console window.
The template code defines a class, Program
, with a single method, Main
, that takes a String array as an argument:
Main
is the application entry point, the method that's called automatically by the runtime when it launches the application. Any command-line arguments supplied when the application is launched are available in the args array.
Dotnet Visual Studio Code
Run the app
Run the following command in the Terminal:
The program displays 'Hello World!' and ends.
Enhance the app
Enhance the application to prompt the user for their name and display it along with the date and time.
Open Program.cs by clicking on it.
The first time you open a C# file in Visual Studio Code, OmniSharp loads in the editor.
Select Yes when Visual Studio Code prompts you to add the missing assets to build and debug your app.
Replace the contents of the
Main
method in Program.cs, which is the line that callsConsole.WriteLine
, with the following code:This code displays a prompt in the console window and waits until the user enters a string followed by the Enter key. It stores this string in a variable named
name
. It also retrieves the value of the DateTime.Now property, which contains the current local time, and assigns it to a variable nameddate
. And it displays these values in the console window. Finally, it displays a prompt in the console window and calls the Console.ReadKey(Boolean) method to wait for user input.NewLine is a platform-independent and language-independent way to represent a line break. Alternatives are
n
in C# andvbCrLf
in Visual Basic.The dollar sign (
$
) in front of a string lets you put expressions such as variable names in curly braces in the string. The expression value is inserted into the string in place of the expression. This syntax is referred to as interpolated strings.Save your changes.
Important
In Visual Studio Code, you have to explicitly save changes. Unlike Visual Studio, file changes are not automatically saved when you build and run an app.
Run the program again:
Respond to the prompt by entering a name and pressing the Enter key.
Press any key to exit the program.
Additional resources
Dotnet Build Visual Studio Code
Next steps
Dotnet Publish Visual Studio Code
In this tutorial, you created a .NET console application. In the next tutorial, you debug the app.