Asp.net 使用ConfigurationManager从任意位置加载配置

Asp.net 使用ConfigurationManager从任意位置加载配置,asp.net,configuration,asp-classic,Asp.net,Configuration,Asp Classic,我正在开发一个数据访问组件,该组件将在包含经典ASP和ASP.NET页面的网站中使用,并且需要一种管理其配置设置的好方法 我想使用一个自定义的配置部分,对于ASP.NET页面来说,这非常有用。但是,当从经典ASP页面通过COM互操作调用该组件时,该组件不会在ASP.NET请求的上下文中运行,因此不了解web.config 有没有办法告诉ConfigurationManager仅从任意路径加载配置(例如。\web.config如果我的程序集位于/bin文件夹中)?如果有,那么我认为如果默认的Con

我正在开发一个数据访问组件,该组件将在包含经典ASP和ASP.NET页面的网站中使用,并且需要一种管理其配置设置的好方法

我想使用一个自定义的
配置部分
,对于ASP.NET页面来说,这非常有用。但是,当从经典ASP页面通过COM互操作调用该组件时,该组件不会在ASP.NET请求的上下文中运行,因此不了解web.config

有没有办法告诉
ConfigurationManager
仅从任意路径加载配置(例如
。\web.config
如果我的程序集位于
/bin
文件夹中)?如果有,那么我认为如果默认的
ConfigurationManager.GetSection
为我的自定义节返回
null
,那么我的组件就可以回到这个状态

欢迎采用任何其他方法

试试这个:

System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(strConfigPath); //Path to your config file
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);

除了Ishmael的答案之外,方法
OpenMappedMachineConfiguration()
将始终返回
配置
对象。因此,要检查它是否已加载,您应该检查
HasFile
属性,其中true表示它来自文件。

Ishmael的答案通常是有效的,但是我发现了一个问题,即使用
OpenMappedMachineConfiguration
似乎会丢失从machine.config继承的节组。这意味着您可以访问自己的自定义部分(这是所有OP想要的),但不能访问正常的系统部分。例如,此代码将不起作用:

ConfigurationFileMap fileMap = new ConfigurationFileMap(strConfigPath);
Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
MailSettingsSectionGroup thisMail = configuration.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;  // returns null
基本上,如果您对
配置.SectionGroups
进行监视,您将看到system.net未注册为SectionGroups,因此通过正常通道几乎无法访问它

我发现有两种方法可以解决这个问题。第一个,我不喜欢,是通过将系统节组从machine.config复制到您自己的web.config中来重新实现它们

<sectionGroup name="system.net" type="System.Net.Configuration.NetSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <sectionGroup name="mailSettings" type="System.Net.Configuration.MailSettingsSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    <section name="smtp" type="System.Net.Configuration.SmtpSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </sectionGroup>
</sectionGroup>

我敢说,这里提供的答案,无论是我的还是伊什梅尔的,都没有完全按照.NET设计者的意图使用这些功能。但是,这似乎对我有效。

我为word托管的.nET组件提供了如下配置值

正在MS Word中调用/承载的.NET类库组件。为了向我的组件提供配置值,我在C:\Program Files\Microsoft Office\OFFICE11文件夹中创建了winword.exe.config。您应该能够像在传统的.NET中一样读取配置值

string sMsg = System.Configuration.ConfigurationManager.AppSettings["WSURL"];

另一种解决方案是覆盖默认的环境配置文件路径

我发现它是非平凡路径配置文件加载的最佳解决方案,特别是将配置文件附加到dll的最佳方式

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", <Full_Path_To_The_Configuration_File>);
更多详情请访问

此外,它还提供了一个优秀的解决方案,包括可刷新的代码 应用程序配置和一个
IDisposable
对象将其重置回原始状态。用这个 解决方案中,您可以保持临时应用程序配置的作用域:

using(AppConfig.Change(tempFileName))
{
    // tempFileName is used for the app config during this context
}

对于ASP.NET,请使用WebConfiguration Manager:

var config = WebConfigurationManager.OpenWebConfiguration("~/Sites/" + requestDomain + "/");
(..)
config.AppSettings.Settings["xxxx"].Value;
使用XML处理:

var appPath = AppDomain.CurrentDomain.BaseDirectory;
var configPath = Path.Combine(appPath, baseFileName);;
var root = XElement.Load(configPath);

// can call root.Elements(...)

这应该可以做到:

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "newAppConfig.config);

来源:

接受的答案是错误的

它在访问AppSettings属性时引发以下异常:

无法将“System.Configuration.DefaultSection”类型的对象强制转换为“System.Configuration.AppSettingsSection”类型

以下是正确的解决方案:

System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "YourFilePath";
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

您还可以出于相同目的使用ConfigurationManager.OpenExeConfiguration(字符串)重载。请参阅:请参阅,这也适用于加载web.config文件。对于与任务相关的控制台应用程序,我使用它来加载web.config而不是app.config这个(以及这里的其他答案)对我不适用。我已经在program.cs-function:Main()中添加了代码。“我的配置”包含程序集版本重定向(请参阅),但手动更改配置时重定向不起作用。是否使用“应用程序配置文件”?这是一个非常干净且易于实现的解决方案,对我来说非常有效。如何以编程方式获取托管在路径C:\Portals\App1\v2中的ASP.NET WebForms应用程序的strConfigPath值,以及C:\Portals\App1\v2\web.config中的配置文件?@Kiquenet:问题的关键是strConfigPath是一个任意位置。换句话说,您可以决定路径是什么,而不是依赖框架从常规位置加载配置文件。我假设会为您的解决方案中的任何文件提供绝对位置。可能
var config=System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(“~/Web.config”)@Kiquenet绝对正确。是的,这绝对是正确的答案!感谢您发布答案。我认为System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration更为正确,但它不适用于.NET Core,因此在这种情况下,此答案似乎完成了工作。OpenWebConfiguration的第一个参数就是路径(文件夹)。要指定文件名,需要第二个参数
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "newAppConfig.config);
System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "YourFilePath";
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);