Ada*_*dam 115 c# configuration
我知道我可以使用静态ConfigurationManager.OpenExe(exePath)
方法打开与程序集相关的配置文件,但我只想打开与程序集无关的配置.只是一个标准的.NET配置文件.
Oli*_*ver 235
Ricky发布的文章非常好,但不幸的是他们没有回答你的问题.
要解决您的问题,您应该尝试这段代码:
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = @"d:\test\justAConfigFile.config.whateverYouLikeExtension";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
Run Code Online (Sandbox Code Playgroud)
如果需要访问配置中的值,可以使用索引运算符:
config.AppSettings.Settings["test"].Value;
Run Code Online (Sandbox Code Playgroud)
配置文件只是一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后通过以下方式检索值:
// retrieve appSettings node
XmlNode node = doc.SelectSingleNode("//appSettings");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
103631 次 |
最近记录: |