0

I am writing a csharp console application. Depending on the user input I want to update my app.config file at run time.

Every time I need to replace a string with another string.

If the user has selected devenvironment, I need to replace all occurrence of testenvironment with devenvironement.

Please suggest how to do this at run time.

Patan
  • 17,073
  • 36
  • 124
  • 198
  • Did you take a look at http://stackoverflow.com/questions/6150644/change-default-app-config-at-runtime ? – Dethariel Nov 27 '13 at 13:11
  • check http://stackoverflow.com/questions/980440/update-app-config-system-net-setting-at-runtime and http://stackoverflow.com/questions/13286916/c-sharp-applicationsettings-how-to-update-app-config first – Kamran Shahid Nov 27 '13 at 13:13
  • Code changing the config is backwards, surely? Or deploy different configs to the diffrent environments. – doctorlove Nov 27 '13 at 13:22

2 Answers2

4

You don't do that. Don't. Ever. Use user settings or come up with something of your own. User space applications are not allowed to write to the Program Files folder. Don't.

You can create a user setting that stores what kind of environment the user selected. Generalize all application settings so they contain a placeholder that covers the part you need to change between dev/test and replace that part in your code.

Example: Create a setting for a log file that should be environment specific. The value for this setting could be

"[Environment]\MyProgram\Logs"

Then, in your code you'd use someting like this to create the real path:

string logPath = Properties.Settings.Default.LogPath.Replace("[Environment]", IsDevEnvironment ? "C:\\DevEnvironment\\Test", "C:\\TestEnvironment");

Of course you'd fill in the real values here. Please note that they can also come from application- or user-settings.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
1

In General scenario it is not a good practice to change the configuration value at a runtime.

But still if it is requirement and there is not any other way to overcome the situation then you can update the App.Config file :

Below is the code sample of updating value of the AppSettings :

var config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
if (config.AppSettings == null || config.AppSettings.Settings == null)
{
    return;
}

config.AppSettings.Settings["Key"].Value = "My Value";
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);

Suggestion: Instead of above, I would suggest to create xml or text file and store it in AppData. And next time when you need it, you can get data from that file.

This shows how to write file in AppData folder.

Community
  • 1
  • 1
SpiderCode
  • 10,062
  • 2
  • 22
  • 42
  • How do you think to get around the limitation of non-admins not being allowed to write to the program files folder? – Thorsten Dittmar Nov 27 '13 at 13:19
  • By the way, while I appreciate that you mention it's not good practice: It is a breach of the MS coding guidelines and will fail in most cases! Also: there is no scenario that would require any user space application (except for setups) to write to the program files folder. Not one. And as long as developers do it anyway, Windows won't ever be secure. – Thorsten Dittmar Nov 27 '13 at 13:27
  • If I would have the same case, I would write data in AppData. – SpiderCode Nov 27 '13 at 13:34