加载自定义配置文件我知道我可以使用静态ConfigurationManager.OpenExe(exePath)方法打开与程序集相关的配置文件,但我只想打开与程序集无关的配置。只是一个标准的.NET配置文件。
3 回答
子衿沉夜
TA贡献1828条经验 获得超3个赞
但不幸的是他们没有回答你的问题。
要解决您的问题,您应该尝试这段代码:
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();configMap.ExeConfigFilename = @"d:\test\justAConfigFile.config.whateverYouLikeExtension";Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
如果需要访问配置中的值,可以使用索引运算符:
config.AppSettings.Settings["test"].Value;
qq_遁去的一_1
TA贡献1725条经验 获得超7个赞
配置文件只是一个XML文件,您可以通过以下方式打开它:
private static XmlDocument loadConfigDocument(){ XmlDocument doc = null; try { doc = new XmlDocument(); doc.Load(getConfigFilePath()); return doc; } catch (System.IO.FileNotFoundException e) { throw new Exception("No configuration file found.", e); } catch (Exception ex) { return null; }}
然后通过以下方式检索值:
// retrieve appSettings node XmlNode node = doc.SelectSingleNode("//appSettings");
- 3 回答
- 0 关注
- 469 浏览
添加回答
举报
0/150
提交
取消