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.

127 lines
3.7 KiB

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;
namespace OnDoc.Klassen
{
public static class clsProcessWatch
{
public static System.Timers.Timer watchtimer = new System.Timers.Timer(1500);
static List<FileToCheck> FilestoCheck = new List<FileToCheck>();
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);
RemoveFromList(fc.dokumentid);
return;
};
}
}
}
private static void Save_File(string dokumentid, string filename)
{
DB db = new DB(AppParams.connectionstring);
db.Save_To_DB(dokumentid, filename);
}
private static bool Check_Modified(FileToCheck fc)
{
DateTime lwt = System.IO.File.GetLastWriteTime(fc.filename);
if ((DateTime.Now - lwt).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;
}
}
}