Visual Studio 插件有配置文件吗?

5
创建Visual Studio Add-In时,如何利用app.config文件?如果我将其添加到项目并部署它,然后当Add-In运行并且我通过ConfigurationManager.AppSettings以编程方式尝试访问它时,它没有获取Add-In的配置文件。
我是否做错了什么,或者还有其他方法可以访问基于文件的配置设置来进行Add-In?
1个回答

8

ConfigurationManager.AppSettings会获取你所加载的AppDomain的配置文件。该配置文件通常是与入口可执行文件相关联的文件。在你的情况下,你无法控制入口可执行文件或运行的AppDomain,因此无法使用ConfigurationManager.AppSettings。

你的问题基本上可以归结为“如何将配置文件与DLL关联?”(C# Dll config file)。你需要做两件事:

  1. Add an Application Configuration File item to your project and make sure you deploy it to the same folder as your DLL.
  2. Access the config file from your DLL using code like this:

     string pluginAssemblyPath = Assembly.GetExecutingAssembly().Location;
     Configuration configuration = ConfigurationManager.OpenExeConfiguration(pluginAssemblyPath);
     string someValue = configuration.AppSettings.Settings["SomeKey"].Value;
    

这对于没有使用影子复制加载的常规DLL肯定是有效的。我不确定VS如何加载其插件。如果遇到问题,请告诉我,我可以发布一些针对通过影子复制加载的DLL的解决方案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接