class FileMonitor { //The watcher class FileSystemWatcher sysWatcher; //directory to monitor string directoryPath = "C:\\AAAA"; //An output label and listBox Label outputLabel; ListBox operationView; //process id int processId; string processNm; //Threat details List<object> threatDetails; //constructor function public FileMonitor() { sysWatcher = new FileSystemWatcher(this.directoryPath); sysWatcher.IncludeSubdirectories = true; sysWatcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.FileName | NotifyFilters.LastWrite; //register event handlers sysWatcher.Changed += new FileSystemEventHandler(onChanged); sysWatcher.Deleted += new FileSystemEventHandler(onChanged); sysWatcher.Created += new FileSystemEventHandler(onChanged); sysWatcher.Renamed += new RenamedEventHandler(onRenamed); sysWatcher.EnableRaisingEvents = true; } public void setOutputComponents(Label myLable, ListBox myListBox) { this.outputLabel = myLable; this.operationView = myListBox; } private void onRenamed(object sender, RenamedEventArgs e) { this.doWork(); } private void onChanged(object sender, FileSystemEventArgs e) { this.doWork(); } // i believe this has some sort of bug. Need help here private void callProcessChecker() { ProcessStartInfo sInfo = new ProcessStartInfo(@"C:\handle64.exe"); sInfo.Arguments = this.directoryPath + " /accepteula"; sInfo.UseShellExecute = false; sInfo.RedirectStandardOutput = true; sInfo.CreateNoWindow = true; Process pingHandle = Process.Start(sInfo); pingHandle.WaitForExit(); string myOutput = pingHandle.StandardOutput.ReadToEnd(); string matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)"; foreach (Match match in Regex.Matches(myOutput, matchPattern)) { this.processId = int.Parse(match.Value); } } //a list to contain information about the process private void processDetails() { Process myprocess = Process.GetProcessById(this.processId); this.processNm = myprocess.ProcessName; this.threatDetails = new List<object>(); this.threatDetails.Add(this.processNm); this.threatDetails.Add(this.processId); this.threatDetails.Add(1); } //display items in a listBox private void displayInfoListBox() { if (this.operationView.InvokeRequired) { this.operationView.Invoke(new MethodInvoker( () => { this.operationView.Items.Add("Threat:" + this.processNm + "| Pid:" + this.processId + "| Status: "); } )); } else { this.operationView.Items.Add("Threat:" + this.processNm + "| Pid:" + this.processId + "| Status: "); } } private void doWork() { this.callProcessChecker(); this.processDetails(); this.displayInfoListBox(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)