[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Provide autosave in C#

[Provide autosave in C#]

This example extends the scribble example (most recently described in the post Provide undo and redo in C#) so it provides an auto-save feature.

The following code creates the name of the program's auto-save file.

// The auto-save file name. private string AutoSaveFile = Path.GetTempPath() + "scribble.tmp";

This code simply uses the System.IO.Path class's GetTempPath method to find the system's temporary directory and adds the file name scribble.tmp.

The previous version of the program saves a snapshot whenever you make changes to the drawing. It then uses the saved snapshots to provide undo and redo features. The following code shows the new program's version of the SaveSnapshot method. The new code is highlighted in blue.

// Save a snapshot in the undo list. private void SaveSnapshot(bool auto_save) { // Save the snapshot. UndoList.Push(GetSnapshot()); // Empty the redo list. if (RedoList.Count > 0) RedoList = new Stack(); // Enable or disable the Undo and Redo menu items. EnableUndo(); // Auto-save. if (auto_save) AutoSave(); } // Auto-save. private void AutoSave() { SaveIntoFile(AutoSaveFile); }

The SaveSnapshot method saves a snapshot as before and then, if the auto_save variable is true, it calls the AutoSave method. That method simply calls the SaveIntoFile method, asking it to save a snapshot into the autosave file. The SaveIntoFile method is the same method the program uses to create saved scribble files. (See the post Save and restore pictures drawn by the user in C# for information on how that works.)

When the program starts, the following Load event handler executes.

// Look for an auto-save file. private void Form1_Load(object sender, EventArgs e) { // See if the file exists. if (File.Exists(AutoSaveFile)) { // Ask the user if we should load this file. if (MessageBox.Show( "An auto-save file exists. Do you want to load it?", "Restore?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { // Load the file. LoadFromFile(AutoSaveFile); } } }

This code checks to see if an autosave file exists. If it does, the program asks if you want to reload it. If you click Yes, the code calls the LoadFromFileMethod (which is also used to load saved scribble files) to load it.

The last new piece of code is the following FormClosing event handler.

// Remove the auto-save file. private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (File.Exists(AutoSaveFile)) File.Delete(AutoSaveFile); }

If the autosave file exists, this code deletes it. That makes the program delete the file if it closes normally. To make the program crash, click the little bomb button on the right wide of the toolbar.

That's all you really need for a simple autosave. If the program lets you save and restore data, then it already has the methods you need to save and restore an autosave file.

The remaining issue is whether the program is fast enough to make an autosave file every time the user makes a change. If the data files are really big or the user can make changes very quickly, then it might take too long to save after every change. In that case you might want to save asynchronously or save periodically (perhaps every 5 or 10 minutes).

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.