Share via

Automatic Backup for Notepad C#

Hemanth B 886 Reputation points
2021-06-04T16:18:01.483+00:00

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.

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2021-06-05T03:07:36.133+00:00

    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.

    Was this answer helpful?

    0 comments No comments

  2. Anonymous
    2021-06-05T00:00:08.16+00:00

    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.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.