[Mono-list] Opening user's browser
Felipe Almeida Lessa
felipe.lessa at gmail.com
Fri Dec 30 07:58:18 EST 2005
On 12/29/05, Miguel de Icaza <miguel at ximian.com> wrote:
> Hello,
>
> > I'd like to have a function that opens a link in the user's web
> > browser. Googling about it I didn't find an arch-independent solution.
> > The best I could do until now is:
>
> The answers to this post deserve to make it into our FAQ.
>
> Felipe, do you have a summary? I can give you a Wiki account, it might
> come in handy later as well.,
>
> Miguel
>
You mean something like:
== How to open a link in the user's browser? ==
Currently different environments have different ways of opening the
user's web browser:
* Windows: you just execute a command with only the URL of the site
and its shell opens the right web browser.
* Linux: there are several different systems. On GNOME you can use
"gnome-open" command. You could also read the BROWSER environment
variable or use the "sensible-browser" command on Debian systems, but
these may call a console browser, which is not desirable. Another
possiblity is to try to open several common browsers, such as Firefox,
Epiphany, Konqueror, Galeon, Mozilla and Opera.
* Mac: you can use the "open" command.
If you want your program to be cross-platform, you have to try to
support each way of opening an URL. You can do so by having a function
like:
<code>
using System;
using System.Diagnostics;
/// <summary>
/// Tries to open a link on the user's web browser.
/// </summary>
/// <remarks>
/// You just need to call this function with the URL of the address of the
/// web site and have a fallback in case it returns false.
/// <return>
/// Returns true if the link could be successfully opened, false otherwise.
/// </return>
/// <param name="address">
/// The URL of the site you want to open (e.g. "http://www.mono-project.com/").
/// </param>
public static bool OpenLink(string address) {
try {
int plat = (int) Environment.OSVersion.Platform;
if ((plat != 4) && (plat != 128)) {
// Use Microsoft's way of opening sites
Process.Start(address);
} else {
// We're on Unix, try gnome-open (used by GNOME), then open
// (used my MacOS), then Firefox or Konqueror browsers (our last
// hope).
string cmdline = String.Format("gnome-open {0} || open {0} || "+
"firefox {0} || mozilla-firefox {0} || konqueror {0}", address);
Process proc = Process.Start (cmdline);
// Sleep some time to wait for the shell to return in case of error
System.Threading.Thread.Sleep(250);
// If the exit code is zero or the process is still running then
// appearently we have been successful.
return (!proc.HasExited || proc.ExitCode == 0);
}
} catch (Exception e) {
// We don't want any surprises
return false;
}
}
</code>
If your program is meant to be run only under GNOME, you have a better
and easier solution, however. It is suficient to call Gnome.Url::Show,
as shown below:
<code>
import Gnome;
...
public void OpenMyProgramWebsite()
{
Url.Show("http://websiteofmyproject/");
}
</code>
--
"Quem excele em empregar a força militar subjulga os exércitos dos outros
povos sem travar batalha, toma cidades fortificadas dos outros povos sem as
atacar e destrói os estados dos outros povos sem lutas prolongadas. Deve
lutar sob o Céu com o propósito primordial da 'preservação'. Desse modo suas
armas não se embotarão, e os ganhos poderão ser preservados. Essa é a
estratégia para planejar ofensivas."
-- Sun Tzu, em "A arte da guerra"
More information about the Mono-list
mailing list