.net – How to prevent .NET from using the compiled .config settings when no config file is present

confignetsettings

How can I prevent .NET from using the compiled .config settings when no config file is present?

For example, I have a connection string setting in the app.config file that specifies a development server. When built, this produces an .exe.config file. And all is well.

However, if you ever forget to tote along the .exe.config file, the application will happily – and without warning – use the values that were specified in the designer (app.config).

Is there any way to cause a .NET assembly to throw an exception instead of silently using values that were specified during development?

Best Answer

On the Settings pane there is a GenerateDefaultValueInCode property for each setting, which by default is set to true.

If you set it to false and no .config file is found an empty string is returned for that setting. I'm not sure how it would work for other datatypes.