- 我有 .NET 程序集,其中一些类标记为
ComVisible
- 此程序集已注册到
regasm/codebase "assembly_path"
- 我有 app.config 名称(实际上是 -
MyAssemblyName.dll.config
),它位于程序集的文件夹中 - 我通过
ConfigurationManager.AppSettings["SettingName"]
访问程序集中的 appSettings - 我有 VBScript 文件,它通过
CreateObject("...")
创建我的 COM 对象
- 创建对象时(从 VBScript),
ConfigurationManager.AppSettings["SettingName"]
返回 null。看起来程序集没有看到配置文件。
我应该怎么做才能使其可行?
最佳答案
正如 Komyg 所说,一种可能的方法是直接读取配置文件,而不是使用 ConfigurationManager 的嵌入式行为。对于那些会遇到同样问题的人:而不是
ConfigurationManager.AppSettings["SettingName"]
你可以使用:
var _setting = ConfigurationManager.AppSettings["SettingName"];
// If we didn't find setting, try to load it from current dll's config file
if (string.IsNullOrEmpty(_setting))
{
var filename = Assembly.GetExecutingAssembly().Location;
var configuration = ConfigurationManager.OpenExeConfiguration(filename);
if (configuration != null)
_setting = configuration.AppSettings.Settings["SettingName"].Value;
}
这样您将始终使用位于程序集文件夹中的文件 YourAssemblyName.dll.config
中的读取设置。它还允许您使用 app.config
的其他功能(如 appSetting
的 file
属性),如果您将使用 XPath 或类似的东西。
关于c# - ComVisible .NET 程序集和 app.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8656317/