How to Create a Single Exe file with apsettings.json

Create a Single Exe file with configuration apsettingsjson

In this article, we will see how to create a Create a Single Exe file with apsettings.json configuration copied along with exe.

Today in this article, we will cover below aspects,

In our last article on Create Self Contained Single Executable (EXE) in .NET Core we already understood basic and best practices on how to create a single exe with Visual Studio and using CLI commands like,

dotnet publish -r linux-x64 -p:PublishSingleFile=true   

Or

dotnet publish -r win10-x64  -p:PublishSingleFile=true

Exe file we are generating might contain a configuration that can remain editable for changing the configuration as required.

This could be a genuine requirement for most of the batch file running applications.

In the above article, we also looked at Visual Studio and VSCode-specific settings to be used.

As above exec contains all the resources including the configuration file loaded within the EXE.

Using the Configuration file in Exe

You can copy configuration files like apsettings.json or ini or XML file, as usual, using the below setting options when ExcludeFromSingleFile property is defined as below,

<ItemGroup>
    <None Update="apsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
    </None>
  </ItemGroup>

Once you run the config file, you shall see the configuration file is getting copied to the exe location

Creating a trimmed EXE file

Creating reduced size/ trimmed EXE file using configuration change is discussed in the below article in detail,

References:

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *