Home > Mobile >  Embed json file within .exe file
Embed json file within .exe file

Time:04-20

I am trying to publish my console application into 1 exe file. I have managed to embed the dll files into the exe file by adding to the csproj file:

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
      <PublishSingleFile>true</PublishSingleFile>
      <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
      <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

However I still need my file extractConfig.json in the same space as the exe file as well or else it will not run. How can I embed this file into the exe as well?

It is currently getting published like so:

enter image description here

But I need it so it is just the exe file

CodePudding user response:

There are a few options:

Create the config if it does not exist

When the program starts you check if the config file exist. If not, you create it. You could create this file from a default configuration object that you serialize, or include the configuration file as a string resource or some other way. I would also consider placing this file in the localappdata folder instead, since you might not have write access to the folder of the .exe file.

Exclude the configuration file from the package

You can add the ExcludeFromSingleFile option for the config file so that it is not included in the single .exe file. This will obviously require you to deploy multiple files.

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

CodePudding user response:

Spoke with the people I am working with and have gone with the suggestion from @SirOneOfMany and I am now using constants rather than the json file which easily allows me to just have the 1 exe file.

Thanks everyone for the suggestions

  • Related