1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188:
| using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Text; using System.IO; using System.Threading; using System.Collections.Specialized; using System.Reflection;
namespace FileWatchService { public partial class Service1 : ServiceBase { private Thread thread; private string path = "";
public Service1() { InitializeComponent(); }
protected override void OnStart(string[] args) { if (args.Length != 0) this.path = args[0]; Worker worker = new Worker(this.path); thread = new Thread(new ThreadStart(worker.StartWorker)); thread.Start(); this.EventLog.WriteEntry("FileWatchService wurde erfolgreich gestartet"); }
protected override void OnStop() { thread.Abort(); thread = null; this.EventLog.WriteEntry("FileWatchService wurde beendet"); }
protected override void OnPause() { thread.Suspend(); this.EventLog.WriteEntry("FileWatchService wurde angehalten"); }
protected override void OnContinue() { thread.Resume(); this.EventLog.WriteEntry("FileWatchService wurde fortgesetzt"); } }
public class Worker { private string protocolFile = "C:\\Projekte\\FileWatchSystem\\FileWatchLog.log"; private string path = "C:\\";
public Worker(string path) { if (path != "") this.path = path; }
public void StartWorker() { FileSystemWatcher fsw = new FileSystemWatcher(this.path);
fsw.IncludeSubdirectories = true;
fsw.EnableRaisingEvents = true;
fsw.Changed += new FileSystemEventHandler(ChangeFile); fsw.Deleted += new FileSystemEventHandler(DeleteFile); fsw.Created += new FileSystemEventHandler(CreateFile); fsw.Renamed += new RenamedEventHandler(RenameFile);
try { while (true) { fsw.WaitForChanged(WatcherChangeTypes.All); } } catch { }
fsw.EnableRaisingEvents = false; }
private void RenameFile(object sender, RenamedEventArgs e) { string entry = "[" + DateTime.Now.ToShortTimeString() + "] " + String.Format("Pfad: {0}; Datei umbenannt: von {1} nach {2}", Path.GetDirectoryName(e.FullPath), e.OldName, e.Name); WriteToLogFile(entry); }
private void CreateFile(object sender, FileSystemEventArgs e) { string entry = "[" + DateTime.Now.ToShortTimeString() + "] " + String.Format("Pfad: {0}; Datei erstellt{1}", Path.GetDirectoryName(e.FullPath), e.Name); WriteToLogFile(entry); }
private void DeleteFile(object sender, FileSystemEventArgs e) { string entry = "[" + DateTime.Now.ToShortTimeString() + "]" + String.Format("Pfad: {0}; Datei gelöscht: {1}", Path.GetDirectoryName(e.FullPath), e.Name); WriteToLogFile(entry); }
private void ChangeFile(object sender, FileSystemEventArgs e) { string pathChanged = e.FullPath.ToLower(); string pathProtocol = protocolFile.ToLower();
if (pathChanged == pathProtocol) return; string entry = "[" + DateTime.Now.ToShortTimeString() + "] " + String.Format("Pfad: {0}; Datei geändert:{1}", Path.GetDirectoryName(e.FullPath), e.Name); WriteToLogFile(entry); }
private StringCollection ReadFromLogFile() { StringCollection strCol = new StringCollection(); StreamReader sr = new StreamReader(this.protocolFile); string line = ""; int count = 1;
while ((line = sr.ReadLine()) != null) { if (count == 999) break; strCol.Add(line); count++; }
sr.Close(); return strCol; }
private void WriteToLogFile(string entry) { StringCollection changedEntriesCol; if (File.Exists(this.protocolFile)) changedEntriesCol = ReadFromLogFile(); else changedEntriesCol = new StringCollection();
changedEntriesCol.Insert(0, entry);
FileStream fs = new FileStream(this.protocolFile, FileMode.Create); StreamWriter sw = new StreamWriter(fs);
foreach (string str in changedEntriesCol) sw.WriteLine(str); sw.Close(); } } } |