Search code examples
c#windowsbackupwindows-forms-designerbackup-strategies

Creating a backup-maker program in C# windows form


I'd like to do a program which can make copies of a folder/subfolders to a specific destination in every hour. I've made a simple program which can copy files if I press a button, but its not scheduled and it forgets the source and the destination every time I open it. Could you please help me out? Thanks in advance!


Solution

  • Console case

    static public void Main(string[] args)
    {
      Console.WriteLine("Starting background process, press ESCAPE to stop.");
      var timer = new System.Threading.Timer(ProcessFiles, null, 1000, 2000);
      while ( Console.ReadKey().Key != ConsoleKey.Escape ) ;
    }
    
    static private void ProcessFiles(object state)
    {
      Console.WriteLine("Copy files... " + DateTime.Now.ToLongTimeString());
    }
    

    You can also create a service:

    https://learn.microsoft.com/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer

    https://www.c-sharpcorner.com/article/create-windows-services-in-c-sharp/

    Output

    enter image description here

    WinForms case

    Add a Timer from the Component tab of the Visual Studio Toolbox and set the desired Interval property in milliseconds hence 3600000 for an hour.

    Double click on it to create associated the event method:

    private void TimerCopyFile_Tick(object sender, EventArgs e)
    {
      LabelInfo.Text("Copy files... " + DateTime.Now.ToLongTimeString());
    }
    

    You need to start it somewhere or set the enabled property.

    timer.Start();
    

    Thus you can design your app as you want to offer for example settings and you can use a TrayIcon.

    WPF case

    In addition to the System.Threading.Timer there is:

    https://learn.microsoft.com/dotnet/api/system.windows.threading.dispatchertimer

    ASP.NET case

    In addition to the System.Threading.Timer there is:

    https://learn.microsoft.com/dotnet/api/system.web.ui.timer