如何从我的应用程序中打开网页?

时间:2009-02-02 04:36:40

标签: c# .net wpf

我想让我的WPF应用程序打开默认浏览器并转到某个网页。我该怎么做?

12 个答案:

答案 0 :(得分:237)

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

众多方式中的一种。

答案 1 :(得分:30)

我一直在使用此行启动默认浏览器:

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

答案 2 :(得分:14)

虽然给出了一个很好的答案(使用Process.Start),但将它封装在一个函数中是更安全的,该函数检查传递的字符串确实是一个URI,以避免意外启动机器上的随机进程。

public static bool IsValidUri(string uri)
{
    if (!Uri.IsWellFormedUriString(uri, UriKind.Absolute))
        return false;
    Uri tmp;
    if (!Uri.TryCreate(uri, UriKind.Absolute, out tmp))
        return false;
    return tmp.Scheme == Uri.UriSchemeHttp || tmp.Scheme == Uri.UriSchemeHttps;
}

public static bool OpenUri(string uri) 
{
    if (!IsValidUri(uri))
        return false;
     System.Diagnostics.Process.Start(uri);
     return true;
}

答案 3 :(得分:8)

Microsoft在How to start the default Internet browser programmatically by using Visual C#上的 KB305703 文章中解释了这一点。

不要忘记查看“疑难解答”部分。

答案 4 :(得分:5)

您无法从提升的应用程序启动网页。这将引发0x800004005异常,可能是因为explorer.exe和浏览器正在运行非提升。

要在非提升的Web浏览器中从提升的应用程序启动网页,请使用code made by Mike Feng。我试图将URL传递给lpApplicationName,但这不起作用。当我使用带有lpApplicationName =“explorer.exe”(或iexplore.exe)和lpCommandLine = url的CreateProcessWithTokenW时也不行。

以下解决方法确实有效:创建一个包含一个任务的小型EXE项目:Process.Start(url),使用CreateProcessWithTokenW运行此.EXE。在我的Windows 8 RC上,这可以正常工作,并在谷歌浏览器中打开网页。

答案 5 :(得分:4)

这是我完整的代码如何打开。

有两个选项:

  1. 使用默认浏览器打开(行为就像在浏览器窗口中打开一样)

  2. 打开默认命令选项(行为就像你使用“RUN.EXE”命令)

  3. 通过'explorer'打开(行为就像你在你的文件夹窗口网址中写了url)

  4. [可选建议] 4.使用iexplore进程位置打开所需的URL

    CODE:

    internal static bool TryOpenUrl(string p_url)
        {
            // try use default browser [registry: HKEY_CURRENT_USER\Software\Classes\http\shell\open\command]
            try
            {
                string keyValue = Microsoft.Win32.Registry.GetValue(@"HKEY_CURRENT_USER\Software\Classes\http\shell\open\command", "", null) as string;
                if (string.IsNullOrEmpty(keyValue) == false)
                {
                    string browserPath = keyValue.Replace("%1", p_url);
                    System.Diagnostics.Process.Start(browserPath);
                    return true;
                }
            }
            catch { }
    
            // try open browser as default command
            try
            {
                System.Diagnostics.Process.Start(p_url); //browserPath, argUrl);
                return true;
            }
            catch { }
    
            // try open through 'explorer.exe'
            try
            {
                string browserPath = GetWindowsPath("explorer.exe");
                string argUrl = "\"" + p_url + "\"";
    
                System.Diagnostics.Process.Start(browserPath, argUrl);
                return true;
            }
            catch { }
    
            // return false, all failed
            return false;
        }
    

    和助手功能:

    internal static string GetWindowsPath(string p_fileName)
        {
            string path = null;
            string sysdir;
    
            for (int i = 0; i < 3; i++)
            {
                try
                {
                    if (i == 0)
                    {
                        path = Environment.GetEnvironmentVariable("SystemRoot");
                    }
                    else if (i == 1)
                    {
                        path = Environment.GetEnvironmentVariable("windir");
                    }
                    else if (i == 2)
                    {
                        sysdir = Environment.GetFolderPath(Environment.SpecialFolder.System);
                        path = System.IO.Directory.GetParent(sysdir).FullName;
                    }
    
                    if (path != null)
                    {
                        path = System.IO.Path.Combine(path, p_fileName);
                        if (System.IO.File.Exists(path) == true)
                        {
                            return path;
                        }
                    }
                }
                catch { }
            }
    
            // not found
            return null;
        }
    

    希望我帮助过。

答案 6 :(得分:2)

我测试过它的效果很好

我一直在使用此行启动默认浏览器:

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

答案 7 :(得分:2)

我有解决方案,因为我今天遇到了类似的问题。

假设您要从使用admin priviliges运行的应用中打开http://google.com

ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe", "http://www.google.com/");
Process.Start(startInfo); 

答案 8 :(得分:1)

接受的答案不再适用于 .NET Core 3 。要使其正常工作,请使用以下方法:

var psi = new ProcessStartInfo
{
    FileName = url,
    UseShellExecute = true
};
Process.Start (psi);

答案 9 :(得分:0)

老派的方式;)

public static void openit(string x) {
   System.Diagnostics.Process.Start("cmd", "/C start" + " " + x); 
}

使用:openit("www.google.com");

答案 10 :(得分:0)

    string target= "http://www.google.com";
try
{
    System.Diagnostics.Process.Start(target);
}
catch (System.ComponentModel.Win32Exception noBrowser)
{
    if (noBrowser.ErrorCode==-2147467259)
    MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
    MessageBox.Show(other.Message);
}

答案 11 :(得分:-1)

我认为这是最常用的:

System.Diagnostics.Process.Start("http://www.google.com");
相关问题