CKY CKY - 1 month ago 9
C# Question

What is the best way to handle Multi-instance Properties.Settings.Default.Save()

I have a C# Winforms application that uses app.config to save 3 simple settings.


  • Last selected path as
    string

  • Window state as
    bool

  • Updated as
    bool



This is a multi-instance application and when a user has more then one instance open and closes the application group, they are met with


System.Configuration.ConfigurationErrorsException: Failed to save settings: The configuration file has been changed by another program.


What is the best way to manage this?

I know I can catch the error and return out of application-exit method but this seems improper. It would seem to me that there should be a way to check if another instance is working in the file before calling
Properties.Settings.Default.Save();
and then return out of the method.

Answer

Catch the exception and call Properties.Settings.Default.Reload() then attempt to save again.