【问题标题】:How does wpf application settings actually work?wpf 应用程序设置实际上是如何工作的?
【发布时间】:2019-07-04 21:34:21
【问题描述】:

在我的应用程序中,我有简单的登录页面,带有 IP 地址/登录名/密码/记住我检查器。

当我检查记住我时,应用程序将这些数据存储在文件中:C:\Users\user\AppData\Local\App\App.exe_Url_5b4qolbj3ip4ltms1ohyh4cdivud5wfg\1.0.0.0\user.config

这对我来说完全没问题,但问题是(我不知道为什么会这样)当我构建我的 SetupWizard.msi 时,这个 user.config 是由 app 文件夹中的应用程序创建的。

然后当我执行程序并选中记住我框时,它不会将值存储在这个应用程序创建的 user.config 中,但它仍然存储在 AppData 文件中。

有什么办法可以摆脱应用程序文件夹中的 user.config 并将其仅存储在 AppData 中?另一个主要原因是这个(由安装创建的)配置文件有两个扩展名:App.exe.config。我不可能在发布时就这样离开它。

保存代码

        public void Save_data()
    {
        if (CheckBoxRemCredts.IsChecked == true)
        {
            Properties.Settings.Default.userName = txtBoxLogin.Text;
            Variable.passEncrypted = (Encrypt(pswBox.Password));
            //  MessageBox.Show(Variable.passEncrypted); Debugging
            Properties.Settings.Default.userPass = Variable.passEncrypted;
            Properties.Settings.Default.ipAddress = txtBoxIP.Text;
            Properties.Settings.Default.Remme = "yes";
            Properties.Settings.Default.Save();
        }
        else
        {
            Properties.Settings.Default.userName = txtBoxLogin.Text;
            Properties.Settings.Default.userPass = pswBox.Password;
            Properties.Settings.Default.ipAddress = txtBoxIP.Text;
            Properties.Settings.Default.Remme = "no";
            Properties.Settings.Default.Reset();
        }
    }

Settings.settings 文件:

   namespace SMS_Vrána.Properties {


[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.7.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

    private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

    public static Settings Default {
        get {
            return defaultInstance;
        }
    }

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Configuration.DefaultSettingValueAttribute("")]
    public string userPass {
        get {
            return ((string)(this["userPass"]));
        }
        set {
            this["userPass"] = value;
        }
    }

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Configuration.DefaultSettingValueAttribute("")]
    public string Remme {
        get {
            return ((string)(this["Remme"]));
        }
        set {
            this["Remme"] = value;
        }
    }

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Configuration.DefaultSettingValueAttribute("")]
    public string ipAddress {
        get {
            return ((string)(this["ipAddress"]));
        }
        set {
            this["ipAddress"] = value;
        }
    }

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Configuration.DefaultSettingValueAttribute("")]
    public string userName {
        get {
            return ((string)(this["userName"]));
        }
        set {
            this["userName"] = value;
        }
    }
}

}

【问题讨论】:

    标签: c# .net wpf


    【解决方案1】:

    有什么办法可以摆脱应用程序文件夹中的 user.config 并将其仅存储在 AppData 中?另一个主要原因是这个(由安装创建的)配置文件有两个扩展名:App.exe.config。

    这就是 .NET 设置的工作方式。如果您有用户级设置,您的应用程序需要user.config 文件,如果您有应用程序级设置,它需要app.config 文件。当应用程序创建一个新的用户设置文件时,它会将user.config 文件从安装文件夹复制到AppData\Local\ 文件夹。同样,当它创建{APPNAME}.exe.config 文件时,它会复制安装文件夹中的那个。

    简而言之,没有理由删除(并且有很多理由保留)应用程序安装文件夹中的user.config 文件。

    【讨论】:

      【解决方案2】:

      检查您的设置范围。您可以通过在解决方案资源管理器 -> 属性 -> 设置中右键单击您的项目名称来执行此操作。您的代码表明它是User。以下是设置可能无法保存的原因:

      根据范围,有两种类型的应用程序设置:

      • 应用程序范围的设置可用于 Web 服务的 URL 或数据库连接字符串等信息。这些值 与应用程序相关联。因此,用户无法更改 它们在运行时。
      • 用户范围的设置可用于保留表单的最后位置或字体首选项等信息。用户可以更改 这些值在运行时。

      您可以使用 Scope 属性更改设置的类型。

      项目系统将应用程序设置存储在两个 XML 文件中:

      • app.config 文件,在您创建第一个应用程序设置时在设计时创建
      • user.config 文件,在运行应用程序的用户更改任何用户设置的值时在运行时创建。

      请注意,用户设置的更改不会写入磁盘,除非 应用程序专门调用一个方法来执行此操作。

      更多关于设置here。

      【讨论】:

      • OP 代码中的 [global::System.Configuration.UserScopedSettingAttribute()] 暗示设置确实是用户范围的。
      • 很抱歉,我现在发现自己表达的很糟糕。我不希望它出现在我的安装路径中。我希望仅在 AppData 中或至少在 AppData 中拥有该 user.config,以便它可以隐藏在安装路径中。
      • @Pati K 你不知道在那种情况下该怎么办吗?
      • @Cabry 对不起,但不是真的。我想到的唯一解决方案是以编程方式在文件出现时删除它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 2011-09-27
      • 2021-12-16
      相关资源
      最近更新 更多