using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Security.AccessControl; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; using Database; using Syncfusion.Windows.Forms.Tools; namespace OnDoc.Klassen { public static class clsProcessWatch { public static System.Timers.Timer watchtimer = new System.Timers.Timer(2000); static List FilestoCheck = new List(); public static void AddToList(string dokumentid, string filename, string applicatoin) { FilestoCheck.Add(new FileToCheck(dokumentid, filename, applicatoin)); if (watchtimer.Enabled == false) { watchtimer.Enabled = true; } watchtimer.Elapsed += WatchProcesses; } public static void RemoveFromList(string dokumentid) { foreach (FileToCheck fc in FilestoCheck) { if (fc.dokumentid == dokumentid) { FilestoCheck.Remove(fc); break; } } if (FilestoCheck.Count == 0) { watchtimer.Enabled = false; } } private static void WatchProcesses(object source, ElapsedEventArgs e) { bool word = false; bool excel = false; bool pdf = false; bool found = false; found= false; foreach (FileToCheck fc in FilestoCheck) { if (fc.application == "Word") { word = true; } if (fc.application == "Excel") { excel = true; } if (fc.application == "PDF") { pdf = true; } if (word) { Process[] localByName = Process.GetProcessesByName("WINWORD"); foreach (Process p in localByName) { if (p.MainWindowTitle.IndexOf(fc.dokumentid) > 0) { found = true; } } } if (excel) { Process[] localByName = Process.GetProcessesByName("EXCEL"); foreach (Process p in localByName) { if (p.MainWindowTitle.IndexOf(fc.dokumentid) > 0) { found = true; } } } if (!found) { if (Check_Modified(fc) == true) { Save_File(fc.dokumentid, fc.filename); Logging.DocLog.Info("Dokument gespeichert und geschlossen", "Processwatch", fc.dokumentid, "", fc.filename); RemoveFromList(fc.dokumentid); Remove_Dok_in_Bearbeitung(fc.dokumentid); return; } else { Logging.DocLog.Info("Dokument ohne speichern geschlossen", "Processwatch", fc.dokumentid, "", fc.filename); RemoveFromList(fc.dokumentid); Remove_Dok_in_Bearbeitung(fc.dokumentid); return; }; } } } private static void Remove_Dok_in_Bearbeitung(string dokumentid) { DB db = new DB(AppParams.connectionstring); db.Dok_in_Bearbeietung(2, dokumentid, AppParams.CurrentMitarbieter); db = null; } private static void Save_File(string dokumentid, string filename) { DB db = new DB(AppParams.connectionstring); db.Get_Tabledata("Select * from dokument where dokumentid='" + dokumentid + "'", false, true); db.Save_To_DB(dokumentid, filename); db.set_approvalstate(dokumentid, false); Logging.DocLog.Info("Dokument gespeichert", "Processwatch", dokumentid, "", filename); db = null; } private static bool Check_Modified(FileToCheck fc) { DateTime lwt = System.IO.File.GetLastWriteTime(fc.filename); if ((lwt- fc.filedatetime).Seconds > 2) { return true; } else { return false; } } } public class FileToCheck { public string dokumentid { get; set; } public string filename { get; set; } public string application { get; set; } public DateTime filedatetime { get; set; } public FileToCheck(string dokumentid, string filename, string application) { this.dokumentid = dokumentid; this.filename = filename; this.application = application; this.filedatetime = DateTime.Now; } } }