app.config inside Visual Studio Extension?

房东的猫 提交于 2021-01-28 10:22:30

问题


I have created a Visual Studio extension representing a Visual Studio Project wizard (vsix package). I am attempting to wire up log4net, and this has been unsuccessful. I have chased the issue down to my app.config not being loaded properly.

I have added this to my app.config in my visual studio extension:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="test" value="hello world"/>
  </appSettings>
</configuration>

and within my IWizard implementation, I have added this line of code:

var test = System.Configuration.ConfigurationManager.AppSettings["test"];

However, the test variable above is always null when debugging.

I have verified these things:

  • App.config is set to Build Action: Content
  • App.config is set to Copy to Output Directory: Copy Always
  • App.config is set to Include in VSIX: True
  • App.config file is present in my C:\Users\<user>\AppData\Local\Microsoft\VisualStudio\16.0_...\Extensions\<Name>\<Company>\1.0 folder

What am I missing? And if App.config is not allowed within VSIX extension development, how would you go about wiring up log4net?


回答1:


What am I missing? And if App.config is not allowed within VSIX extension development, how would you go about wiring up log4net?

An App.Config file is copied and renamed during build to .exe.config, so only executables can have and use "App.Config" files using ConfigurationManager directly. Usually,a VSIX project generates a DLL that is loaded in the Visual Studio executable (devenv.exe), so your project, using ConfigurationManager directly, can only read settings from the devenv.exe.config (folder C:\Program Files (x86)\Microsoft Visual Studio 16.0\Common7\IDE).

And when l test it with my only app.config file in a vsix project,the project seems to be unable to get the value of my custom files only from the default devenv.exe.config which contains only two values TestProjectRetargetTo35Allowed and EnableWindowsFormsHighDpiAutoResizing.This means that in any case, it gets values from devenv.exe.config.

Solution

1#. you can just define the new key in the devenv.exe.config file and you can get it directly from the file.

 <appSettings>
    <add key ="TestProjectRetargetTo35Allowed" value ="true"/>
    <add key ="EnableWindowsFormsHighDpiAutoResizing" value ="true"/>
      insert new key here
  </appSettings>

2#. You can get this app.config by code and get the values of the keys directly from it.

ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = @"xxxxxxxx"; // the path of the custom app.config
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
var test = config.AppSettings.Settings["test"].Value;

In addition, I recommend solution2 is better and easier to solve your issue. Hope it could help you.



来源:https://stackoverflow.com/questions/59252387/app-config-inside-visual-studio-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!