Jump to content

Help Troubleshooting program -- FSwatch triggered Process Termination

- - - - -

  • Please log in to reply
2 replies to this topic

#1
DoktorD1313

DoktorD1313

    Newbie

  • Members
  • PipPip
  • 24 posts
Hey guys,

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?

#2
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Assuming you are using the correct file name extensions etc. in code, how about permissions of the file you want to monitor as well as the process to be killed?

Does your program have sufficient privileges to do so?
Today is the first day of the rest of my life

#3
DoktorD1313

DoktorD1313

    Newbie

  • Members
  • PipPip
  • 24 posts
Thanks for the response, but I got it.

Turns out, the file wasn't changed, but rather deleted and another created in it's place. All I had to do was modify the code to trigger the event on that file creation.

Thanks for all the help, guys!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users