views:

785

answers:

5

Hi,

I have a winforms application, that why someone clicks on a button I need to open up IE to a specific URL.

When someone closes the winforms app, I then need to close IE.

Is this possible? If yes, how?

+1  A: 

It would be possible to do that, but it might be a better idea to simply embed Internet Explorer into your application using the WebBrowser control. That way when you close the website you have no chance of closing the whole window when your website opened in a new tab in an existing IE window.

Edit: If you're going to do it anyway, look at the MSDN page on System.Diagnostics.Process.Close

Dan Walker
Yeah, closing the browser seems like a weird user experience--especially if there are other tabs open.
C. Dragon 76
A: 

Not sure how you are launching IE, but if you keep a reference to the launched process (or the window handle that was created), you can kill the process (or close the window) when you app exits.

EDIT:

Code to close a window handle is like

Utilities.Utilities.SendMessage(mTestPanelHandle, WM_COMMAND, WM_CLOSE, 0);

if you have a P/Invoke

public const int WM_COMMAND = 0x0112;
public const int WM_CLOSE = 0xF060;
[DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true,
        CallingConvention = CallingConvention.StdCall)]
public static extern int SendMessage(IntPtr hwnd, uint Msg, int wParam, int lParam)
Nick
+1  A: 

Yes it is possible,

When you call System.Diagnostics.Process.Start() to start the browser, use a specifically created process object. You can later use the process information to kill the process.

However, as Dan Walker mentioned, it might be a better idea to just use the Web Browser control, unless you have specific navigation needs, then it might be more effective to just start and kill IE.

Mitchel Sellers
+3  A: 

If you dont have the reference to the old process you used for launching IE, you have to search through the process array returned by System.Diagnostics.Process.GetProcessesByName("IEXPLORE") and kill the specific process.

some samples here.

Gulzar
A: 

Try this (assuming by WinForms you are using .NET)

Process ieProcess = Process.Start("iexplore", @"http://www.website.com");

// Do work, etc

ieProcess.Kill();

This will only kill the instance you started.

JTA
That would work if you're using IE8, with each tab as a separate process, but with IE7 where each window is a new process, it isn't a good idea to kill a whole Internet Explorer window when you don't have control of all the tabs which are opened.
Dan Walker
Very good point. I guess I was looking at "I then need to close IE" a bit leterally.
JTA