ConfigurationManager.OpenExeConfiguration - loads the wrong file?

后端 未结 4 895
旧巷少年郎
旧巷少年郎 2020-12-01 13:42

I have added multiple app.config (each with a differet name) files to a project, and set them to copy to the output directory on each build.

I try and access the con

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 14:23

    To avoid this problem altogether, you can read in the config file as an XML file, for example:

    using System.Xml;
    using System.Xml.XPath;    
    
    XmlDocument doc = new XmlDocument();
    doc.Load(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\..\\..\\..\\MyWebProject\\web.config");
    string value = doc.DocumentElement.SelectSingleNode("/configuration/appSettings/add[@key='MyKeyName']").Attributes["value"].Value;
    

提交回复
热议问题