zoukankan      html  css  js  c++  java
  • 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();
                }
            }
        }
    }
    

      

  • 相关阅读:
    暑假集训Day14 I (莫队)
    暑假集训Day14 G (gcd 数学)
    暑假集训Day13 H (进制转换 数学)
    暑假集训Day13 C (二分+贪心)
    暑假集训Day13 I (区间DP)
    暑假集训Day12 H (数论 STL)
    Java8中list转map方法总结
    Typora ---一款简洁的Markdown编辑器
    java8 map flatmap
    Elasticsearch基本查询总结
  • 原文地址:https://www.cnblogs.com/destinyyuan/p/14336394.html
Copyright © 2011-2022 走看看