• C# FileSystemWatcher监控程序发布文件夹自动推送nuget到指定库


    namespace FileSystemWatcher
    {
        class Program
        {
            static void Main(string[] args)
            {
                FileSystemWatchDemo(filePath, "*.nupkg", false);
                Console.ReadLine();
            }
            static void FileSystemWatchDemo(string path, string filter, bool includeSubDirs)
            {
                using (System.IO.FileSystemWatcher fsw = new System.IO.FileSystemWatcher(path))
                {
                    fsw.Created += FileCreatedChangedDeteled;
                    //fsw.Changed += FileCreatedChangedDeteled;
                    //fsw.Deleted += FileCreatedChangedDeteled;
                    fsw.Renamed += FswRenamed;
                    fsw.Error += FswError;
                    fsw.IncludeSubdirectories = includeSubDirs;
                    fsw.EnableRaisingEvents = true;
                    fsw.Filter = filter;
                    fsw.NotifyFilter = NotifyFilters.LastAccess
                                           | NotifyFilters.LastWrite
                                           | NotifyFilters.FileName
                                           | NotifyFilters.DirectoryName;
                    Console.WriteLine("Press 'q' to quit the sample.");
                    while (Console.Read() != 'q')
                    {
                    }
                }
            }
    
            private static void FswError(object sender, ErrorEventArgs e)
            {
                Console.WriteLine($"Error:{e.GetException().Message}");
            }
    
            private static void FswRenamed(object sender, RenamedEventArgs e)
            {
                Console.WriteLine($"Renamed:{e.OldFullPath}->{e.FullPath}");
            }
    
            private static void FileCreatedChangedDeteled(object sender, FileSystemEventArgs e)
            {
                Console.WriteLine($"File {e.FullPath} has been {e.ChangeType}");
                DirectoryInfo theFolder = new DirectoryInfo(filePath);
                string fileName = theFolder.GetFiles().Last(x=>x.Extension == ".nupkg")?.FullName;
                if (!string.IsNullOrWhiteSpace(fileName))
                {
                    string cmd = $@"dotnet nuget push -s http://localhost:8008/v3/index.json {fileName}";
                    RunCMDCommand(out string aa, cmd);
                    Console.WriteLine(aa);
                }
            }
            public static void RunCMDCommand(out string outPut, params string[] command)
            {
                using (Process pc = new Process())
                {
                    pc.StartInfo.FileName = "cmd.exe";
                    pc.StartInfo.CreateNoWindow = true;
                    pc.StartInfo.RedirectStandardError = true;
                    pc.StartInfo.RedirectStandardInput = true;
                    pc.StartInfo.RedirectStandardOutput = true;
                    pc.StartInfo.UseShellExecute = false;
                    pc.Start();
                    foreach (string com in command)
                    {
                        pc.StandardInput.WriteLine(com);
                    }
                    pc.StandardInput.WriteLine("exit");
                    pc.StandardInput.AutoFlush = true;
                    outPut = pc.StandardOutput.ReadToEnd();
                    pc.WaitForExit();
                    pc.Close();
                }
            }
        }
    }
    

      

  • 相关阅读:
    swift学习笔记之-析构过程
    swift学习笔记之-错误处理
    swift学习笔记之-类型转换
    swift学习笔记之-构造过程
    day43 数据库学习egon的博客 约束
    day43 数据库知识欠缺的
    day41 python【事物 】【数据库锁】
    day40 python MySQL【四】 之 【索引】【视图】【触发器】【存储过程】【函数】
    day41 mysql 学习 练习题 重要*****
    day39 python 学习 数据库学习 五个约束,数据库设计(一对一,一对多等等)
  • 原文地址:https://www.cnblogs.com/destinyyuan/p/14336394.html
Copyright © 2020-2023  润新知