Reputation: 11702
I wanted to create a small program to close google incogneto windows.
I have the code to kill ALL chrome windows but im not sure how to isolate just the incognito windows
existing code:
Process[] proc = Process.GetProcessesByName("MyApp");
foreach (Process prs in proc)
{
prs.Kill();
}
Upvotes: 3
Views: 2469
Reputation: 62841
I played around with this a little but didn't have complete success. I was able to determine which windows were incognito, and from there, technically kill the process.
However, it appears the chrome executable has to be killed to close the actual window, which unfortunately closes all the chrome windows.
You may be able to get something like SendKeys to simulate an Alt-F4 using the windows handle, or if I'm not mistaken, .Net 4.5 has some additional closing routines you could try.
Nonetheless, here is the code to determine which windows are chrome and which of those are incognito. They then "kill", but it doesn't close the window, just kills the browsing (Aw, Snap! as Chrome puts it).
[DllImport("user32.dll")]
static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern bool CloseWindow(IntPtr hWnd);
[DllImport("user32")]
private static extern bool SetForegroundWindow(IntPtr hwnd);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
private void button1_Click(object sender, EventArgs e)
{
var proc = Process.GetProcesses().OrderBy(x => x.ProcessName);
foreach (Process prs in proc)
if (prs.ProcessName == "chrome" && WmiTest(prs.Id))
{
prs.Kill();
//To test SendKeys, not working, but gives you the idea
//SetForegroundWindow(prs.Handle);
//SendKeys.Send("%({F4})");
}
}
private bool WmiTest(int processId)
{
using (ManagementObjectSearcher mos = new ManagementObjectSearcher(string.Format("SELECT CommandLine FROM Win32_Process WHERE ProcessId = {0}", processId)))
foreach (ManagementObject mo in mos.Get())
if (mo["CommandLine"].ToString().Contains("--disable-databases"))
return true;
return false;
}
Upvotes: 4