如何写入主 exe 的 .config userSettings 部分? [英] How to write to the main exe's .config userSettings section?

查看:23
本文介绍了如何写入主 exe 的 .config userSettings 部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET 2.0 中是否有任何支持的 API 用于写入主 exe 的 .config 文件userSettings部分?

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 创建的 user.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 并在 XmlDocument 中手动加载 .config 吗?

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

这篇关于如何写入主 exe 的 .config userSettings 部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆