简体   繁体   English

wpf 应用程序设置实际上是如何工作的?

[英]How does wpf application settings actually work?

in my application I have easy login page with IP address / login / password / remember me checker.在我的应用程序中,我有一个简单的登录页面,其中包含 IP 地址/登录名/密码/记住我检查器。

When I check remember me, application stores these data in file in: C:\\Users\\user\\AppData\\Local\\App\\App.exe_Url_5b4qolbj3ip4ltms1ohyh4cdivud5wfg\\1.0.0.0\\user.config当我检查记住我时,应用程序将这些数据存储在文件中:C:\\Users\\user\\AppData\\Local\\App\\App.exe_Url_5b4qolbj3ip4ltms1ohyh4cdivud5wfg\\1.0.0.0\\user.config

That is totally fine for me but the problem is (that I have no idea why is that happening) when I build my SetupWizard.msi this user.config is created by application in app folder.这对我来说完全没问题,但问题是(我不知道为什么会发生这种情况)当我构建我的 SetupWizard.msi 这个 user.config 是由应用程序文件夹中的应用程序创建的。

Then when I execute program, and check remember me box, it won't store values in this app created user.config but it is still stored in AppData file.然后当我执行程序并选中记住我框时,它不会在这个应用程序创建的 user.config 中存储值,但它仍然存储在 AppData 文件中。

Is there any way how can I get rid of user.config in application folder and have it stored only in AppData?有什么办法可以摆脱应用程序文件夹中的 user.config 并将其仅存储在 AppData 中? Also main reason for that is that this (created by instalation) config file have two extension: App.exe.config.另一个主要原因是这个(由安装创建)配置文件有两个扩展名:App.exe.config。 There is no way I leave it like that in release.我不可能在发布时就这样离开它。

Saving Code保存代码

        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 file: 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;
        }
    }
}

} }

Check your settings scope.检查您的设置范围。 You can do it by right clicking on you project name in Solution Explorer -> Properties -> Settings.您可以通过在解决方案资源管理器 -> 属性 -> 设置中右键单击您的项目名称来实现。 Your code suggests that it is User .您的代码表明它是User Here is why the settings may not be saved:以下是可能无法保存设置的原因:

There are two types of application settings, based on scope:根据范围,有两种类型的应用程序设置:

  • Application-scoped settings can be used for information such as a URL for a web service or a database connection string.应用程序范围的设置可用于信息,例如 Web 服务的 URL 或数据库连接字符串。 These values are associated with the application.这些值与应用程序相关联。 Therefore, users cannot change them at run time.因此,用户不能在运行时更改它们。
  • User-scoped settings can be used for information such as persisting the last position of a form or a font preference.用户范围的设置可用于诸如保留表单的最后一个位置或字体首选项之类的信息。 Users can change these values at run time.用户可以在运行时更改这些值。

You can change the type of a setting by using the Scope property.您可以使用 Scope 属性更改设置的类型。

The project system stores application settings in two XML files:项目系统将应用程序设置存储在两个 XML 文件中:

  • an app.config file, which is created at design time when you create the first application setting app.config 文件,在设计时创建第一个应用程序设置时创建
  • a user.config file, which is created at run time when the user who runs the application changes the value of any user setting. user.config 文件,当运行应用程序的用户更改任何用户设置的值时,会在运行时创建该文件。

Notice that changes in user settings are not written to disk unless the application specifically calls a method to do this.请注意,除非应用程序专门调用方法来执行此操作,否则不会将用户设置的更改写入磁盘。

More about Settings here .更多关于设置在这里

Is there any way how can I get rid of user.config in application folder and have it stored only in AppData?有什么办法可以摆脱应用程序文件夹中的 user.config 并将其仅存储在 AppData 中? Also main reason for that is that this (created by instalation) config file have two extension: App.exe.config.另一个主要原因是这个(由安装创建)配置文件有两个扩展名:App.exe.config。

This is how .NET settings work.这就是 .NET 设置的工作方式。 Your application needs both the user.config file if you have user-level settings, and it needs the app.config file if you have app-level settings.如果您有用户级设置,您的应用程序需要user.config文件,如果您有应用程序级设置,它需要app.config文件。 When the application creates a new file of user settings, it copies the user.config file from the installation folder to the AppData\\Local\\ folder.当应用程序创建用户设置的新文件时,它会将user.config文件从安装文件夹复制到AppData\\Local\\文件夹。 Similarly, when it creates the {APPNAME}.exe.config file, it copies the one in the installation folder.同样,当它创建{APPNAME}.exe.config文件时,它会复制安装文件夹中的文件。

In short, there is no reason to delete (and plenty of reasons to keep) the user.config file in the application's installation folder.简而言之,没有理由删除(并且有很多理由保留)应用程序安装文件夹中的user.config文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM