Home > OS >  How to detect a property value of the setting file is changed?
How to detect a property value of the setting file is changed?

Time:04-06

I have tried to make a simple user Authorization. After installation, the property called "UserHasBeenVerified" in the Settings1 file, is changed. I have added Settings Changed event handler to App.cs file but still I don't know the reason. I want the property to be FALSE after the installation. But somehow it does be TRUE.

Do you think how I can solve this problem? Thank you.


I have created an Installer Class to reset the Settings1 after the installation:

 [RunInstaller(true)]
public partial class InstallerClass : System.Configuration.Install.Installer
{
    public InstallerClass()
    {
        InitializeComponent();
    }

    protected override void OnAfterInstall(IDictionary savedState)
    {
        // This function runs when the installation is finished.

        base.OnAfterInstall(savedState);

        try
        {
            System.Windows.MessageBox.Show($"01\nVERIFIED STATE: {BackendLayer.Settings1.Default.UserHasBeenVerified}"); // Shows: VERIFIED STATE: False
            BackendLayer.Settings1.Default.Reset();
            System.Windows.MessageBox.Show($"02\nVERIFIED STATE: {BackendLayer.Settings1.Default.UserHasBeenVerified}"); // Shows: VERIFIED STATE: False
            BackendLayer.Settings1.Default.Save();
            System.Windows.MessageBox.Show($"03\nVERIFIED STATE: {BackendLayer.Settings1.Default.UserHasBeenVerified}"); // Shows: VERIFIED STATE: False
            System.Windows.MessageBox.Show("Installed");
        }
        catch 
        {
            System.Windows.MessageBox.Show("NOT Installed");
        }
    }


}

And the App.cs file is:

App() 
{
// Runs first whenever the program is opened
    BackendLayer.Settings1.Default.SettingChanging  = Default_SettingChanging;
    MessageBox.Show($"VERIFIED STATE: {BackendLayer.Settings1.Default.UserHasBeenVerified}"); // Shows: VERIFIED STATE: TRUE

}
private void Default_SettingChanging(object sender, System.Configuration.SettingChangingEventArgs e)
{
// Fires when any property in setting1 is changed.
   MessageBox.Show($"Setting Name: {e.SettingName}\nSetting Key: {e.SettingKey}\nSetting Class: {e.SettingClass}\nNew value: {e.NewValue}");
}

The function that calls App() Constructor:

/// <summary>
    /// Application Entry Point.
    /// </summary>
    [System.STAThreadAttribute()]
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public static void Main() {
        ScheduleProgram.App app = new ScheduleProgram.App();
        app.InitializeComponent();
        app.Run();
    }

UserHasBeenVerified Code in Settings1.Designer.cs:

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

The program's output after the installation by order:

  1. 01\nVERIFIED STATE: False
  2. 02\nVERIFIED STATE: False
  3. 03\nVERIFIED STATE: False
  4. Installed

The program's output after it is opened:

  1. VERIFIED STATE: True

The things that I have tried:

I have changed these properties of the Settings1 file:

*Build Action: From "None" to "Content"* ,

*Copy to Output Directory: From "Do not copy" to "Copy always"*

CodePudding user response:

I don't think BackendLayer.Settings1.Default is getting you the value from the file when you call it in the installer, you're getting the value from an instance of the Settings1 class instantiated with the default constructor.

Confirm that the value of UserHasBeenVerified is correct in your Settings1.settings file and that it's being copied to your app.config when you build your application. Since you just need default values, your installer just needs to package the output from building your app

  • Related