zoukankan      html  css  js  c++  java
  • C# Monitor and transfer or copy the changed or created file to a new location

    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:CConsoleApplication2ConsoleApplication2";
    
            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
                {
                }
                
            } 
        }
    }
  • 相关阅读:
    macOS 终端可用的 Hex 查看与编辑器
    MAC brew install 跳过 update
    zstd
    JAVA中的时区设置
    conda虚拟环境中设置环境变量
    vertx 获取请求参数
    idea2020.3激活码最新破解教程(亲测有效)
    Camtasia recorder 的快捷键
    ARM STM32 各种缩写和全称
    如何解决keil mdk中文汉字乱码或设置编码问题
  • 原文地址:https://www.cnblogs.com/Fred1987/p/11971370.html
Copyright © 2011-2022 走看看