Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After some research I came up with this solution. It is a bit low level, but still goes through the .NET configuration API without having to manually parse the .config file.</p> <pre><code>static void SaveUserSettingDefault(string clientSectionName, string settingName, object settingValue) { System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // find section group ConfigurationSectionGroup group = config.SectionGroups[@"userSettings"]; if (group == null) return; // find client section ClientSettingsSection clientSection = group.Sections[clientSectionName] as ClientSettingsSection; if (clientSection == null) return; // find setting element SettingElement settingElement = null; foreach (SettingElement s in clientSection.Settings) { if (s.Name == settingName) { settingElement = s; break; } } if (settingElement == null) return; // remove the current value clientSection.Settings.Remove(settingElement); // change the value settingElement.Value.ValueXml.InnerText = settingValue.ToString(); // add the setting clientSection.Settings.Add(settingElement); // save changes config.Save(ConfigurationSaveMode.Full); } </code></pre> <p>Given a .config with the following content:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;configuration&gt; &lt;configSections&gt; &lt;sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" &gt; &lt;section name="MyAssembly.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /&gt; &lt;/sectionGroup&gt; &lt;/configSections&gt; &lt;userSettings&gt; &lt;MyAssembly.Properties.Settings&gt; &lt;setting name="SqlConnectionString" serializeAs="String"&gt; &lt;value&gt;Server=(local);Database=myDatabase;Integrated Security=true;&lt;/value&gt; &lt;/setting&gt; &lt;/MyAssembly.Properties.Settings&gt; &lt;/userSettings&gt; &lt;/configuration&gt; </code></pre> <p>You would use it like this:</p> <pre><code>if (RunningAsAdmin) // save value in main exe's config file { SaveUserSettingDefault(@"MyAssembly.Properties.Settings", @"SQLConnectionString", theNewConnectionString); } else // save setting in user's config file { Settings.Default. SQLConnectionString = theNewConnectionString; Settings.Default.Save(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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