我创建了一个代表 Visual Studio 项目向导(vsix 包)的 Visual Studio 扩展。我正在尝试连接 log4net,但没有成功。我已将问题归结为 app.config 未正确加载。
我已将其添加到我的 Visual Studio 扩展中的 app.config 中:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="test" value="hello world"/>
</appSettings>
</configuration>
在我的 IWizard 实现中,我添加了这行代码:
var test = System.Configuration.ConfigurationManager.AppSettings["test"];
但是,那test
调试时上面的变量始终为空。
我已经验证了这些事情:
- App.config 设置为“构建操作:内容”
- App.config 设置为复制到输出目录:始终复制
- App.config 设置为“包含在 VSIX 中:True”
- App.config 文件存在于我的
C:\Users\<user>\AppData\Local\Microsoft\VisualStudio\16.0_...\Extensions\<Name>\<Company>\1.0
folder
我缺少什么?如果 VSIX 扩展开发中不允许使用 App.config,那么您将如何连接 log4net?
我缺少什么?如果 VSIX 中不允许 App.config
扩展开发,您将如何连接 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)。
当我用我唯一的测试它时app.config
在 vsix 项目中的文件中,该项目似乎无法仅从默认值获取我的自定义文件的值devenv.exe.config
其中仅包含两个值TestProjectRetargetTo35Allowed
and EnableWindowsFormsHighDpiAutoResizing
这意味着在任何情况下,它都会从 devenv.exe.config 获取值。
Solution
1#.您可以在中定义新密钥devenv.exe.config
文件,您可以直接从文件中获取它。
<appSettings>
<add key ="TestProjectRetargetTo35Allowed" value ="true"/>
<add key ="EnableWindowsFormsHighDpiAutoResizing" value ="true"/>
insert new key here
</appSettings>
2#.您可以通过代码获取此app.config,并直接从中获取键的值。
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可以更好、更轻松地解决您的问题。
希望它可以帮助你。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)