首页 > 解决方案 > 以 C# windows 形式创建备份制作程序

问题描述

我想做一个程序,它可以每小时将文件夹/子文件夹复制到特定目的地。我制作了一个简单的程序,如果我按下一个按钮,它可以复制文件,但它没有安排好,每次打开它都会忘记源和目标。你能帮帮我吗?提前致谢!

标签: c#windowsbackupwindows-forms-designerbackup-strategies

解决方案


控制台案例

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/

输出

在此处输入图像描述

WinForms 案例

Timer从 Visual Studio 工具箱的“组件”选项卡中添加一个并以毫秒为单位设置所需的Interval属性,因此 3600000 为一小时。

双击它以创建关联的事件方法:

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

您需要在某处启动它或设置启用的属性。

timer.Start();

因此,您可以根据需要设计您的应用程序,例如设置,您可以使用 TrayIcon。

WPF案例

除此之外System.Threading.Timer还有:

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

ASP.NET 案例

除此之外System.Threading.Timer还有:

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


推荐阅读