I asked for advice before and I generated some code that monitors a file for modification and then terminates a specific process.
Here is my code:
using System;
using System.IO;
using System.Security.Permissions;
using System.Diagnostics;
public class Watcher
{
public static void Main()
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "C:\\XCELFILE";
/* Watch for changes in LastWrite times. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch RST files.
watcher.Filter = "*.rst";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Please Wait....");
while (Console.Read() != 'Q') ;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
Process[] procs = Process.GetProcessesByName("AfariaAutoConnect.exe");
foreach (Process proc in procs) proc.Kill();
Console.WriteLine("");
Console.WriteLine("Complete. You may now close the window.");
}
}
I tried it before using a trial directory with a notepad file inside. I asked the code to monitor the .txt file for changes, then terminate the "notepad.exe" process. When I ran the program and saved the text file, the notepad process was immediately terminated and shut down.
However, once I changed the code to monitor the file I needed it to monitor and to terminate the process I needed closed, it did not work.
It ran and displayed the "Please Wait....", but never closed the process even when I watched the modification date change on the file in question.
I'm no Ace when it comes to C#, and this code is mostly cut/pasted/modified from snippets I found online.
Any suggestions as to why the code is not functioning properly while being ran outside of my test parameters?


Sign In
Create Account


Back to top









