fileMonitor 检测文件变化 执行脚本

检测文件发生变化 启动脚本并传入 变化的文件名用于 修改同步 文件到ftp adb linux 等 代码同步修改
源码下载地址
FileMonitor2.0exe
使用时 把 需要执行的脚本拖动到 fileMonitor 即可
需要执行的代码 通过%1%获取 变动的文件路径 具体干什么看具体需求
mac端 稍后更新上来
代码贴出来

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;

using System.Diagnostics;
namespace FileMonitor
{
    class Program
    {
        static void Main(string[] args)
        {
            string File2;
            if (args.Length<1)
            {
                Console.WriteLine("请输入要执行的文件....");
                File2 = Console.ReadLine();
            }
            else File2 = args[0];
            Console.WriteLine("监控目录[" + AppDomain.CurrentDomain.BaseDirectory + "]" + "执行文件[" + File2 + "]");
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = AppDomain.CurrentDomain.BaseDirectory;
            watcher.NotifyFilter =NotifyFilters.LastWrite |NotifyFilters.FileName;
            watcher.Changed += (object source, FileSystemEventArgs e) =>
            {
                Console.WriteLine("文件{0}已经被修改,修改类型{1}", e.FullPath, e.ChangeType.ToString());
                //System.Diagnostics.Process.Start(File2);
                exec(File2, e.FullPath);
            };
            watcher.Created += (object source, FileSystemEventArgs e) =>
            {
                Console.WriteLine("文件{0}被建立", e.FullPath);
            };
            watcher.Deleted += (object source, FileSystemEventArgs e) =>
            {
                Console.WriteLine("文件{0}已经被删除", e.FullPath);
            };
            watcher.Renamed += (object source, RenamedEventArgs e) =>
            {
                Console.WriteLine("文件{0}的名称已经从{1}变成了{2}", e.OldFullPath, e.OldName, e.Name);
            };
            watcher.EnableRaisingEvents = true;
            while (true) Thread.Sleep(1000);
        }
        public static void exec(string exePath, string parameters)
        {
            Process process = new System.Diagnostics.Process();
            process.StartInfo.FileName = exePath;
            process.StartInfo.Arguments = parameters;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();
            process.BeginOutputReadLine();
            process.OutputDataReceived += new DataReceivedEventHandler(processOutputDataReceived);
            process.WaitForExit();
            process.OutputDataReceived -= new DataReceivedEventHandler(processOutputDataReceived);
            Console.WriteLine("执行完毕");
        }

        public static void processOutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine(e.Data);
        }
    }
}

微信截图_20200508105631.png

标签: none

添加新评论