如何在另一个appdomain中加载dll的配置文件

前端之家收集整理的这篇文章主要介绍了如何在另一个appdomain中加载dll的配置文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个应用程序,需要以dll的形式加载一个加载项. dll需要从配置(app.config)文件获取其配置信息.我想动态找出app.config文件名称,据我所知,这样做的方法是AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

但是,由于它是在父应用程序中托管的,因此从上面的代码获取配置文件是(parentapplication).exe.config.我无法在父应用程序中加载另一个appdomain,但我想更改appdomain的配置文件详细信息.我该怎么做才能得到dll的配置文件

解决方法

好吧,最后,我设法破解了一些对我有用的东西.也许这会有所帮助;

使用Assembly.GetExecutingAssembly,从具有我想要读取的配置文件的DLL中,我可以使用.CodeBase查找DLL之前我为其启动新AppDomain的位置. * .dll
.config位于同一文件夹中.

然后必须转换URI(如.CodeBase看起来像“file://path/assembly.dll”)来获取ConfigurationManager的LocalPath(它不喜欢Uri格式的字符串).

try
{
    string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
    string originalAssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
    Uri uri = new Uri(String.Format("{0}\\{1}.dll",originalAssemblyPath,assemblyName));
    string dllPath = uri.LocalPath;

    configuration = ConfigurationManager.OpenExeConfiguration(dllPath);
}
catch { }

猜你在找的Windows相关文章