Change App.config during installation

I have an XML file with these settings

<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <section name="UpdateReportService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </sectionGroup> </configSections> <applicationSettings> <UpdateReportService.Properties.Settings> <setting name="Path" serializeAs="String"> <value>C:\1</value> </setting> <setting name="Branch" serializeAs="String"> <value>200</value> </setting> <setting name="b204" serializeAs="String"> <value>192.168.1.55</value> </setting> <setting name="b200" serializeAs="String"> <value>192.168.0.83</value> </setting> <setting name="Hour" serializeAs="String"> <value>11</value> </setting> </UpdateReportService.Properties.Settings> </applicationSettings> </configuration> 

And I would like to change some values ​​to the values ​​entered by the user during the setup program.

I find an example on VB and try to convert it to C #:

 namespace InstallConfigurator { [RunInstaller(true)] public class SettingsClass : Installer { public override void Install(System.Collections.IDictionary stateSaver) { Configuration config = ConfigurationManager.OpenExeConfiguration(Context.Parameters["TARGETDIR"].ToString() + "UpdateReportService.exe"); ClientSettingsSection applicationSettingsSection = (ClientSettingsSection)config.SectionGroups["applicationSettings"].Sections["UpdateReportService.Properties.Settings"]; SettingElement Elem = applicationSettingsSection.Settings["Branch"]; applicationSettingsSection.Settings.Remove(Elem); Elem.Value.ValueXml.InnerXml = "30000"; applicationSettingsSection.Settings.Add(Elem); config.Save(ConfigurationSaveMode.Full); } } } 

But get the error β€œunavailable due to protection level” in this place:

 SettingElement Elem = applicationSettingsSection.Settings["Branch"]; 

So, is it possible in C # to access a section in App.config and change it.


Upd. 2012.02.10

I solved the problem this way:

 namespace InstallConfigurator { [RunInstaller(true)] public class SettingsClass : Installer { public override void Install(System.Collections.IDictionary stateSaver) { string xml = Context.Parameters["TARGETDIR"].ToString() + "UpdateReportService.exe.config"; XmlDocument document = new XmlDocument(); document.Load(xml); XPathNavigator navigator = document.CreateNavigator(); XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable); foreach (XPathNavigator nav in navigator.Select(@"/configuration/applicationSettings/UpdateReportService.Properties.Settings/setting[@name='Branch']/value")) { nav.SetValue(Context.Parameters["BRANCH"].ToString()); } foreach (XPathNavigator nav in navigator.Select(@"/configuration/applicationSettings/UpdateReportService.Properties.Settings/setting[@name='Path']/value")) { nav.SetValue(Context.Parameters["PATH"].ToString()); } document.Save(xml); } } } 
+4
source share
1 answer

In a similar project, I do it a little differently:

  • Submit your installation without the file "myapp.exe.config".
  • Instead, send the file "myapp.exe.config.default", which contains placeholders such as " {Branch} ".
  • During setup, load the file myapp.exe.config.default as a string into memory.
  • Replace the placeholders with the actual values ​​(for example, your " 30000 ").
  • Record the replaced string as the actual file "myapp.exe.config".
  • Bonus: before recording the configuration, check if any existing configuration file is present and copy it as a backup to save the previous version.

In our applications, this is pretty smooth.

+2
source

Source: https://habr.com/ru/post/1395565/


All Articles