简体   繁体   English

是否有Visual Studio加载项的配置类型文件?

[英]Is there a config type file for Visual Studio Add-In?

When creating a Visual Studio Add-In, how can you utilise an app.config for the add-in. 创建Visual Studio加载项时,如何使用app.config加载项。 If I add one to the project and deploy it then when the Add-In runs and I programmatically try to access it via the ConfigurationManager.AppSettings its not picking up the config file for the add-in. 如果我将一个添加到项目并进行部署,那么当加载项运行时,我以编程方式尝试通过ConfigurationManager.AppSettings访问它,它不会获取加载项的配置文件。
Am I doing something wrong or is there another means to access file based configuration settings for an add-in? 我是做错了还是有其他方法可以访问加载项的基于文件的配置设置?

ConfigurationManager.AppSettings picks up the configuration file for the AppDomain you are loaded into. ConfigurationManager.AppSettings选择要加载的AppDomain的配置文件。 That config file is typically the one associated with the entry point executable. 该配置文件通常是与入口点可执行文件相关联的文件。 In your case, you don't control the entry point executable nor the AppDomain you run in so you can't use ConfigurationManager.AppSettings. 在您的情况下,您不控制入口点可执行文件或您运行的AppDomain,因此您无法使用ConfigurationManager.AppSettings。

You question basically boils down to "How can I have a config file associated with a DLL?" 你的问题基本归结为“我怎么能有一个与DLL相关的配置文件?” ( C# Dll config file ). C#Dll配置文件 )。 You need to do two things: 你需要做两件事:

  1. Add an Application Configuration File item to your project and make sure you deploy it to the same folder as your DLL. 将应用程序配置文件项添加到项目中,并确保将其部署到与DLL相同的文件夹中。
  2. Access the config file from your DLL using code like this: 使用以下代码从DLL访问配置文件:

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

That will definitely work for regular DLLs that are not loaded using shadow copy. 这肯定适用于未使用卷影副本加载的常规DLL。 I'm not sure how VS loads its plugins. 我不确定VS如何加载它的插件。 If you run into problems, let me know and I can post a work around for DLLs that get loaded via shadow copy. 如果您遇到问题,请告诉我,我可以发布一个解决方案来处理通过卷影副本加载的DLL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM