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

查看:138
本文介绍了如何写入主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创建的用户 .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文件在用户设置部分中包含一个默认值。

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部分。但是我的Google搜索失败了,当试图找到如何写到主.config的用户设置部分

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

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

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