This article shows you how to automatically preserve your database connection string in app.config whilst also deploying an encrypted version in your production app….
In my example, I am connecting to a remote MySQL database from a winforms app.
Add your data source via the usual means, choosing to save your password and store in app.config:
The open ProjectSettings > Compile > BuildEvents > Edit Post-build
and post the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
SET ApplicationName=OmniGameZ echo. echo POST BUILD ACTIONS echo ==================== if EXIST web.config ( echo Deleting web.config DEL web.config ) echo Renaming %ApplicationName%.exe.config to web.config REN %ApplicationName%.exe.config web.config echo Running aspnet_regis against webconfig SET rpath=%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "connectionStrings" "$(TargetDir) SET rpath=%rpath:~0,-1%" echo Path: %rpath% %rpath% echo Renaming web.config to %ApplicationName%.exe.config REN web.config %ApplicationName%.exe.config echo Doing any replacements echo Path: "$(TargetDir)%ApplicationName%.exe.config" cscript replace.vbs "$(TargetDir)%ApplicationName%.exe.config" "<userSettings>c" "<userSettings>" DEL replace.vbs echo Now delete dll xmls echo Build mode: $(ConfigurationName) if $(ConfigurationName)==Release ( echo Build is release - deleting xml and pdb files del "$(TargetDir)*.xml del "$(TargetDir)*.pdb ) echo Done. |
Replacing “YourAppWithoutExtention” with your app name.
Then every time it builds, it will automatically encrypt your app.config.
The last paragraph of code removes any unnecessary .pdb or .xml files. This can be removed if your app requires them.
Excellent blog post. I was searching for something
completely different,
but stumbled on your site. I am glad I did. Many thanks for sharing
useful information. Many thanks and best of luck.