如何写入主exe文件的.config userSettings部分?如何写、部分、文件、exe

2023-09-02 02:01:00 作者:沵不要肆意挥洒誐的爱情 ̄

有没有在.NET 2.0支持的任何API 写到 userSettings 在主要exe文件的config文件的栏目?

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

该方案是:

的WinForms 2.0应用程序。

Winforms 2.0 application.

我有一个设置(数据库连接字符串,如果您需要知道),有用户级范围。这意味着每个用户都有一个的用户的时候,用户保存的设定值由.NET创建config文件。

I have a setting (a database connection string, if you need to know) that has user level scope. This means that each user has a user.config file created by .net when the user saves the value of the setting.

对于运行应用程序的第一次新的用户,应用程序的主exe文件的config文件包含了用户设置部分的默认值。这部分是由当设置在项目属性的设置选项卡上创建Visual Studio创建的。

For new users that run the application for the first time, the main exe's .config file of the application contains a default value in the user settings section. This section is created by visual studio when the setting is created in the Settings tab of the project properties.

现在,我想允许在计算机中的管理员用户能够更改默认值,为新用户。只有管​​理员都会有这个选项,因为普通用户没有权限写入主exe文件的config文件呢。

Now, I want to allow any Administrator user in the computer to be able to change the default value for new users. Only Administrators will have this option, because regular users don't have permission to write to the main exe's .config file anyway.

我已经找到了如何编写用户设置用户的config文件,以及如何写入主config文件的appSettings部分。但是,试图找出如何写主的.config的userSettings节

I have found how to write user settings to the user's .config file, and how to write to the appSettings section of the main .config file. But my googling has failed when trying to find out how to write to the userSettings section of the main .config

是我唯一的机会未能回的System.Xml和做手工加载的.config在一个XmlDocument?

Is my only chance failing back to System.Xml and do it manually loading the .config in an XmlDocument?

推荐答案

一些研究,我想出了这个解决方案之后。这是一个有点低的水平,但仍经历了.NET配置API,而无需手动解析.config文件。

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

考虑到与以下内容的.config:

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>

您会使用这样的:

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