Open URL with standard browser.

  • Thread starter Thread starter Jan Krumsiek
  • Start date Start date
J

Jan Krumsiek

Hi.

I searched the Usenet and the Web for this simple information but did
not really find anything.


How can I open URL in the standard browser of the system? (VB6, VC++ you
could easily use the ShellExecute() function for this.)
The LinkLabel control example of MSDN does not work as I get a "file not
found exception" when System.Diagnostics.Process.Start() method.

Is there any platform-independent solution at all? Is there a solution
for Windows?


Regards,

Jan
 
Jan,

When you are calling Start on the Process class, does the instance of
ProcessStartInfo have the UseShellExecute property set to true?
 
Shakir said:
Can you paste the code launching the process clicking link label?

Sure I used the Microsoft example code. I have a LinkLabel called
"emailLink". When loading the form I use:


linkEmail.Text = "Click here to get more info.";
linkEmail.Links.Add(6,4,"www.microsoft.com");

// and then:

private void linkEmail_LinkClicked(object sender,
System.Windows.Forms.LinkLabelLinkClickedEventArgs e){

linkEmail.Links[linkEmail.Links.IndexOf(e.Link)].Visited = true;
System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
// ^^^^^^^^^^ this line throws an exception

}



(i know the link is not yet an email address but there's still the
microsoft code in there)


Regards,
Jan
 
Hi Jan,

If you use Process.Start( "document.ext" ) ;

it will create a new process and will use the application associated with
".ext"

Cheers,
 
Jan,

Try this. Please make sure that e.Link.LinkData.ToString() gets you the
string value. If its null, it wont work.

linkEmail.Links[linkEmail.Links.IndexOf(e.Link)].Visited = true;

Process linkLabelProc= new Process();
linkLabelProc.StartInfo.FileName = @"iexplore.exe";
linkLabelProc.StartInfo.Arguments = e.Link.LinkData.ToString();
linkLabelProc.Start();

Shak


Jan Krumsiek said:
Shakir said:
Can you paste the code launching the process clicking link label?

Sure I used the Microsoft example code. I have a LinkLabel called
"emailLink". When loading the form I use:


linkEmail.Text = "Click here to get more info.";
linkEmail.Links.Add(6,4,"www.microsoft.com");

// and then:

private void linkEmail_LinkClicked(object sender,
System.Windows.Forms.LinkLabelLinkClickedEventArgs e){

linkEmail.Links[linkEmail.Links.IndexOf(e.Link)].Visited = true;
System.Diagnostics.Process.Start(e.Link.LinkData.ToString());
// ^^^^^^^^^^ this line throws an exception

}



(i know the link is not yet an email address but there's still the
microsoft code in there)


Regards,
Jan
 
Nicholas said:
When you are calling Start on the Process class, does the instance of
ProcessStartInfo have the UseShellExecute property set to true?

Hello Nicholas


I tried this code


using System.Diagnostics;

// ...
// ...
// ...

ProcessStartInfo psi = new ProcessStartInfo("http://www.microsoft.com");
psi.UseShellExecute = true;

Process.Start(psi);



still does not work

Regards,
Jan
 
Shakir said:
linkEmail.Links[linkEmail.Links.IndexOf(e.Link)].Visited = true;

Process linkLabelProc= new Process();
linkLabelProc.StartInfo.FileName = @"iexplore.exe";
linkLabelProc.StartInfo.Arguments = e.Link.LinkData.ToString();
linkLabelProc.Start();

Thank you Shak, that code works... but it always uses IE instead of the
standard browser. Additionally I would like to use "mailto:" links to
open the email client which does not work very well with the code above.


Regards,

Jan Krumsiek
 
Jan,

to open default browser and mail client you have to do this

linkLabelProc.StartInfo.FileName = e.Link.LinkData.ToString(); //can be URL
or mailto::

remove this line from my previous example
linkLabelProc.StartInfo.Arguments = e.Link.LinkData.ToString();

Shak



Jan Krumsiek said:
Shakir said:
linkEmail.Links[linkEmail.Links.IndexOf(e.Link)].Visited = true;

Process linkLabelProc= new Process();
linkLabelProc.StartInfo.FileName = @"iexplore.exe";
linkLabelProc.StartInfo.Arguments = e.Link.LinkData.ToString();
linkLabelProc.Start();

Thank you Shak, that code works... but it always uses IE instead of the
standard browser. Additionally I would like to use "mailto:" links to
open the email client which does not work very well with the code above.


Regards,

Jan Krumsiek
 
Shakir said:
Jan,

to open default browser and mail client you have to do this

linkLabelProc.StartInfo.FileName = e.Link.LinkData.ToString(); //can be URL
or mailto::

remove this line from my previous example
linkLabelProc.StartInfo.Arguments = e.Link.LinkData.ToString();

No, that's almost the same issue as in my original posting. This does
NOT work. It throws a "file not found" exception if there is a URL in
StartInfo.Filename .


Regards,

Jan
 
You can figure out the default browser by reading the registry value
HKCR\HTTP\shell\open\command. Read the value and open it with
System.Diagnostics.Process.Start.

Robert
 
Back
Top