Program.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Xml.Linq;
  7. namespace SwitchMode
  8. {
  9. class Program
  10. {
  11. static void Main (string [] args) {
  12. string file = args [0];
  13. string mode = args [1];
  14. XElement config = XElement.Load(file,LoadOptions.PreserveWhitespace);
  15. XElement appSettings = config.Element("appSettings");
  16. if (appSettings == null) {
  17. appSettings = new XElement ("appSettings");
  18. config.Add (appSettings);
  19. }
  20. var results = from el in appSettings.Elements ()
  21. where el.Attribute ("key").Value == "onlyClients"
  22. select el;
  23. XElement onlyClients;
  24. if (results.Count() == 1)
  25. onlyClients = results.First ();
  26. else if (results.Count() == 0) {
  27. onlyClients = new XElement ("add");
  28. onlyClients.SetAttributeValue ("key", "onlyClients");
  29. appSettings.Add (onlyClients);
  30. }
  31. else
  32. throw new Exception ("Too many onlyClients appSettings clauses");
  33. if (mode == "client")
  34. onlyClients.SetAttributeValue("value","true");
  35. else if (mode == "inproc")
  36. onlyClients.SetAttributeValue ("value", "false");
  37. else {
  38. throw new Exception ("Unrecognized mode: " + mode);
  39. }
  40. config.Save (file,SaveOptions.DisableFormatting);
  41. Console.WriteLine ("Successfully switched to mode : " + mode);
  42. }
  43. }
  44. }