C#使用默认浏览器打开指定网页

来源:互联网 发布:劫中劫电影西班牙 知乎 编辑:程序博客网 时间:2024/05/02 07:42

C#使用默认浏览器打开指定网页

原理:通过注册表取得默认浏览器的路径,然后用进程打开即可。

1.引用命名空间

using System.Text.RegularExpressions;
using Microsoft.Win32;

2.将如下代码插入指定位置:

RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\");
             string s = key.GetValue("").ToString();
 
             Regex reg = new Regex("\"([^\"]+)\"");
             MatchCollection matchs = reg.Matches(s);
 
             string filename="";
             if (matchs.Count > 0)
             {
                 filename = matchs[0].Groups[1].Value;
                 System.Diagnostics.Process.Start(filename, "http://www.5566.net") ;
             }

原创粉丝点击