| 1 |
using System; |
| 2 |
using System.ComponentModel; |
| 3 |
using System.Diagnostics; |
| 4 |
using System.Windows.Forms; |
| 5 |
using System.IO; |
| 6 |
using Microsoft.Win32; |
| 7 |
|
| 8 |
namespace nft.controls |
| 9 |
{ |
| 10 |
/// <summary> |
| 11 |
/// LinkLabel control that launchs a browser and |
| 12 |
/// navigates to a specified URL. |
| 13 |
/// </summary> |
| 14 |
public class UrlLinkLabel : LinkLabel |
| 15 |
{ |
| 16 |
private string targetUrl; |
| 17 |
|
| 18 |
[ |
| 19 |
Description("URL to be opened") |
| 20 |
] |
| 21 |
public string TargetUrl { |
| 22 |
get { return targetUrl; } |
| 23 |
set { targetUrl=value; } |
| 24 |
} |
| 25 |
|
| 26 |
public UrlLinkLabel() {} |
| 27 |
|
| 28 |
protected override void OnLinkClicked(LinkLabelLinkClickedEventArgs e) { |
| 29 |
base.OnLinkClicked(e); |
| 30 |
|
| 31 |
// Because shell execuse doesn't work |
| 32 |
// we have to specify executing module directory |
| 33 |
ProcessStartInfo info = new ProcessStartInfo(); |
| 34 |
// get default browser (exe) path |
| 35 |
RegistryKey rkey = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command"); |
| 36 |
String val = rkey.GetValue("").ToString(); |
| 37 |
Debug.WriteLine(val); |
| 38 |
if(val.StartsWith("\"")) |
| 39 |
{ |
| 40 |
int n = val.IndexOf("\"",1); |
| 41 |
info.FileName = val.Substring(1,n-1); |
| 42 |
info.Arguments = val.Substring(n+1); |
| 43 |
} |
| 44 |
else |
| 45 |
{ |
| 46 |
string[] a = val.Split(new char[]{' '}); |
| 47 |
info.FileName = a[0]; |
| 48 |
info.Arguments = val.Substring(a[0].Length+1); |
| 49 |
} |
| 50 |
// Specifies working directory is necessary to run browser successfuly. |
| 51 |
info.WorkingDirectory = Path.GetDirectoryName(info.FileName); |
| 52 |
|
| 53 |
info.Arguments += targetUrl; |
| 54 |
Debug.WriteLine(info.FileName); |
| 55 |
Debug.WriteLine(info.WorkingDirectory); |
| 56 |
Debug.WriteLine(info.Arguments); |
| 57 |
System.Diagnostics.Process.Start(info); |
| 58 |
|
| 59 |
// Following code doesn't work. I don't know why... |
| 60 |
//System.Diagnostics.Process.Start(targetUrl); |
| 61 |
} |
| 62 |
} |
| 63 |
} |