使用C#仅关闭Google chrome隐身窗口 [英] using C# to close Google chrome incognito windows only

查看:297
本文介绍了使用C#仅关闭Google chrome隐身窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个小程序来关闭google incogneto窗口.

I wanted to create a small program to close google incogneto windows.

我有杀死所有Chrome窗口的代码,但是我不确定如何仅隔离隐身窗口

I have the code to kill ALL chrome windows but im not sure how to isolate just the incognito windows

现有代码:

        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.

但是,似乎必须杀死chrome可执行文件才能关闭实际窗口,不幸的是,该可执行文件会关闭所有chrome窗口.

However, it appears the chrome executable has to be killed to close the actual window, which unfortunately closes all the chrome windows.

您可能可以使用Windows句柄获得类似SendKeys的东西来模拟Alt-F4,或者,如果我没有记错的话,.Net 4.5可以尝试一些其他关闭例程.

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.

尽管如此,这是确定哪些窗口是chrome以及哪些窗口是隐身的代码.然后他们杀死",但它不会关闭窗口,只会杀死浏览器(Av,Snap!就像Chrome所说的那样).

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

这篇关于使用C#仅关闭Google chrome隐身窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆