JustMeGaaRa
JustMeGaaRa

Reputation: 520

Undo ConfigurationSection changes

I'm curious if it's possible to undo changes I've made to ConfigurationSection in run-time. I don't want to create lots of variables & set current values, so that when I want to undo them, I can just set them back. I need a simpler way, a method I suppose? Can anyone help me?

Upvotes: 1

Views: 170

Answers (3)

Charles Lambert
Charles Lambert

Reputation: 5132

You can implement the Command Pattern. It is ideal for your situation. You can use StreamReader and StreamWriter to read and write your backup files.

interface ICommandPattern {
    void Execute();
    void Undo();
}

class SaveConfigurationPattern : ICommandPattern {
    string _backupFileName;
    public void Execute()
    {
        // serialize your original and save to the backup file name
        // make changes and save to your config file
    }

    public void Undo()
    {
        // copy your backup file over the configuration file
    }
}

you can store an array of instances of the SaveConfigurationPattern and allow for multi-level undo and redo operations.

Upvotes: 1

m3kh
m3kh

Reputation: 7941

IMO, you can write it in several approaches. For an instance if you are after to change your application settings at run-time, you can store them in a better place like database in lieu of configuration file and a simple versioning system to log a history from user changes.

Upvotes: 1

zmilojko
zmilojko

Reputation: 2135

Have you considered using serialization? You could serialize ConfiguartionSection and then deserialize it to undo. Here are some tips.

Upvotes: 1

Related Questions