如何使用C#关闭Windows中的警报窗口?

如何使用C#关闭Windows中的警报窗口?,c#,windows,process,kill,C#,Windows,Process,Kill,我使用C#中的System.Diagnostics.Process命名空间启动系统进程,有时新创建的进程无法正常启动,在这种情况下,Windows会向我显示一个警报窗口,提供有关失败进程的信息。我需要一种方法以编程方式关闭(杀死)此警报窗口。我尝试了以下代码,但它不起作用,因为警报窗口不会出现在Process.getprocesss()列表中 foreach(Process pror in Process.getprocesss()) { if(pror.MainWindowTitle.Star

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

foreach(Process pror in Process.getprocesss()) { if(pror.MainWindowTitle.StartsWith(“警报窗口文本”)) { prol.Kill(); 继续; } } 我将非常感谢在这方面的任何帮助。 谢谢

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

using System; using System.Windows.Forms; using System.Runtime.InteropServices; /* More info about Window Classes at http://msdn.microsoft.com/en-us/library/ms633574(VS.85).aspx */ namespace WindowsFormsApplication1 { public partial class Form1 : Form { const uint WM_CLOSE = 0x10; [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); public Form1() { InitializeComponent(); } /* This event will silently kill any alert dialog box */ private void button2_Click(object sender, EventArgs e) { string dialogBoxText = "Rename File"; /* Windows would give you this alert when you try to set to files to the same name */ IntPtr hwnd = FindWindow("#32770", dialogBoxText); SendMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero); } } } 使用制度; 使用System.Windows.Forms; 使用System.Runtime.InteropServices; /*有关窗口类的详细信息,请访问http://msdn.microsoft.com/en-us/library/ms633574(第85节)*/ 命名空间Windows窗体应用程序1 { 公共部分类Form1:Form { 常数WM_CLOSE=0x10; [DllImport(“user32.dll”,EntryPoint=“FindWindow”,SetLastError=true)] 私有静态外部IntPtr FindWindow(字符串lpClassName,字符串lpWindowName); [DllImport(“user32.dll”,CharSet=CharSet.Auto)] 静态外部IntPtr发送消息(IntPtr hWnd、UInt32消息、IntPtr wParam、IntPtr LPRAM); 公共表格1() { 初始化组件(); } /*此事件将自动终止任何警报对话框*/ 私有无效按钮2\u单击(对象发送者,事件参数e) { string dialogBoxText=“重命名文件”;/*当您尝试将文件设置为相同名称时,Windows会发出此警报*/ IntPtr hwnd=FindWindow(“32770”,dialogBoxText); 发送消息(hwnd、WM_CLOSE、IntPtr.Zero、IntPtr.Zero); } } }
正确,因为警报窗口(正确地称为消息框)不是应用程序的主窗口


我认为您必须使用和检查进程的windows。

您可以尝试使用PInvoke通过参数(如名称和/或窗口类)调用FindWindow()API,然后PInvoke发送消息(窗口,WM_CLOSE,0,0)API关闭它。

谢谢ErikHeemskerk!我要试试这个!你不应该做这种事@Phil1970为什么你永远不能做这个黑客? using System; using System.Windows.Forms; using System.Runtime.InteropServices; /* More info about Window Classes at http://msdn.microsoft.com/en-us/library/ms633574(VS.85).aspx */ namespace WindowsFormsApplication1 { public partial class Form1 : Form { const uint WM_CLOSE = 0x10; [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); public Form1() { InitializeComponent(); } /* This event will silently kill any alert dialog box */ private void button2_Click(object sender, EventArgs e) { string dialogBoxText = "Rename File"; /* Windows would give you this alert when you try to set to files to the same name */ IntPtr hwnd = FindWindow("#32770", dialogBoxText); SendMessage(hwnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero); } } }