Note that there are some explanatory texts on larger screens.

plurals
  1. POChange App.config during installation
    primarykey
    data
    text
    <p>I have XML-file with settings like this</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;configuration&gt; &lt;configSections&gt; &lt;sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" &gt; &lt;section name="UpdateReportService.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt; &lt;/sectionGroup&gt; &lt;/configSections&gt; &lt;applicationSettings&gt; &lt;UpdateReportService.Properties.Settings&gt; &lt;setting name="Path" serializeAs="String"&gt; &lt;value&gt;C:\1&lt;/value&gt; &lt;/setting&gt; &lt;setting name="Branch" serializeAs="String"&gt; &lt;value&gt;200&lt;/value&gt; &lt;/setting&gt; &lt;setting name="b204" serializeAs="String"&gt; &lt;value&gt;192.168.1.55&lt;/value&gt; &lt;/setting&gt; &lt;setting name="b200" serializeAs="String"&gt; &lt;value&gt;192.168.0.83&lt;/value&gt; &lt;/setting&gt; &lt;setting name="Hour" serializeAs="String"&gt; &lt;value&gt;11&lt;/value&gt; &lt;/setting&gt; &lt;/UpdateReportService.Properties.Settings&gt; &lt;/applicationSettings&gt; &lt;/configuration&gt; </code></pre> <p>And I'd like to change some values to values typed by user during install program.</p> <p>I find example on VB and try convert it to c#:</p> <pre><code>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); } } } </code></pre> <p>But get error "inaccessible due to its protection level" at this place:</p> <pre><code>SettingElement Elem = applicationSettingsSection.Settings["Branch"]; </code></pre> <p>So, is it possible on c# to access to section in App.config and to change it.</p> <hr> <p><strong>Upd. 2012.02.10</strong></p> <p>i've solved problem this way:</p> <pre><code>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); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload