Open URL with standard browser.

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
 
S

Shakir Hussain

Jan.

Can you paste the code launching the process clicking link label?

Shak.
 
N

Nicholas Paldino [.NET/C# MVP]

Jan,

When you are calling Start on the Process class, does the instance of
ProcessStartInfo have the UseShellExecute property set to true?
 
J

Jan Krumsiek

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Jan,

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

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

Cheers,
 
S

Shakir Hussain

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
 
J

Jan Krumsiek

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
 
J

Jan Krumsiek

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
 
S

Shakir Hussain

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
 
J

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
 
R

Robert Misiak

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
 

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