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

Ale*_*sky 117 .net c# wpf

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

Ini*_*eer 251

System.Diagnostics.Process.Start("http://www.webpage.com");
Run Code Online (Sandbox Code Playgroud)

许多方法之一.

  • 我也使用过这个,但现在事实证明这不适用于UAC.在我的应用程序中,我在清单中有这个<requestedExecutionLevel level ="requireAdministrator"uiAccess ="false"/>当我在Windows 8下运行应用程序时(你不能再禁用UAC),打开网页时出现以下异常:Win32Exception(0x80004005):未在System.Diagnostics.Process.StartWithShellExecuteEx上注册的类 (3认同)
  • 如果您不小心从用户输入获取URL而不验证它是URI,这可能会在您的应用程序中打开一个相当大的安全漏洞.然后,他们可以在您的系统上启动他们想要的任何应用 (2认同)

Ale*_*nov 58

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

var psi = new ProcessStartInfo
{
    FileName = url,
    UseShellExecute = true
};
Process.Start (psi);
Run Code Online (Sandbox Code Playgroud)


ajm*_*jma 32

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

System.Diagnostics.Process.Start("http://www.google.com"); 
Run Code Online (Sandbox Code Playgroud)


cdi*_*ins 15

虽然给出了一个很好的答案(使用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;
}
Run Code Online (Sandbox Code Playgroud)


mlo*_*kot 9

Microsoft在KB305703文章中解释了它如何使用Visual C#以编程方式启动默认Internet浏览器.

不要忘记查看"故障排除"部分.


lvm*_*jer 6

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

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

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

  • 见[评论](http://stackoverflow.com/questions/1173630/how-do-you-de-elevate-privileges-for-a-child-process#comment40205343_15041823),使用`Explorer.exe`降级不支持:“不幸的是,Windows Shell 团队已经回复说,“Explorer.exe AppName.exe”的当前行为是一个错误,可能在未来的 Windows 更新/版本中不起作用。应用程序不应依赖它。” (2认同)

mr.*_*123 5

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

有2个选项:

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

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

  3. 通过“资源管理器”打开(行为就像你在文件夹窗口 url 中写了 url)

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

代码:

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;
    }
Run Code Online (Sandbox Code Playgroud)

和助手功能:

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;
    }
Run Code Online (Sandbox Code Playgroud)

希望我有所帮助。