2

What I am doing:

  1. Create new WPF Application project
  2. Add App.config to the project
  3. Build project
  4. Open App.config properties and set 'Copy to output directory' parameter to 'Do not copy'
  5. Make some changes to App.config
  6. Go to bin\Debug folder and set read-only attribute to file WpfApplication1.exe.config
  7. Rebuild project

Result:

Error   2   Unable to copy file "App.config" to "bin\Debug\WpfApplication1.exe.config". Access to the path 'bin\Debug\WpfApplication1.exe.config' is denied.    WpfApplication1

Why VS still trying to copy the file, and how to force it to stop doing this?

P.S. I'm debugging my app on several remote computers (switching between them as needed) and each one should have it's own configuration. That's why I don't want VS to overwrite configuration on each build.

  • Why don't you want VS to overwrite the config? – alex Apr 11 '13 at 13:27
  • possible duplicate of [How do I prevent the app.config from being integrated into a .net Library (dll)](http://stackoverflow.com/questions/4668910/how-do-i-prevent-the-app-config-from-being-integrated-into-a-net-library-dll) – Mike Perrenoud Apr 11 '13 at 13:27

2 Answers2

0

If you really need to force stop VS from changing your config, the easiest way to do this may be writing simple script which would protect config:

Add execution of script backup-config to pre-build and restore-config to after-build. Backup config should copy config somewhere safe and restore should copy it back, after VS changed it.

Ari
  • 3,101
  • 2
  • 27
  • 49
0

Default for App.config is "do not copy", and it is not copied in that part of that build process. If you want different versions for builds ( Debug_1, Debug_2,... ) make pre-build event script to copy App.config.debug_1 or App.config.debug_2,... to App.config depending on build type.

I don't think you can change the way visual studio copies app.config. But I think this might be solution for you. Put this at the beginning of your program.

#if DEBUG_1
    AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "debug_1.config");
#endif
#if DEBUG_2
    AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "debug_2.config");
#endif
...

This way config file will be copied, but it wont be used. Simply copy that config and make changes you need.

IvanP
  • 263
  • 7
  • 21