c# – 如何写入主exe的.config userSettings部分?

前端之家收集整理的这篇文章主要介绍了c# – 如何写入主exe的.config userSettings部分?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
.NET 2.0中是否有任何支持的API用于写入主exe的.config文件的userSettings部分?

场景是:

Winforms 2.0应用程序.

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

对于第一次运行应用程序的新用户,应用程序的主exe .config文件用户设置部分中包含默认值.在项目属性的“设置”选项卡中创建设置时,此部分由visual studio创建.

现在,我想允许计算机中的任何管理员用户能够更改新用户的默认值.只有管​​理员才有此选项,因为普通用户无权写入主exe的.config文件.

我已经找到了如何将用户设置写入用户的.config文件,以及如何写入主.config文件的appSettings部分.但是当我试图找出如何写入主.config的userSettings部分时,我的谷歌搜索失败了

我唯一的机会是失败回System.Xml并在XmlDocument中手动加载.config吗?

解决方法

经过一番研究,我想出了这个解决方案.它有点低级,但仍然可以通过.NET配置API而无需手动解析.config文件.
  1. static void SaveUserSettingDefault(string clientSectionName,string settingName,object settingValue)
  2. {
  3. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  4.  
  5. // find section group
  6. ConfigurationSectionGroup group = config.SectionGroups[@"userSettings"];
  7. if (group == null) return;
  8.  
  9. // find client section
  10. ClientSettingsSection clientSection = group.Sections[clientSectionName] as ClientSettingsSection;
  11. if (clientSection == null) return;
  12.  
  13. // find setting element
  14. SettingElement settingElement = null;
  15. foreach (SettingElement s in clientSection.Settings)
  16. {
  17. if (s.Name == settingName)
  18. {
  19. settingElement = s;
  20. break;
  21. }
  22. }
  23. if (settingElement == null) return;
  24.  
  25. // remove the current value
  26. clientSection.Settings.Remove(settingElement);
  27.  
  28. // change the value
  29. settingElement.Value.ValueXml.InnerText = settingValue.ToString();
  30.  
  31. // add the setting
  32. clientSection.Settings.Add(settingElement);
  33.  
  34. // save changes
  35. config.Save(ConfigurationSaveMode.Full);
  36. }

给定一个包含以下内容的.config:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <configSections>
  4. <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup,System,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089" >
  5. <section name="MyAssembly.Properties.Settings" type="System.Configuration.ClientSettingsSection,PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
  6. </sectionGroup>
  7. </configSections>
  8. <userSettings>
  9. <MyAssembly.Properties.Settings>
  10. <setting name="sqlConnectionString" serializeAs="String">
  11. <value>Server=(local);Database=myDatabase;Integrated Security=true;</value>
  12. </setting>
  13. </MyAssembly.Properties.Settings>
  14. </userSettings>
  15. </configuration>

你会像这样使用它:

  1. if (RunningAsAdmin) // save value in main exe's config file
  2. {
  3. SaveUserSettingDefault(@"MyAssembly.Properties.Settings",@"sqlConnectionString",theNewConnectionString);
  4. }
  5. else // save setting in user's config file
  6. {
  7. Settings.Default. sqlConnectionString = theNewConnectionString;
  8. Settings.Default.Save();
  9. }

猜你在找的C#相关文章