Monday, August 29, 2011

Use regular expressions to save time.

Replace in files...

Utils.GetConfigValue\({"[a-zA-Z0-9_ ]+"}\)

to

System.Configuration.ConfigurationManager.ConnectionStrings\[\1\]

Wednesday, August 3, 2011

Sorting entries in config appsettings.

   How to sort AppSettings without manually missing any keys or important information.
Code Snippet
  1.   private void WriteAppSettingsToFile(string filename)
  2.     {
  3.  
  4.         List<string> lstConfigKeys = new List<string>();
  5.         foreach (string appKey in ConfigurationManager.AppSettings.AllKeys)
  6.         {
  7.             lstConfigKeys.Add(appKey);
  8.         }
  9.  
  10.         lstConfigKeys.Sort();
  11.  
  12.         using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\" + filename + ".txt"))
  13.         {
  14.             foreach (string key in lstConfigKeys)
  15.             {
  16.                 file.WriteLine(string.Format("'{0}', -- {1}", key, ConfigurationManager.AppSettings.Get(key)));
  17.             }
  18.         }
  19.     }

You can modify the code to write the keys in the form of   . Then copy and paste the output to your actual config file. You have all the keys in sorted order.