AppSettings from custom files

狂风中的少年 提交于 2019-12-23 10:20:05

问题


I am struggling with the configuration and setting classes in .NET 2.0

If the following is contaned in a file called app.config

<config>
<appSettings>
<add key="Foo" value="Hello World!"/>
</appSettings>
</config>

I know I can access the appSetting by

// this returns "Hello World!"
ConfigurationManager.AppSettings["Foo"]

However if the file is called app1.config (or any other name) I cannot access the appSetting. As long as I understand, with ConfigurationManager.OpenExeConfiguration I should read custom config setting files.

Configuration conf = ConfigurationManager.OpenExeConfiguration(@"..\..\app1.config");
// this prints an empty string.
Console.WriteLine(conf.AppSettings.Settings["Foo"]);

However conf.AppSettings.Settings["Foo"] returns an empty string.

I have also tried the following code but no success

ExeConfigurationFileMap exeFileMap = new ExeConfigurationFileMap();
exeFileMap.ExeConfigFilename = System.IO.Directory.GetCurrentDirectory()
                                                        + "\\App1.config";
Configuration myConf = ConfigurationManager.OpenMappedExeConfiguration
                                 (exeFileMap, ConfigurationUserLevel.None);
// returns empty string as well
Console.WriteLine(myConf.AppSettings.Settings["Foo"]);

How to read setting from a file not called app.config?


回答1:


I have created custom file myCustomConfiguration and changes its property Copy to Output Directory to true

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Foo" value="Hello World!"/>
  </appSettings>
</configuration>

In CS file

static void Main(string[] args)
{
    var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "myCustomConfiguration.config");
    Dictionary<string, string> dictionary = GetNameValueCollectionSection("appSettings", filePath);

    //To get your key do dictionary["Foo"]
            Console.WriteLine(dictionary["Foo"]);

    Console.ReadLine();
}

private static Dictionary<string, string> GetNameValueCollectionSection(string section, string filePath)
{
    var xDoc = new XmlDocument();
    var nameValueColl = new Dictionary<string, string>();

    var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = filePath };
    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

    string xml = config.GetSection(section).SectionInformation.GetRawXml();
    xDoc.LoadXml(xml);
    XmlNode xList = xDoc.ChildNodes[0];
    foreach (XmlNode xNodo in xList.Cast<XmlNode>().Where(xNodo => xNodo.Attributes != null))
    {
        nameValueColl.Add(xNodo.Attributes[0].Value, xNodo.Attributes[1].Value);
    }

    return nameValueColl;
}

Although this is working but I am also looking for better approach.




回答2:


You should make use of a Settings-File, it's way more comfortable to use, has save and load methods and you can name it what ever you want. Eg. my Settings-File is called "EditorSettings.settings" and I access its properties like this:

MyNamespace.MyProject.EditorSettings.Default.MyProperty1


来源:https://stackoverflow.com/questions/17336641/appsettings-from-custom-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!