vb.net 如何从运行时为 Visual Studio 加载项保存对 app.config 文件的更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25040243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:55:45  来源:igfitidea点击:

How to save changes to app.config file from runtime for a Visual Studio Add-In

vb.netvisual-studio-addins

提问by user3712663

Thanks to Is there a config type file for Visual Studio Add-In?I was able to create an app.config file for the Visual Studio add-in I am developing with the ability to read/write to it.

感谢Visual Studio 加载项是否有配置类型文件?我能够为我正在开发的 Visual Studio 加载项创建一个 app.config 文件,并具有读/写的能力。

Now I am having trouble saving the changes made to it at runtime. I have been running the following test code, but when I look the app.config file after running in debug, nothing has changed.

现在我无法保存在运行时对其所做的更改。我一直在运行以下测试代码,但是当我在调试中运行后查看 app.config 文件时,没有任何变化。

Dim pluginAssemblyPath As String = Assembly.GetExecutingAssembly().Location
Dim configuration As Configuration = ConfigurationManager.OpenExeConfiguration(pluginAssemblyPath)
Dim test1 As String = configuration.AppSettings.Settings.Item("Key1").Value

configuration.AppSettings.Settings.Item("Key1").Value = "Is this thing on?"

Dim test3 As String = configuration.AppSettings.Settings.Item("Key1").Value
configuration.AppSettings.SectionInformation.ForceSave = True
configuration.Save(ConfigurationSaveMode.Modified)

回答by user3712663

Dim pluginAssemblyPath As String = Assembly.GetExecutingAssembly().Location
   'Dim configuration As Configuration = ConfigurationManager.OpenExeConfiguration(pluginAssemblyPath)
    Dim test1 As String = configuration.AppSettings.Settings.Item("Key1").Value

    configuration.AppSettings.Settings.Item("Key1").Value = "Is this thing on?"

    Dim test3 As String = configuration.AppSettings.Settings.Item("Key1").Value
    configuration.AppSettings.SectionInformation.ForceSave = True
    configuration.Save(ConfigurationSaveMode.Modified)
    ConfigurationManager.RefreshSection("appSettings")

The actual app.config will not change, but the config file inside of the bin folder does.

实际的 app.config 不会改变,但 bin 文件夹内的配置文件会改变。