using ManagementEventWatcher

MiPakTeh 1,476 Reputation points
2021-07-17T11:16:42.353+00:00

Hi All,
I use the ManagementEventWatcher to detect program "Third" .If program "Third" begin running then the program "Second" automatically start to monitoring my computer.somebody can show the true coding.

Here is code ;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _Exe_5F
{
    public partial class Form1 : Form
    {

        string query = "SELECT * FROM __InstanceCreationEvent " + "WITHIN 1 WHERE " + "TargetInstance isa \"Win32_Process\"";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ManagementEventWatcher watcher = new ManagementEventWatcher(query);
            watcher.EventArrived += new EventArrivedEventHandler(Start_Bulk);
            watcher.Start();

        }
        private void Start_Bulk(object sender, EventArrivedEventArgs e)
        {
            ManagementBaseObject targetInstance = (ManagementBaseObject)e.NewEvent["TargetInstance"];
            Invoke(new Action(() => listBox1.Items.Add(targetInstance["Name"])));

            foreach (var property in targetInstance.Properties)
            {
                try
                {
                    string name = property.Value.ToString();
                    if (name == "Third")
                    {
                        try
                        {
                            using (Process myProcess = new Process())
                            {
                                myProcess.StartInfo.UseShellExecute = false;
                                myProcess.StartInfo.FileName = "C:\\Users\\family\\Desktop\\Second.exe.exe";
                                myProcess.StartInfo.CreateNoWindow = true;
                                myProcess.Start();
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
                catch
                {
                }
            }

        }
    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,015 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 77,506 Reputation points
    2021-07-18T11:42:53.41+00:00

    You can simplify by replacing the query by :

     string query = "SELECT * FROM __InstanceCreationEvent " + "WITHIN 1 WHERE " + "TargetInstance isa \"Win32_Process\" and TargetInstance.Name='Third.exe'";
    

    then remove all the code inside "foreach (var property in targetInstance.Properties)..."
    to just keep "using (Process myProcess = new Process())..." to launch your process when "Third.exe" is started

    0 comments No comments

0 additional answers

Sort by: Most helpful