c# - 以 C# windows 形式创建备份制作程序
问题描述
我想做一个程序,它可以每小时将文件夹/子文件夹复制到特定目的地。我制作了一个简单的程序,如果我按下一个按钮,它可以复制文件,但它没有安排好,每次打开它都会忘记源和目标。你能帮帮我吗?提前致谢!
解决方案
控制台案例
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://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
还有:
推荐阅读
- mysql - 我无法更新我的实体表(Spring Boot 2 hibernate 5)
- firebase - 通过电子邮件从 Firebase 发送调查链接
- r - 线性回归:行(作为观察)与变量
- python - 用于请求调用 Google Maps API 的 python 字符串编码
- amazon-web-services - 唯一识别 Amazon EC2 机器
- regex - Google 表格中的正则表达式 AND 运算符
- php - 解析 json 并将其转换为 php 数组
- python-3.x - 2^n 问题(它不适用于大量数字)
- go - 如何从头订阅
- r - GLMMadaptive 用于半连续数据