Advertisement
Guest User

Mike Atlas

a guest
Dec 23rd, 2009
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Configuration;
  4.  
  5. namespace ChangeAppConfigProperty
  6. {
  7.     class ChangeAppConfigProperty
  8.     {
  9.         private static string pathToExeFile;
  10.         private static string keyName;
  11.         private static string newValue;
  12.         static void Main(string[] args)
  13.         {
  14.             if (args.Length < 3)
  15.             {
  16.                 Console.WriteLine("Updates an application configuration setting file property.");
  17.                 Console.WriteLine("Usage: ChangeAppConfigProperty.exe /exeFile:{PathToExe.exe} /key:{AppSettingsKeyName} /value:{newValue} [/debug]");
  18.             }
  19.             foreach (string arg in args)
  20.             {
  21.                 if (arg.ToUpperInvariant().Contains("/exeFile:".ToUpperInvariant()))
  22.                 {
  23.                     pathToExeFile = arg.Split(':')[1];
  24.                 }
  25.                 if (arg.ToUpperInvariant().Contains("/key:".ToUpperInvariant()))
  26.                 {
  27.                     keyName = arg.Split(':')[1];
  28.                 }
  29.                 if (arg.ToUpperInvariant().Contains("/value:".ToUpperInvariant()))
  30.                 {
  31.                     newValue = arg.Split(':')[1];
  32.                 }
  33.                 if (arg.ToUpperInvariant().Contains("/debug".ToUpperInvariant()))
  34.                 {
  35.                     Debugger.Break();
  36.                 }
  37.             }
  38.  
  39.             //save a backup copy first.
  40.             var cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile);
  41.             cfg.SaveAs(cfg.FilePath + "." + DateTime.Now.ToFileTime() + ".bak");
  42.  
  43.             //reopen the original config again and update it.
  44.             cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile);
  45.             var setting = cfg.AppSettings.Settings[keyName];
  46.             setting.Value = newValue;
  47.  
  48.             //save the changed configuration.
  49.             cfg.Save(ConfigurationSaveMode.Full);
  50.  
  51.         }
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement