如何使用 C# 杀死 Windows 中的警报窗口?

Dan*_*iel 6 c# windows kill process

我在 C# 中使用 System.Diagnostics.Process 命名空间来启动系统进程,有时这个新创建的进程无法正常启动,在这些情况下,Windows 会向我显示一个警报窗口,提供有关失败进程的信息。我需要一种以编程方式关闭(终止)此警报窗口的方法。我尝试了以下代码但它不起作用,因为警报窗口不会出现在 Process.GetProcesses() 列表中。

foreach(Process.GetProcesses() 中的进程 procR)
{
    if (procR.MainWindowTitle.StartsWith("警报窗口文本"))
    {
        procR.Kill();
        继续;
    } 
} 

我将不胜感激。谢谢!

更新:只是想让你知道这个例子对我有用。非常感谢。下面是一些可以帮助其他人的代码。该代码已使用 Visual Studio 2008 进行了测试,您仍然需要一个 winform 和一个按钮才能使其工作。

使用系统;
使用 System.Windows.Forms;
使用 System.Runtime.InteropServices;
/* 有关窗口类的更多信息,请访问 http://msdn.microsoft.com/en-us/library/ms633574(VS.85).aspx */

命名空间 WindowsFormsApplication1
{
    公共部分类 Form1 :表单
    {

        const uint WM_CLOSE = 0x10;

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        私有静态外部 IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);


        公共 Form1()
        {
            初始化组件();
        }

        /* 此事件将默默地杀死任何警告对话框 */
        private void button2_Click(object sender, EventArgs e)
        {
            string dialogBoxText = "重命名文件"; /* 当您尝试将文件设置为相同名称时,Windows 会给您此警报 */
            IntPtr hwnd = FindWindow("#32770", dialogBoxText);
            SendMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
        }

    }
}

Pav*_*sky 2

您可以尝试使用 PInvoke 通过参数(例如名称和/或窗口类)调用 FindWindow() API,然后 PInvoke SendMessage(window, WM_CLOSE, 0, 0) API 将其关闭。