C#
private
void OpenUrl(
string url)
{
string browser = GetDefaultBrowser();
if ( browser.Length > 0 )
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.Arguments = url;
psi.FileName = browser;
System.Diagnostics.Process.Start(psi);
}
else
{
System.Diagnostics.Process.Start(url);
}
}
private string GetDefaultBrowser()
{
string browser = String.Empty;
RegistryKey key = null;
try
{
key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);
browser = key.GetValue( null).ToString().ToLower().Replace("\"", "");
if ( !browser.EndsWith(".exe") )
{
browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);
}
}
catch
{
if ( key != null )
{
key.Close();
}
}
return browser;
}
{
string browser = GetDefaultBrowser();
if ( browser.Length > 0 )
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.Arguments = url;
psi.FileName = browser;
System.Diagnostics.Process.Start(psi);
}
else
{
System.Diagnostics.Process.Start(url);
}
}
private string GetDefaultBrowser()
{
string browser = String.Empty;
RegistryKey key = null;
try
{
key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);
browser = key.GetValue( null).ToString().ToLower().Replace("\"", "");
if ( !browser.EndsWith(".exe") )
{
browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4);
}
}
catch
{
if ( key != null )
{
key.Close();
}
}
return browser;
}
VB.NET
Private
Sub OpenUrl()
Sub OpenUrl(
ByRef url
As
String )
Dim browser As String = GetDefaultBrowser()
If browser.Length > 0 Then
Dim psi As ProcessStartInfo = New ProcessStartInfo
psi.UseShellExecute = False
psi.Arguments = url
psi.FileName = browser
System.Diagnostics.Process.Start(psi)
Else
System.Diagnostics.Process.Start(url)
End If
End Sub
Private Function GetDefaultBrowser() Function GetDefaultBrowser()
Dim browser As String = String.Empty
Dim key As RegistryKey
Try
key = Registry.ClassesRoot.OpenSubKey("HTTP\shell\open\command", False)
browser = key.GetValue( Nothing).ToString().ToLower().Replace("""", "")
If Not browser.EndsWith(".exe") Then
browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4)
End If
Catch ex As Exception
If Not key Is Nothing Then
key.Close()
End If
End Try
GetDefaultBrowser = browser
End Function
Dim browser As String = GetDefaultBrowser()
If browser.Length > 0 Then
Dim psi As ProcessStartInfo = New ProcessStartInfo
psi.UseShellExecute = False
psi.Arguments = url
psi.FileName = browser
System.Diagnostics.Process.Start(psi)
Else
System.Diagnostics.Process.Start(url)
End If
End Sub
Private Function GetDefaultBrowser() Function GetDefaultBrowser()
Dim browser As String = String.Empty
Dim key As RegistryKey
Try
key = Registry.ClassesRoot.OpenSubKey("HTTP\shell\open\command", False)
browser = key.GetValue( Nothing).ToString().ToLower().Replace("""", "")
If Not browser.EndsWith(".exe") Then
browser = browser.Substring(0, browser.LastIndexOf(".exe") + 4)
End If
Catch ex As Exception
If Not key Is Nothing Then
key.Close()
End If
End Try
GetDefaultBrowser = browser
End Function
本文转自博客园鸟食轩的博客,原文链接:http://www.cnblogs.com/birdshome/,如需转载请自行联系原博主。