You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

307 lines
13 KiB

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Security.AccessControl;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using Database;
using Syncfusion.Windows.Forms.Tools;
using Syncfusion.WinForms.Input.Enums;
namespace OnDoc.Klassen
{
public static class clsProcessWatch
{
public static System.Timers.Timer watchtimer = new System.Timers.Timer(Convert.ToInt32(Properties.Settings.Default.OfficeWatchTimerIntervall));
static List<FileToCheck> FilestoCheck = new List<FileToCheck>();
public static void AddToList(string dokumentid, string filename, string application)
{
FilestoCheck.Add(new FileToCheck(dokumentid, filename, application));
watchtimer.Elapsed += WatchProcesses;
if (watchtimer.Enabled == false) { watchtimer.Enabled = true; }
}
public static void RemoveFromList(string dokumentid)
{
foreach (FileToCheck fc in FilestoCheck)
{
if (fc.dokumentid == dokumentid)
{
FilestoCheck.Remove(fc);
try
{
System.IO.File.Delete(fc.filename);
}
catch { }
break;
}
}
if (FilestoCheck.Count == 0)
{
watchtimer.Enabled = false;
}
}
private static string GetProcessOwner(int processId)
{
string query = "SELECT * FROM Win32_Process WHERE ProcessID = " + processId;
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection processList = searcher.Get();
foreach (ManagementObject obj in processList)
{
string[] argList = new string[] { string.Empty, string.Empty };
int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));
if (returnVal == 0)
{
// return DOMAIN\user
return argList[1] + "\\" + argList[0];
}
}
searcher = null;
return "NO OWNER";
}
private static void WatchProcesses(object source, ElapsedEventArgs e)
{
bool word = false;
bool excel = false;
bool pdf = false;
bool found = false;
watchtimer.Enabled = false;
found = false;
Logging.Logging.Debug("Start Watch_Process:"+ DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), "OnDoc.Processwatch","" );
try
{
foreach (FileToCheck fc in FilestoCheck)
{
if (fc.filedatetime < DateTime.Now.AddSeconds(-5))
{
found = false;
word = false;
excel = false;
pdf = false;
Logging.Logging.Debug(fc.application + " / FileChek " + fc.filename + " / " + fc.filedatetime.ToString("yyyy-MM-dd hh:mm:ss") + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), "OnDoc.Processwatch", fc.dokumentid);
if (fc.application == "Word") { word = true; }
if (fc.application == "Excel") { excel = true; }
if (fc.application == "PDF") { pdf = true; }
if (word)
{
Thread.Sleep(200);
Process[] localByName = Process.GetProcessesByName("WINWORD");
foreach (Process p in localByName)
{
string owner = GetProcessOwner(p.Id);
owner = owner.ToUpper();
Logging.Logging.Debug(AppParams.systemtgnummer + "/" + AppParams.currenttgnummer + "/" + owner + "/" + fc.dokumentid + "/" + p.MainWindowTitle, "", "");
if (p.MainWindowTitle.IndexOf(fc.dokumentid) > -1 ||
(p.MainWindowTitle.Trim() == "" && owner.Contains(AppParams.systemtgnummer.ToUpper())))
{
if (p.MainWindowTitle.Trim() != "") { fc.Lastfound = DateTime.Now; }
found = true;
save_to_db(fc);
}
}
//foreach (Process p in localByName)
//{
// Logging.Logging.Debug(fc.dokumentid + "/" + p.MainWindowTitle, "", "");
// if (p.MainWindowTitle.IndexOf(fc.dokumentid) > -1 || p.MainWindowTitle.Trim() == "")
// {
// if (p.MainWindowTitle.Trim() != "") { fc.Lastfound = DateTime.Now; }
// found = true;
// save_to_db(fc);
// break;
// }
//}
}
if (excel)
{
Process[] localByName = Process.GetProcessesByName("EXCEL");
foreach (Process p in localByName)
{
string owner = GetProcessOwner(p.Id);
owner = owner.ToUpper();
if (p.MainWindowTitle.IndexOf(fc.dokumentid) > -1 ||
(p.MainWindowTitle.Trim() == "" && owner.Contains(AppParams.systemtgnummer.ToUpper())))
{
if (p.MainWindowTitle.Trim() != "") { fc.Lastfound = DateTime.Now; }
found = true;
save_to_db(fc);
}
}
//Process[] localByName = Process.GetProcessesByName("EXCEL");
//foreach (Process p in localByName)
//{
// if (p.MainWindowTitle.IndexOf(fc.dokumentid) > -1 || p.MainWindowTitle.Trim() == "")
// {
// if (p.MainWindowTitle.Trim() != "") { fc.Lastfound = DateTime.Now; }
// found = true;
// save_to_db(fc);
// }
//}
}
if (!found)
{
Logging.Logging.Debug("Not Found " + fc.filename + " / " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), "OnDoc.Processwatch", fc.dokumentid);
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);
Remove_Dokumentbearbeitung_Zwingend(fc.dokumentid);
Remove_Approvals(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;
};
}
}
}
watchtimer.Enabled = true;
}
catch (Exception ex)
{
Logging.Logging.Debug("Error Processwatch", "OnDoc", ex.Message);
}
finally
{
watchtimer.Enabled = true ;
}
}
private static void Remove_Dok_in_Bearbeitung(string dokumentid)
{
DB db = new DB(AppParams.connectionstring);
db.Dok_in_Bearbeitung(2, dokumentid, AppParams.CurrentMitarbeiter);
db = null;
}
private static void Remove_Dokumentbearbeitung_Zwingend(string dokumentid)
{
DB db = new DB(AppParams.connectionstring);
db.Exec_SQL("Update dokument set bearbeitung_zwingend=0 where dokumentid='" + dokumentid + "'");
db = null;
}
private static void Remove_Approvals(string dokumentid)
{
DB db = new DB(AppParams.connectionstring);
db.Exec_SQL("Update dokument_bewilligung set aktiv=0, mutiert_am=getdate(), mutierer=" + AppParams.CurrentMitarbeiter.ToString() + " where dokumentid='" + dokumentid + "'");
db = null;
}
private static void save_to_db(FileToCheck fc)
{
if (Check_Modified(fc))
{
try
{
System.IO.File.Copy(fc.filename, fc.filename + ".tmp");
Save_File(fc.dokumentid, fc.filename + ".tmp");
fc.filedatetime = DateTime.Now;
System.IO.File.Delete(fc.filename + ".tmp");
Logging.Logging.Debug(fc.application + " / save_to_db " + fc.filename + " / " + fc.filedatetime.ToString("yyyy-MM-dd hh:mm:ss") + "/" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), "OnDoc.Processwatch", fc.dokumentid);
}
catch
{
Logging.Logging.Debug(fc.application + " / save_to_db faild " + fc.filename + " / " + fc.filedatetime.ToString("yyyy-MM-dd hh:mm:ss") + "/" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), "OnDoc.Processwatch", fc.dokumentid);
}
}
}
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);
db.Exec_SQL("Update dokument set mutiertam = getdate(), mutierer=" + AppParams.CurrentMitarbeiter + " where dokumentid='" + dokumentid + "'");
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.Year < DateTime.Now.Year)
{
lwt = fc.filedatetime;
lwt = fc.filedatetime.AddSeconds(+5);
}
int secdiff = (int)((lwt - fc.filedatetime).TotalSeconds);
Logging.Logging.Debug("Prozesswatch - Check Modified: " + lwt.ToString() + "," + fc.filedatetime.ToString(), "OnDoc", fc.dokumentid);
//Logging.DocLog.Debug("Prozesswatch - Check Modified: " + lwt.ToString() + "," + fc.filedatetime.ToString(), "Processwatch", fc.dokumentid, "", fc.filename);
//if ((lwt- fc.filedatetime).Seconds > 2)
if (secdiff > 2)
{
return true;
}
else
{
return false;
}
}
public static int check_open_files()
{
return FilestoCheck.Count;
}
}
public class FileToCheck
{
public string dokumentid { get; set; }
public string filename { get; set; }
public string application { get; set; }
public int counter { get; set; } = 0;
public DateTime Lastfound { get; set; } = DateTime.Now;
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;
//Logging.DocLog.Debug("Add Processwatch: " + DateTime.Now.ToString(), "New FileToCheck", dokumentid, "", "Add Processwatch");
Logging.Logging.Debug("Add Processwatch: " + DateTime.Now.ToString(), "OnDoc", this.dokumentid);
}
}
}