C# Monitor and transfer or copy the changed or created file to a new location
2024-08-28 01:53:37
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics; namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
MonitorAndTransferFiles();
Console.ReadLine();
} static string destPath = @"D:\C\ConsoleApplication2\ConsoleApplication2"; static void MonitorAndTransferFiles(string sourcePath=null)
{
sourcePath = Directory.GetCurrentDirectory();
WatchFiles(sourcePath);
} static void WatchFiles(string path)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
watcher.NotifyFilter = NotifyFilters.LastWrite|NotifyFilters.CreationTime;
watcher.Filter = "*.*";
watcher.Changed += Watcher_Changed;
watcher.Created += Watcher_Created;
watcher.EnableRaisingEvents = true;
} private static void Watcher_Created(object sender, FileSystemEventArgs e)
{
try
{
Console.WriteLine($"Created:FullPath:{e.FullPath}, ChangeType: {e.ChangeType}");
File.Copy(e.FullPath, Path.Combine(destPath, Path.GetFileName(e.FullPath)), true);
}
catch
{
}
} private static void Watcher_Changed(object sender, FileSystemEventArgs e)
{
try
{
Console.WriteLine($"Changed:FullPath:{e.FullPath}, ChangeType: {e.ChangeType}");
File.Copy(e.FullPath, Path.Combine(destPath, Path.GetFileName(e.FullPath)), true);
}
catch
{
} }
}
}
最新文章
- Zen of Python
- python统计元素重复次数
- Thinkphp源码分析系列(四)–Dispatcher类
- android 布局优化常用技巧
- PHP中的 extends与implements 区别 [转]
- Ubuntu/CentOS使用BIND配置DNS服务器
- Elasticsearch简单介绍
- Memcached(七)Memcached的并发实例
- ArcServer,ArcSDE,ArcIMS,ArcEngine
- jquery动态插入行,不用拼写html,简洁版
- 关于slideup和slidedown 鼠标多次滑过累积的动画效果
- Python中文显示问题
- IOS 使用程序外地图(IOS Map and google Map)
- Flink 1.3.2 Standalone模式安装
- 【Kafka】操作命令
- 20160220.CCPP体系详解(0030天)
- python 给对象绑定属性和方法和__slots__的使用
- java单例模式实例
- JavaScript之深浅拷贝
- C++的virtual详解