Skip to content

Instantly share code, notes, and snippets.

@strictlymike
Created August 24, 2016 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save strictlymike/46d717a929e38460b5774476878db125 to your computer and use it in GitHub Desktop.
Save strictlymike/46d717a929e38460b5774476878db125 to your computer and use it in GitHub Desktop.
Quick and dirty copy-pasta process tracing systray app for the curious and paranoid
using System;
using System.Diagnostics;
using System.Drawing;
using System.Management;
using System.Windows.Forms;
namespace MyTrayApp
{
public class SysTrayApp : Form
{
[STAThread]
public static void Main()
{
SysTrayApp app = new SysTrayApp();
ManagementEventWatcher startWatch = new ManagementEventWatcher(
new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"));
startWatch.EventArrived += new EventArrivedEventHandler(app.NotifyProcessStart);
startWatch.Start();
Application.Run(app);
}
private NotifyIcon trayIcon;
private ContextMenu trayMenu;
public void NotifyProcessStart(object sender, EventArrivedEventArgs e)
{
string name = (string)e.NewEvent.Properties["ProcessName"].Value;
string path = name;
const int timeout = 8;
try {
Console.WriteLine("PID: {0}", e.NewEvent.Properties["ProcessID"].Value);
Process p = Process.GetProcessById(Convert.ToInt32(e.NewEvent.Properties["ProcessID"].Value));
path = p.MainModule.FileName;
trayIcon.ShowBalloonTip(timeout, "New Process", name + " (" + path + ")", ToolTipIcon.Info);
} catch (Exception) {
trayIcon.ShowBalloonTip(timeout, "New Process", name + " (exited immediately)", ToolTipIcon.Info);
}
}
public SysTrayApp()
{
trayMenu = new ContextMenu();
trayMenu.MenuItems.Add("Exit", OnExit);
trayIcon = new NotifyIcon();
trayIcon.Text = "MyTrayApp";
trayIcon.Icon = new Icon(SystemIcons.Application, 40, 40);
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = true;
}
protected override void OnLoad(EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
base.OnLoad(e);
}
private void OnExit(object sender, EventArgs e)
{
Application.Exit();
}
protected override void Dispose(bool isDisposing)
{
if (isDisposing) { trayIcon.Dispose(); }
base.Dispose(isDisposing);
}
}
}
@strictlymike
Copy link
Author

Easier than the event log notification/scraper I was going to write, and gives me a quick idea of what is happening. TODO: add parentage, arguments, etc., etc.

Copy link

ghost commented Aug 24, 2016

Love the code and it's so neat. Good job.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment