2011-03-11 24 views
1

我有一个c#的.dll库,它喜欢在启动时弹出一个“欢迎”屏幕。 此屏幕在任务管理器中显示为应用程序。如何检测从C#启动的新应用程序?

是否有某种方式来自动检测该应用程序/表格正在启动和关闭它?

谢谢! :)

+0

可能重复http://stackoverflow.com/questions/848618/net-events-for-process-executable-start – Terry 2011-03-11 10:35:08

回答

3

未由您自己的大会打开的表单。

foreach (Form form in Application.OpenForms) 
    if (form.GetType().Assembly != typeof(Program).Assembly) 
     form.Close(); 

什么是你自己的大会是由类Program定义,你也可以使用Assembly.GetExecutingAssemblyAssembly.GetCallingAssembly,但我不知道它会正确运行,如果运行Visual Studio中的应用程序(因为它可能返回VS组件)。

+0

它工作!目前我把它挂在一个计时器,并作出if(form.Text ==“欢迎”){form.Close(); }并关闭。 :)还有一件事,我如何检测到一个新的表单正在打开,这样我就可以在创建表单的时候运行这个表单,而不是通过定时器呢? :) – Roger 2011-03-11 11:14:24

+0

在计时器上运行这不是很好,是的。您应该能够追踪表格打开的来源,然后直接关闭它。有可能获得一个[回调表单打开](http://stackoverflow.com/questions/1603600/winforms-is-there-a-way-to-be-informed-whenever-a-form-gets-gets-在我的应用程序中打开),但我认为我之前尝试过类似的东西,但它不起作用。 – xod 2011-03-11 12:09:07

3

这里我们简单的控制台应用程序如果它是你的进程中运行,并打开一个表格(不是对话框),你可以使用这样的事情,关闭所有将监控和关闭指定的窗口

class Program 
{ 
    static void Main(string[] args) 
    { 
     while(true) 
     { 
      FindAndKill("Welcome"); 
      Thread.Sleep(1000); 
     } 
    } 

    private static void FindAndKill(string caption) 
    { 
     Process[] processes = Process.GetProcesses(); 
     foreach (Process p in processes) 
     { 
      IntPtr pFoundWindow = p.MainWindowHandle;   
      StringBuilder windowText = new StringBuilder(256); 

      GetWindowText(pFoundWindow, windowText, windowText.Capacity); 
      if (windowText.ToString() == caption) 
      { 
       p.CloseMainWindow(); 
       Console.WriteLine("Excellent kill !!!"); 
      } 
     } 
    } 

    [DllImport("user32.dll", EntryPoint = "GetWindowText",ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern int GetWindowText(IntPtr hWnd,StringBuilder lpWindowText, int nMaxCount); 
} 
+1

Console.WriteLine(“MMMMMMONSTER KILL!”); = P – 2011-03-11 10:37:09

+0

MMMMMMONSTER KILL! :) *竖起大拇指* – Roger 2011-03-11 10:46:32

+0

感谢您的代码,我会检查出来!顺便说一句,我应该添加user32.dll作为参考? – Roger 2011-03-11 10:47:16

相关问题