【问题标题】:appSettings value in App.Config differ in VS 2015 and VS 2017App.Config 中的 appSettings 值在 VS 2015 和 VS 2017 中有所不同
【发布时间】:2020-02-22 15:54:21
【问题描述】:

这是 VS 2015 的正常行为吗?

我正在尝试在 App.Config 的 appSettings 中存储一个值。在 VS 2017 中,它存储在 WindowsFormsApplication1.exe.config 中,但它没有将值存储在 VS 2015 中

我的代码和两个版本的VS都是一样的

        var conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        var settings = conf.AppSettings.Settings;
        settings.Add("password", textBox1.Text);
        conf.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection(conf.AppSettings.SectionInformation.Name);

我的 App.config 是这样的:

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
<appSettings>

</appSettings>
</configuration>

对此有何解释?

【问题讨论】:

    标签: c# asp.net app-config


    【解决方案1】:

    我相信构建程序时生成的 WindowsFormsApplication1.exe.config 文件是用于调试目的。

    试试这个,看看配置文件所在的路径。 (taken from this answer)

        using System.Configuration;  // Add a reference to System.Configuration.dll
        ...
        var path = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
    

    【讨论】:

    • 它使用 \user.config 进入 appdata 文件夹
    • @ron 那么它是否存储了值?
    • 它不存储在常规的 app.config 中,而是存储在 app name.svchost.exe.config 中
    猜你喜欢
    • 2018-10-19
    • 1970-01-01
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 2018-04-28
    • 2017-08-25
    • 1970-01-01
    相关资源
    最近更新 更多