Создание программы резервного копирования в форме C# windows - PullRequest
0 голосов
/ 11 июля 2020

Я хотел бы создать программу, которая может делать копии папки / подпапок в указанное c место назначения каждый час. Я сделал простую программу, которая может копировать файлы, если я нажимаю кнопку, но это не запланировано, и она забывает источник и место назначения каждый раз, когда я ее открываю. Не могли бы вы мне помочь? Заранее спасибо!

1 Ответ

0 голосов
/ 11 июля 2020

Корпус консоли

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());
}

Также можно создать услугу:

https://docs.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/

Выход

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://docs.microsoft.com/dotnet/api/system.windows.threading.dispatchertimer

ASP . NET case

Помимо System.Threading.Timer есть:

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

...