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

时间:2022-10-13 00:19:00

I want to make my WPF application open the default browser and go to a certain web page. How do I do that?

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

9 个解决方案

#1


212  

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

One of many ways.

许多方面之一。

#2


29  

I've been using this line to launch the default browser:

我一直在用这条线来启动默认浏览器:

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

#3


7  

Microsoft explains it in the KB305703 article on How to start the default Internet browser programmatically by using Visual C#.

微软在KB305703文章中解释了如何使用Visual c#以编程方式启动默认的Internet浏览器。

Don't forget to check the Troubleshooting section.

不要忘记检查故障排除部分。

#4


7  

While a good answer has been given (using Process.Start), it is safer to encapsulate it in a function that checks that the passed string is indeed a URI, to avoid accidentally starting random processes on the machine.

虽然已经给出了一个很好的答案(使用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;
}

#5


5  

You cannot launch a web page from an elevated application. This will raise a 0x800004005 exception, probably because explorer.exe and the browser are running non-elevated.

您无法从一个高级应用程序启动web页面。这将引发0x800004005异常,可能是因为explorer。exe和浏览器正在运行。

To launch a web page from an elevated application in a non-elevated web browser, use the code made by Mike Feng. I tried to pass the URL to lpApplicationName but that didn't work. Also not when I use CreateProcessWithTokenW with lpApplicationName = "explorer.exe" (or iexplore.exe) and lpCommandLine = url.

要在非升级的web浏览器中从高应用程序中启动web页面,请使用由Mike Feng编写的代码。我试图将URL传递给lpApplicationName,但这不起作用。也不是当我使用CreateProcessWithTokenW和lpApplicationName = "explorer "时。exe(或iexplore.exe)和lpCommandLine = url。

The following workaround does work: Create a small EXE-project that has one task: Process.Start(url), use CreateProcessWithTokenW to run this .EXE. On my Windows 8 RC this works fine and opens the web page in Google Chrome.

下面的解决方案可以工作:创建一个小的exe项目,它有一个任务:Process.Start(url),使用CreateProcessWithTokenW来运行这个. exe。在我的Windows 8 RC上,它运行良好,打开了谷歌Chrome的网页。

#6


4  

Here is my complete code how to open.

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

there are 2 options:

有两个选择:

  1. open using default browser (behavior is like opened inside the browser window)

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

  2. open through default command options (behavior is like you use "RUN.EXE" command)

    通过默认命令选项打开(行为就像使用“RUN”一样。EXE”命令)

  3. open through 'explorer' (behavior is like you wrote url inside your folder window url)

    打开“浏览器”(行为就像您在文件夹窗口url中编写url)

[optional suggestion] 4. use iexplore process location to open the required url

(可选的建议)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;
    }

and the Helper function:

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;
    }

Hope i helped.

希望我帮助。

#7


2  

I have tested it's working perfectly

我已经测试了它的运行效果。

I've been using this line to launch the default browser:

我一直在用这条线来启动默认浏览器:

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

#8


2  

I have the solution for this due to I have a similar problem today.

我有一个解决方案,因为我今天有一个类似的问题。

Supposed you want to open http://google.com from an app running with admin priviliges:

假设你想从一个运行管理特权的应用程序打开http://google.com:

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

#9


0  

The old school way ;)

旧学校的方式;

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

Use: openit("www.google.com");

用途:openit(“www.google.com”);

#1


212  

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

One of many ways.

许多方面之一。

#2


29  

I've been using this line to launch the default browser:

我一直在用这条线来启动默认浏览器:

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

#3


7  

Microsoft explains it in the KB305703 article on How to start the default Internet browser programmatically by using Visual C#.

微软在KB305703文章中解释了如何使用Visual c#以编程方式启动默认的Internet浏览器。

Don't forget to check the Troubleshooting section.

不要忘记检查故障排除部分。

#4


7  

While a good answer has been given (using Process.Start), it is safer to encapsulate it in a function that checks that the passed string is indeed a URI, to avoid accidentally starting random processes on the machine.

虽然已经给出了一个很好的答案(使用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;
}

#5


5  

You cannot launch a web page from an elevated application. This will raise a 0x800004005 exception, probably because explorer.exe and the browser are running non-elevated.

您无法从一个高级应用程序启动web页面。这将引发0x800004005异常,可能是因为explorer。exe和浏览器正在运行。

To launch a web page from an elevated application in a non-elevated web browser, use the code made by Mike Feng. I tried to pass the URL to lpApplicationName but that didn't work. Also not when I use CreateProcessWithTokenW with lpApplicationName = "explorer.exe" (or iexplore.exe) and lpCommandLine = url.

要在非升级的web浏览器中从高应用程序中启动web页面,请使用由Mike Feng编写的代码。我试图将URL传递给lpApplicationName,但这不起作用。也不是当我使用CreateProcessWithTokenW和lpApplicationName = "explorer "时。exe(或iexplore.exe)和lpCommandLine = url。

The following workaround does work: Create a small EXE-project that has one task: Process.Start(url), use CreateProcessWithTokenW to run this .EXE. On my Windows 8 RC this works fine and opens the web page in Google Chrome.

下面的解决方案可以工作:创建一个小的exe项目,它有一个任务:Process.Start(url),使用CreateProcessWithTokenW来运行这个. exe。在我的Windows 8 RC上,它运行良好,打开了谷歌Chrome的网页。

#6


4  

Here is my complete code how to open.

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

there are 2 options:

有两个选择:

  1. open using default browser (behavior is like opened inside the browser window)

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

  2. open through default command options (behavior is like you use "RUN.EXE" command)

    通过默认命令选项打开(行为就像使用“RUN”一样。EXE”命令)

  3. open through 'explorer' (behavior is like you wrote url inside your folder window url)

    打开“浏览器”(行为就像您在文件夹窗口url中编写url)

[optional suggestion] 4. use iexplore process location to open the required url

(可选的建议)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;
    }

and the Helper function:

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;
    }

Hope i helped.

希望我帮助。

#7


2  

I have tested it's working perfectly

我已经测试了它的运行效果。

I've been using this line to launch the default browser:

我一直在用这条线来启动默认浏览器:

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

#8


2  

I have the solution for this due to I have a similar problem today.

我有一个解决方案,因为我今天有一个类似的问题。

Supposed you want to open http://google.com from an app running with admin priviliges:

假设你想从一个运行管理特权的应用程序打开http://google.com:

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

#9


0  

The old school way ;)

旧学校的方式;

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

Use: openit("www.google.com");

用途:openit(“www.google.com”);