2010-06-23 43 views
2

我在C#中使用System.Diagnostics.Process命名空間來啓動系統進程,有時這個新創建的進程無法正常啓動,在這些情況下,Windows會顯示一個警報窗口,提供有關失敗進程的信息。我需要一種以編程方式關閉(殺死)此警報窗口的方法。我嘗試了下面的代碼,但它不起作用,因爲警報窗口不會出現在Process.GetProcesses()列表中。如何在Windows中使用C#殺死警報窗口?

foreach (Process procR in Process.GetProcesses()) 
{ 
    if (procR.MainWindowTitle.StartsWith("alert window text")) 
    { 
     procR.Kill(); 
     continue; 
    } 
}

我會很感激這方面的幫助。 謝謝!

UPDATE: 只是想讓你知道,這個例子爲我工作。非常感謝你。下面有一些代碼可以幫助別人。代碼已經過Visual Studio 2008測試,您仍然需要一個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); 
     } 

    } 
} 
+0

你不應該做這樣的破解! – Phil1970 2016-08-04 13:25:06

回答

2

您可以嘗試使用PInvoke通過名稱和/或窗口類等參數調用FindWindow()API,然後PInvoke SendMessage(window,WM_CLOSE,0,0)API將其關閉。

0

正確,因爲警報窗口(正確地稱爲消息框)不是應用程序的主窗口。

我想你必須使用EnumThreadWindowsGetWindowText來檢查進程的窗口。

+0

謝謝ErikHeemskerk!我會試試這個! – Daniel 2010-06-23 13:32:35