Skip to content

Instantly share code, notes, and snippets.

@IvanStoychev
Last active April 9, 2023 11:44
Show Gist options
  • Save IvanStoychev/1426c34e71e77e8c5041ce5c678b4231 to your computer and use it in GitHub Desktop.
Save IvanStoychev/1426c34e71e77e8c5041ce5c678b4231 to your computer and use it in GitHub Desktop.
Options that can be used in a C# project file.
<PropertyGroup>
<!-- Changes the application type depending on the build configuration. -->
<!-- In this case in "Debug" it will be a console application, while in "Release" a normal windows application. -->
<!-- This is used so in "Debug" status messages can be pumped to the console, while skipped for the end user in "Release". -->
<OutputType Condition="'$(Configuration)'=='Debug'">Exe</OutputType>
<OutputType Condition="'$(Configuration)'=='Release'">WinExe</OutputType>
<!-- Most-used framework by me. Specifying it like this simplifies some things. -->
<TargetFramework>net6.0-windows</TargetFramework>
<!-- Allows to work with WPF, as .Net 5+ applications don't have such support by default. -->
<UseWPF>true</UseWPF>
<!-- Produce a single (usually ".exe") file. -->
<!-- There is an issue (https://github.com/dotnet/runtime/issues/45382) where trying to debug with this option -->
<!-- results in an error. A workaround is to have a condition as shown. -->
<PublishSingleFile Condition="'$(Configuration)'=='Release'">true</PublishSingleFile>
<!-- Pack all ".dll"s and libraries into the end product so it can be ran on "bare-bones" machines. -->
<!-- Single-file publishing doesn't bundle native libraries by default. -->
<!-- These files will be extracted to a temporary directory in the client machine when the single file application is run. -->
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<!-- This will make the publish result able to run even if no frameworks and/or dependencies are installed on the machine. -->
<SelfContained>true</SelfContained>
<!-- ================================================================================================== -->
<!-- Difference between "IncludeNativeLibrariesForSelfExtract" and "SelfContained" -->
<!-- ================================================================================================== -->
<!-- Setting "SelfContained" to 'true' will produce an output that contains all the *.dll files the -->
<!-- application needs to run. -->
<!-- These files will be put in the output directory alonside the executable that starts the -->
<!-- application. On start the program will look for those dlls and not rely on any .Net being -->
<!-- installed on the machine it runs on. -->
<!-- Conversely, setting "SelfContained" to 'false' will produce and output executable that won't be -->
<!-- able to run if no .Net is installed on the machine. -->
<!-- -->
<!-- Setting "IncludeNativeLibrariesForSelfExtract" to 'true' will package all the aforementioned dlls -->
<!-- in the executable, thus creating an application with less files to manage. -->
<!-- ================================================================================================== -->
<!-- Disables debug symbols (".pdb" files) when publishing for release. -->
<DebugSymbols Condition="'$(Configuration)'=='Release'">false</DebugSymbols>
<DebugType Condition="'$(Configuration)'=='Release'">None</DebugType>
<!-- Defines the environment for which to publish the application. -->
<!-- Specifying "RuntimeIdentifier" sets <SelfContained>true</SelfContained> by default. -->
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PlatformTarget>x64</PlatformTarget>
<!-- Prevents generation of "*.deps.json" files. -->
<GenerateDependencyFile>false</GenerateDependencyFile>
</PropertyGroup>
<ItemGroup>
<None Update="appsettings.json">
<!-- Copy the appsettings file to the build directory if it has been modified. -->
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<!-- This will keep the appsettings outside the single ".exe" file, so they can be updated. -->
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</None>
<!-- Defines a mask for all items inside a folder called "Resources" in the project root folder. -->
<None Update="Resources\**">
<!-- This option, to copy a file only if modified, will be applied to all files maching the above mask.-->
<!-- In this case everything in the "(Project root folder)\Resources".-->
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<!-- Clears the publish folder before building. -->
<!-- This can be done to avoid having obsolete files from a previous build in the folder. -->
<!-- The condition allows for the prebuild command to be executed only if the build is being done through Visual Studio. -->
<!-- This allows, for example, the project to have CI/CD that doesn't need to run on a system that supports those commands. -->
<!-- (As usually CI/CD pipelines don't keep files between runs so the command would be pointless.) -->
<Target Name="PreBuild" BeforeTargets="PreBuildEvent" Condition="'$(BuildingInsideVisualStudio)' == 'true'">
<Exec Command="rd /s /q &quot;$(TargetDir)&quot;" />
</Target>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment