从C#应用程序使用自定义URL启动Web浏览器的最佳方法是什么? [英] What is the best way to launch a web browser with a custom url from a C# application?

查看:218
本文介绍了从C#应用程序使用自定义URL启动Web浏览器的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,使用System.Diagnostics.Process.Start是从C#应用程序启动url的方式:

It's common knowledge that using System.Diagnostics.Process.Start is the way to launch a url from a C# applicaiton:

System.Diagnostics.Process.Start( http://www.mywebsite.com );

System.Diagnostics.Process.Start("http://www.mywebsite.com");

但是,如果此URL无效,则应用程序似乎无法知道调用失败或原因。有没有更好的方法来启动网络浏览器?如果没有,我最好的URL验证方法是什么?

However, if this url is invalid the application seems to have no way of knowing that the call failed or why. Is there a better way to launch a web browser? If not, what is my best option for url validation?

推荐答案

尝试以下方法。

try
{
    var url = new Uri("http://www.example.com/");

    Process.Start(url.AbsoluteUri);
}
catch (UriFormatException)
{
    // URL is not parsable
}

这不能确保资源存在,但是可以确保URL格式正确。您可能还想检查该方案是否匹配http或https。

This does not ensure that the resource exist, but it does ensure the URL is wellformed. You might also want to check if the scheme is matching http or https.

这篇关于从C#应用程序使用自定义URL启动Web浏览器的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆