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