简体   繁体   中英

How does wpf application settings actually work?

in my application I have easy login page with IP address / login / password / remember me checker.

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

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.

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.

Is there any way how can I get rid of user.config in application folder and have it stored only in AppData? Also main reason for that is that this (created by instalation) config file have two extension: 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:

   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 . 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. 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.

The project system stores application settings in two XML files:

  • an app.config file, which is created at design time when you create the first application setting
  • a user.config file, which is created at run time when the user who runs the application changes the value of any user setting.

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? Also main reason for that is that this (created by instalation) config file have two extension: App.exe.config.

This is how .NET settings work. 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. 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. Similarly, when it creates the {APPNAME}.exe.config file, it copies the one in the installation folder.

In short, there is no reason to delete (and plenty of reasons to keep) the user.config file in the application's installation folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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