How to write to the main exe's .config userSettings section?

前端 未结 2 997
耶瑟儿~
耶瑟儿~ 2020-12-03 09:17

Is there any supported API in .NET 2.0 for writing to the userSettings section of the main exe\'s .config file?

Th

相关标签:
2条回答
  • 2020-12-03 09:40

    I think yes - manually writing to the .config file is the only way. Or you can let the administrator edit the .config-file himself.

    0 讨论(0)
  • 2020-12-03 09:43

    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.

    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);
    } 
    

    Given a .config with the following content:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <configSections>
            <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                <section name="MyAssembly.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
            </sectionGroup>
        </configSections>
        <userSettings>
            <MyAssembly.Properties.Settings>
                <setting name="SqlConnectionString" serializeAs="String">
                    <value>Server=(local);Database=myDatabase;Integrated Security=true;</value>
                </setting>
            </MyAssembly.Properties.Settings>
        </userSettings>
    </configuration>
    

    You would use it like this:

    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();
    }
    
    0 讨论(0)
提交回复
热议问题