code_junkie

How can I set application settings at install time (via installer class)

Nov 14th, 2011
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. MessageBox.Show(Properties.Settings.Default.MySetting);
  2.  
  3. Properties.Settings.Default.MySetting = "Foo";
  4. Properties.Settings.Default.Save();
  5.  
  6. MessageBox.Show(Properties.Settings.Default.MySetting);
  7.  
  8. <appSettings file="user.config">
  9. <add key="foo" value="some value unchanged by setup"/>
  10. </appSettings>
  11.  
  12. using System.Collections.Generic;
  13. using System.Text;
  14. using System.Xml;
  15.  
  16. namespace Utils
  17. {
  18. public class ConfigGenerator
  19. {
  20. public static void WriteExternalAppConfig(string configFilePath, IDictionary<string, string> userConfiguration)
  21. {
  22. using (XmlTextWriter xw = new XmlTextWriter(configFilePath, Encoding.UTF8))
  23. {
  24. xw.Formatting = Formatting.Indented;
  25. xw.Indentation = 4;
  26. xw.WriteStartDocument();
  27. xw.WriteStartElement("appSettings");
  28.  
  29. foreach (KeyValuePair<string, string> pair in userConfiguration)
  30. {
  31. xw.WriteStartElement("add");
  32. xw.WriteAttributeString("key", pair.Key);
  33. xw.WriteAttributeString("value", pair.Value);
  34. xw.WriteEndElement();
  35. }
  36.  
  37. xw.WriteEndElement();
  38. xw.WriteEndDocument();
  39. }
  40. }
  41. }
  42. }
  43.  
  44. string configFilePath = string.Format("{0}{1}User.config", targetDir, Path.DirectorySeparatorChar);
  45.  
  46. IDictionary<string, string> userConfiguration = new Dictionary<string, string>();
  47.  
  48. userConfiguration["Server"] = Context.Parameters["Server"];
  49. userConfiguration["Port"] = Context.Parameters["Port"];
  50.  
  51. ConfigGenerator.WriteExternalAppConfig(configFilePath, userConfiguration);
Add Comment
Please, Sign In to add comment