Encrypt the App.config file automatically during the build process

Currently, my application automatically encrypts its own App.config file when it first launches the application.

I am using the ConfigurationManager class, as shown in this article:

Now I want App.config to be encrypted before the application launches for the first time.

The reason why I added the installer to my solution, and I do not want the installer to delete the unencrypted App.config file. I want the process to be automated.

(the only thing I've come up with so far is attaching .exe to the Post-Build event, but there is no more direct way?)

+6
source share
1 answer

First option: Using the built-in MSBuild Exec Task, run your own utility program that can encrypt the configuration file.

Second option: MSBuild's own task

I believe that you can write a simple MSBuild task that will find the App.Config file and then encrypt it during the solution build.

  • Create new MSBuild targets, for example EncryptConfig.targets
  • Update csproj file <Import Project="EncryptConfig.targets" />
  • Add DependsOnTargets for standard AfterBuild goals in csproj:
  <Target Name="AfterBuild" DependsOnTargets="EncryptConfig.targets" /> 

4) Write your own task and execute it in EncryptConfig.targets

How to create your own task:

+2
source

Source: https://habr.com/ru/post/898722/


All Articles