Visual Studio 扩展中的 app.config? [英] app.config inside Visual Studio Extension?

查看:58
本文介绍了Visual Studio 扩展中的 app.config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个代表 Visual Studio 项目向导(vsix 包)的 Visual Studio 扩展.我正在尝试连接 log4net,但没有成功.我已将问题归结为我的 app.config 未正确加载.

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.

我已将此添加到我的 Visual Studio 扩展中的 app.config 中:

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>

在我的 IWizard 实现中,我添加了这行代码:

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

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

然而,上面的test变量在调试时总是为空.

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

我已经验证了这些事情:

I have verified these things:

  • App.config 设置为构建操作:内容
  • App.config 设置为复制到输出目录:始终复制
  • App.config 设置为包含在 VSIX 中:True
  • App.config 文件存在于我的 C:\Users\\AppData\Local\Microsoft\VisualStudio\16.0_...\Extensions\\\1.0文件夹

我错过了什么?如果 VSIX 扩展开发中不允许使用 App.config,您将如何连接 log4net?

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

推荐答案

我错过了什么?如果 VSIX 中不允许使用 App.config扩展开发,你将如何连接 log4net?

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

App.Config 文件在构建过程中被复制并重命名为 .exe.config,因此只有可执行文件才能直接使用 ConfigurationManager 拥有和使用App.Config"文件.通常,VSIX 项目会生成一个加载到 Visual Studio 可执行文件 (devenv.exe) 中的 DLL,因此您的项目直接使用 ConfigurationManager,只能从 devenv.exe.config(文件夹 C:\Program Files (x86)\Microsoft Visual Studio 16.0\Common7\IDE).

当我在 vsix 项目中使用我唯一的 app.config 文件对其进行测试时,该项目似乎无法仅从默认的 devenv 中获取我的自定义文件的值.exe.config 只包含两个值 TestProjectRetargetTo35AllowedEnableWindowsFormsHighDpiAutoResizing.这意味着在任何情况下,它都会从 devenv.exe.config 获取值.

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.

解决方案

1#.您可以在 devenv.exe.config 文件中定义新密钥,然后可以直接从文件中获取它.

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#.可以通过代码获取这个app.config,直接从中获取key的值.

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;

此外,我建议解决方案2更好、更容易解决您的问题.希望能帮到你.

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

这篇关于Visual Studio 扩展中的 app.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆