C# 如何写入主 exe 的 .config userSettings 部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/629143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write to the main exe's .config userSettings section?
提问by Sergio Acosta
Is there any supported API in .NET 2.0 for writingto the userSettingssection of the main exe's .config file?
.NET 2.0 中是否有任何受支持的 API 用于写入主 exe 的 .config 文件的userSettings部分?
The scenario is:
场景是:
Winforms 2.0 application.
Winforms 2.0 应用程序。
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.
我有一个具有用户级别范围的设置(数据库连接字符串,如果您需要知道)。这意味着当用户保存设置的值时,每个用户都有一个由 .net 创建的用户.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 文件在用户设置部分包含一个默认值。当在项目属性的“设置”选项卡中创建设置时,此部分由 Visual Studio 创建。
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.
现在,我希望允许计算机中的任何管理员用户能够更改新用户的默认值。只有管理员才有这个选项,因为普通用户无论如何都没有写入主 exe 的 .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
我已经找到了如何将用户设置写入用户的 .config 文件,以及如何写入主 .config 文件的 appSettings 部分。但是当我试图找出如何写入主 .config 的 userSettings 部分时,我的谷歌搜索失败了
Is my only chance failing back to System.Xml and do it manually loading the .config in an XmlDocument?
我唯一的机会是失败回到 System.Xml 并在 XmlDocument 中手动加载 .config 吗?
采纳答案by Sergio Acosta
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.
经过一番研究,我想出了这个解决方案。它有点低级,但仍然可以通过 .NET 配置 API,而无需手动解析 .config 文件。
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:
给定具有以下内容的 .config:
<?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();
}
回答by Carsten
I think yes - manually writing to the .config file is the only way. Or you can let the administrator edit the .config-file himself.
我认为是的 - 手动写入 .config 文件是唯一的方法。或者您可以让管理员自己编辑 .config 文件。