简体   繁体   English

使用C#仅关闭Google Chrome隐身窗口

[英]using C# to close Google chrome incognito windows only

I wanted to create a small program to close google incogneto windows. 我想创建一个小程序来关闭谷歌incogneto窗口。

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();
        }

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. 但是,似乎必须杀死chrome可执行文件以关闭实际窗口,不幸的是关闭所有chrome窗口。

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. 您可以使用Windows手柄来获取类似SendKeys的模拟Alt-F4,或者如果我没有弄错,.Net 4.5还有一些额外的关闭例程。

Nonetheless, here is the code to determine which windows are chrome and which of those are incognito. 尽管如此,这里是确定哪些窗口是chrome以及哪些窗口是隐身的代码。 They then "kill", but it doesn't close the window, just kills the browsing (Aw, Snap! as Chrome puts it). 然后它们“杀死”,但它没有关闭窗口,只是杀死了浏览(噢,Snap!就像Chrome所说的那样)。

        [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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM