config读取用户设置

config读取用户设置

本文介绍了如何从差异的应用程序的app.config读取用户设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms有一个app.config有一堆被设置在运行时,并保存用户范围设置的.exe文件。
我希望能够使用的WinForms应用更改并保存设置,然后点击按钮做基础上,这些设置一些工作。
我也想从在Sep同config文件读取用户设置。控制台应用程序,所以我可以安排工作要做作为一个计划任务。
什么是能够做到这一点的最好办法

I have a WinForms .exe with an App.config that has a bunch of User Scoped Settings that are set at runtime and saved.I want to be able to use the WinForms app to change and save the settings and then click on button to do some work based on the those settings.I also want to read the user settings in the same .config file from a sep. console app so I can schedule to work to be done as a scheduled task.What is the best way to be able to do this?

更​​新:
我试图在一些描述使用ConfigurationManager.OpenExeConfiguration的reccommendation 。像这样的答案

Update:I tried the reccommendation of using ConfigurationManager.OpenExeConfiguration as described in some of the answers like so.

Configuration config = ConfigurationManager.OpenExeConfiguration("F:\\Dir\\App.exe");



但是当我尝试检索用户设置像这样。

but when I try to retrieve a User Setting like so.

string result = config.AppSettings.Settings["DB"].ToString();



我得到一个空引用错误。

I get a Null reference error.

从代码的EXE但是下面正确返回数据库名称。

From code in the exe however the following correctly returns the DB name.

Properties.Settings.Default.DB

我要去哪里错了。

更新2:

因此,基于一些答案下面我现在可以使用以下方法来检索文件user.config我感兴趣的是从九月的部分的原始XML。 。ConsoleApp

So based on some of the answers below I now can use the following to retrieve the raw XML of the section of the user.config file I am interested in from sep. ConsoleApp.

System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"D:\PathHere\user.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap,ConfigurationUserLevel.None);
System.Configuration.DefaultSection configSection = (System.Configuration.DefaultSection)config.GetSection("userSettings");
string result = configSection.SectionInformation.GetRawXml();
Console.WriteLine(result);



不过,我仍然无法直接拔掉的价值,因为我感兴趣的特定元素。

But I am still unable to just pull the value for the specific element I am interested in.

推荐答案

这应该这样做:

var clientSettingsSection = (System.Configuration.ClientSettingsSection)(ConfigurationManager.GetSection("userSettings/YourApplicationName.Properties.Settings"));
var setting = clientSettingsSection.Settings.Get("yourParamName_DB_");
string yourParamName_DB = ((setting.Value.ValueXml).LastChild).InnerText.ToString();

这篇关于如何从差异的应用程序的app.config读取用户设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:00