locked
How to detect changes in System.Configuration.Configuration RRS feed

  • Question

  • I can easily save any configuration changes in my app by running the instance method Save(ConfigurationSaveMode.Modified) of the System.Configuration.Configuration class.

    However, at app exit I want to ask the user if she want to save the config, but only if config has cahnged since app start.

    I am looking for a method or property something like HasChanged or similar, but I am not able to find any such method.

    Best approach ?

    Wednesday, August 30, 2017 9:27 AM

Answers

  • Hi EuroEager,

    For your question, all the settings will be saved with the following code.

    config.Save(ConfigurationSaveMode.Modified);

    Based on my search, I do not find the directly way to tell us if changes has been done. You could do something like set a flag is a good choose.

    Thanks for your sharing. Please mark the reply as answer.

    This will make answer searching easier in the forum and be beneficial to community members.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by EuroEager Tuesday, September 5, 2017 10:42 AM
    Tuesday, September 5, 2017 7:49 AM

All replies

  • Hi EuroEager,

    Thank you for posting here.

    What is the at app exit? For Winform, you could put the option of messageBox in Form1_FormClosing event. For console application, you could use Application.Exit() to quit the application.

    Here is a simple example in console application. You could make a messageBox before saving the configuration. When you click Yes, it will save the configuration. When you click No, it will exit the application.

    string message = "Do you want to save the configuration?";
                string caption = "Save option";
                MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                DialogResult result;
    
                // Displays the MessageBox.
    
                result = MessageBox.Show(message, caption, buttons);
    
                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    // config.Save(ConfigurationSaveMode.Modified);
                }
                else
                {
                    Application.Exit();
                }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Thursday, August 31, 2017 10:04 AM
  • Thanks, but I guess I was not specific enough in my question.

    I know how to shutdown (WPF b.t.w.) and ask if user want to save config.
    Please note that the question included "ConfigurationSaveMode.Modified" and my question was actually if the Configuration class has a method or property which tells me if changes has been done to this object, thus avoid annoying the user at exit if no changes is done.

    The Configuration class knows about changes somehow, that's why the mentioned argument exists and it would be nice if the application could get the same info, anyway, I have now solved in code (simply setting a "dirty" flag whenever a setter is executed, disregarding the possible case of changing back and thus not actually done any change :)


    Thursday, August 31, 2017 1:52 PM
  • Hi EuroEager,

    For your question, all the settings will be saved with the following code.

    config.Save(ConfigurationSaveMode.Modified);

    Based on my search, I do not find the directly way to tell us if changes has been done. You could do something like set a flag is a good choose.

    Thanks for your sharing. Please mark the reply as answer.

    This will make answer searching easier in the forum and be beneficial to community members.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by EuroEager Tuesday, September 5, 2017 10:42 AM
    Tuesday, September 5, 2017 7:49 AM