Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#访问另一个项目的问题#x27;s app.config_C#_App Config - Fatal编程技术网

C#访问另一个项目的问题#x27;s app.config

C#访问另一个项目的问题#x27;s app.config,c#,app-config,C#,App Config,我有两个项目,一个是应用程序(exe),另一个是库(dll)。我正在从应用程序动态加载库。我正在尝试从DLL中访问exe的app.config。以下是DLL中的代码: config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); object abc = config.Sections["MySection"]; 如果我有一个从程序项目到库项目的引用,那么上面的代码可以正常工作。但如果删除引用,

我有两个项目,一个是应用程序(exe),另一个是库(dll)。我正在从应用程序动态加载库。我正在尝试从DLL中访问exe的app.config。以下是DLL中的代码:

config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
object abc = config.Sections["MySection"];
如果我有一个从程序项目到库项目的引用,那么上面的代码可以正常工作。但如果删除引用,则会得到System.IO.FileNotFoundException

配置的HasFile属性为true,但似乎找不到节或其他内容。所以我想它可能使用了不同的aap.config,我试图手动将app.config路径指定到OpenExeConfiguration中,但仍然得到相同的错误

因此,如何从DLL访问程序的app.config,而不必从程序项目中引用该DLL?

解决方案:

我必须将程序集添加到解析处理程序:

ResolveEventHandler tempResolveEventHandler =
(sender, args) => { return Assembly.LoadFrom(Assembly.GetExecutingAssembly().Location); };

AppDomain.CurrentDomain.AssemblyResolve += tempResolveEventHandler;
//access the app.config here
AppDomain.CurrentDomain.AssemblyResolve -= tempResolveEventHandler;