Support Forums

Full Version: C# Monitor new startup programs
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
An interesting function if you want to avoid new programs to be added to windows startup :

Code:
public void FillStartUpList()
        {
            var mangnmt = new ManagementClass("Win32_StartupCommand");
            ManagementObjectCollection mcol = mangnmt.GetInstances();
            foreach (ManagementObject strt in mcol)
            {
                StartUpList.Add(strt["Command"] + " Method : " + strt["Location"]);
            }
        }

        public void StartUpTimer()
        {
            MyTimer.Tick += CheckStartUpList;
            // Sets the timer interval to 5 seconds.
            MyTimer.Interval = 5000;
            MyTimer.Start();
        }

        public void CheckStartUpList(Object myObject, EventArgs myEventArgs)
        {
            var mangnmt = new ManagementClass("Win32_StartupCommand");
            var mcol = mangnmt.GetInstances();
            foreach (ManagementObject strt in mcol)
            {
                NewStartUpList.Add(strt["Command"] + " Method : " + strt["Location"]);
            }

            var diff = NewStartUpList.Except(StartUpList).ToList();
            foreach (var added in diff)
            {
                MessageBox.Show(added);
            }

            StartUpList.Clear();
            StartUpList.AddRange(NewStartUpList);
        }

Code:
StartUpTimer();

In the exemple it just shows a messagebox with the infos of the program added to startup (path and method). You can do what you want with the results (delete registry key, delete binary...).
But then you need to add a program like this to startup, and why would you use a timer? It would have been better to look for windows messages and changes.