views:

79

answers:

3

I need to open Firefox from my console app, do stuff with it, and then shut it down a few minutes later.

I'm using Process.Start("firefox.exe", "myurl"); to open firefox, which isn't a problem. The issue is with closing it.

The CloseMainWindow() function usually works, but it doesn't work if firefox has a modal window open (like a "download failed" message box). In this case, I can call Kill() on the process, however this seems dirty.

Also, if I call Kill() on the process, then the next time firefox opens there's a "session restore" tab open, which isn't cool. I could try to disable the session restore functionality, which would get around that issue. However, I'm still concerned that killing ffx like that could cause other problems.

Does anyone know a good way of doing this? Is the "cleanest" way to try and pinvoke to get the modal window handle, close that, and then retry CloseMainWindow()?

+1  A: 

maybe watin can help you

public void SearchForWatiNOnGoogle()
{
    using (var browser = new Firefox("http://www.google.com"))
    {
        browser.TextField(Find.ByName("q")).TypeText("WatiN");
        browser.Button(Find.ByName("btnG")).Click();
        browser.Close();

     }
  }
Esben Skov Pedersen
cool, i didn't know about this.
dan
A: 

AutoIt has a COM library called AutoItX which can help you easily and reliably deal with manipulating windows from other processes, including closing them...

It is primarily a scripting language, so if you are not happy with using COM from .Net you could also compile a AutoIt script to an EXE and run that from your .Net application.

http://www.autoitscript.com/

Kharlos Dominguez