I have a C# project made in VS Code on Linux. While debugging I didn't have any issues with binaries or whatsoever but when I finished all the tests and wanted to run the program from terminal it turned out I dont know where to find.
The only solution I found was
cd project_folder dotnet run project_name
Is there any other way to do it? I want to make a single binary file that could be run from terminal.
CodePudding user response:
The process of creating a single file executable is explained at Single-file deployment and executable.
You will need to add some settings into your csproj file, for example:
<PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0</TargetFramework> <PublishSingleFile>true</PublishSingleFile> <SelfContained>true</SelfContained> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <PublishReadyToRun>true</PublishReadyToRun> </PropertyGroup>
...
These properties have the following functions:
PublishSingleFile
. Enables single file publishing. Also enables single file warnings during dotnet build.SelfContained
. Determines whether the app is self-contained or framework-dependent.RuntimeIdentifier
. Specifies the OS and CPU type you're targeting. Also sets<SelfContained>true</SelfContained>
by default.PublishReadyToRun
. Enables ahead-of-time (AOT) compilation.
With these settings set you can run dotnet publish
.
You can also specify these settings to the publish command without adding them to the project file. For example:
dotnet publish -r linux-x64 -p:PublishSingleFile=true --self-contained false