Problem using System.Diagnostics.Process to launch Internet Explorer

G

garyusenet

I have been trying for some time now to launch Internet Explorer from
my code. I have read the MSDN notes on this way of starting processes
and used their example but couldn't get it to work.
I then searched the forums and made ammendments suggested to previous
posters but it still isn't working. Here is my StartInternetExplorer()
method. Could you please look and see what I am doing wrong?

Thankyou.

Gary.

private void StartInternetExplorer()
{
Process myProcess = new Process();

try
{

myProcess.StartInfo.FileName = "c:\\Program
Files\\iexplore.exe";
myProcess.StartInfo.Arguments =
"http://www.google.co.uk";
myProcess.EnableRaisingEvents = false;
myProcess.Start();
myProcess.WaitForExit();
}
catch (Win32Exception e)
{
if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
{
Console.WriteLine(e.Message + ". Internet Explorer
not in default location.");
}

else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
{
// Note that if your word processor might generate
exceptions
// such as this, which are handled first.
Console.WriteLine(e.Message + ". You do not have
permission to run this file.");
}
}
}
 
M

Michael Letterle

Shouldn't FileName be "c:\\Program Files\\Internet
Explorer\\iexplore.exe" ?
 
M

Marc Gravell

First - iexplore.exe doesn't live in program files, and 2nd, even if it
did you shouldn't assume C:\Program Files - you would check the
environment variables.

A better option is to let the path worry about this - just use
"iexplore.exe" as the command.

An *even* better option is to let the user's prefs decide which browser
to launch - use "http://someurl" as the command, and forget about the
args.

Marc
 
G

garyusenet

That did it. I didn't even think of the path being wrong as I thought
my try catch block would have taken care of filenames being faulty.

Can you please tell me why that wasn't the case?
 
G

garyusenet

Also - this is a small part of an app i'm writing which directly
interfaces with internet explorer, so it's only for internet explorer.
That's why i hard coded iexplore.exe.

I'll take a look at the paths though and try to make it more general
thanks!
 
G

garyusenet

Fixed. I was using console.writeline in a windows app...
Thanks for your help everyone.
Gary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top