• 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();
                }
            }
        }
    }
    

      

  • 相关阅读:
    mybatis-Generator 代码自动生成报错 Result Maps collection already contains value for BaseResultMap
    Linux 常用的命令
    Linux 查找文件
    eclipse tasks
    Myeclipse 文件注释和解注释
    proxool连接池 异常
    EL 表达式 函数 操作 字符串
    Myeclipse 选中高亮
    Spring 定时作业
    linux配置java环境变量(详细)
  • 原文地址:https://www.cnblogs.com/destinyyuan/p/14336394.html
Copyright © 2020-2023  润新知