question

HemanthB-9452 avatar image
0 Votes"
HemanthB-9452 asked karenpayneoregon answered

Automatic Backup for Notepad C#

Hi, I created a notepad using C# and I want the application to save (Automatic backup/auto save) the user's data every 30 seconds in a particular folder incase any unexpected circumstances. For example we are doing some work in Word and suddenly the power went off and once you open Word again, it saves an automatic backup so that you can recover. Please give the code for it if possible.

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

$$ANON_USER$$ avatar image
0 Votes"
$$ANON_USER$$ answered

Hello,

First inside of the form's load event add this code:

 if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "ApplicationState") == false)
             {
                 System.IO.StreamWriter objFile = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "ApplicationState");
    
                 objFile.Write("ClosedSucessfully");
                 objFile.Close();
    
                 LoadFile();
             }
             else
             {
                 System.IO.StreamReader objFile = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "ApplicationState");
                 if (objFile.ReadToEnd() == "")
                 {
                     LoadBackup();
                 }
                 else
                 {
                     LoadFile();
                 }
                 objFile.Close();
    
                 System.IO.StreamWriter WriteToFile = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "ApplicationState");
                 WriteToFile.Close();
             }
             Timer Timer = new Timer { Interval = 30000, Enabled = true };
             Timer.Tick += (s, eventArfs) =>
             {
                 SaveBackup();
             };

Now inside of the formclosing event add these statments:

 System.IO.StreamWriter objFile = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "ApplicationState");
             objFile.Write("ClosedSuccessfully");
             objFile.Close();

Finally add these procedures to your code:

 private void SaveBackup()
         {
             System.IO.StreamWriter objFile = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "Backup.txt");
    
             objFile.Write(txtEditor.Text);
             objFile.Close();
         }
    
         private void LoadBackup()
         {
             if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Backup.txt"))
             {
                 System.IO.StreamReader objFile = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "Backup.txt");
    
                 txtEditor.Text = objFile.ReadToEnd();
                 objFile.Close();
             }
         }
    
         private void LoadFile()
         {
             //Add your code here to load the normal file...
         }

This code assumes that the textbox is named: txtEditor. I hope it helps.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

Although the following takes time to setup consider reading the documentation and look at the sample. This is only for .NET Framework 4.8 or lower, does not work for .NET Core Framework.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.