0

In C#, I need to look for the trigger of a process say "abc.exe". Whenever such a process starts, i need to fetch the first argument (always equal to the caption name,say caption_name) . Then i need to trigger another process say "xyz.exe" with caption_name as argument to it.

I want this program to run as a background exe which runs in the system.Hence i need it to occupy less memory space. How can I do this?

This is how I started it:

        ManagementEventWatcher watcher = new ManagementEventWatcher("Select * From __InstanceCreationEvent Within 5 Where TargetInstance Isa \"Win32_Process\" And TargetInstance.Name = \"hilite.exe\"");
        watcher.EventArrived += watcher_EventArrived;
tshepang
  • 12,111
  • 21
  • 91
  • 136
user2799127
  • 113
  • 2
  • 9

1 Answers1

0

Since you need to launch cmd processes from the application, I'd recommend making it an application, rather than a service. Launching applications from services lies somewhere in the "impossible to damned hard" space.

To enumerate current windows, get the process ID (to enable you to find out the exe name) and the title), you'll have to use pInvoke and call some windows API functions. Take a look at How to enumerate all windows belonging to a particular process using .NET? for details. Check out in particular the link in the accepted answer for some code examples on using these calls.

Finally, to launch the new process, you'll need to set up a ProcessStartInfo instance, with the path to xyz.exe as the FileName value and the title as the Arguments value. Pass this to an instance of Process and start the latter. You'll want to check out other properties of ProcessStartInfo and Process if you want to control whether the cmd winodw is visible, access the output etc.

Hope this helps.

Community
  • 1
  • 1
David Arno
  • 42,717
  • 16
  • 86
  • 131