Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Configuration;
- namespace ChangeAppConfigProperty
- {
- class ChangeAppConfigProperty
- {
- private static string pathToExeFile;
- private static string keyName;
- private static string newValue;
- static void Main(string[] args)
- {
- if (args.Length < 3)
- {
- Console.WriteLine("Updates an application configuration setting file property.");
- Console.WriteLine("Usage: ChangeAppConfigProperty.exe /exeFile:{PathToExe.exe} /key:{AppSettingsKeyName} /value:{newValue} [/debug]");
- }
- foreach (string arg in args)
- {
- if (arg.ToUpperInvariant().Contains("/exeFile:".ToUpperInvariant()))
- {
- pathToExeFile = arg.Split(':')[1];
- }
- if (arg.ToUpperInvariant().Contains("/key:".ToUpperInvariant()))
- {
- keyName = arg.Split(':')[1];
- }
- if (arg.ToUpperInvariant().Contains("/value:".ToUpperInvariant()))
- {
- newValue = arg.Split(':')[1];
- }
- if (arg.ToUpperInvariant().Contains("/debug".ToUpperInvariant()))
- {
- Debugger.Break();
- }
- }
- //save a backup copy first.
- var cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile);
- cfg.SaveAs(cfg.FilePath + "." + DateTime.Now.ToFileTime() + ".bak");
- //reopen the original config again and update it.
- cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile);
- var setting = cfg.AppSettings.Settings[keyName];
- setting.Value = newValue;
- //save the changed configuration.
- cfg.Save(ConfigurationSaveMode.Full);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement