我想让我的 WPF 应用程序打开默认浏览器,进入某个网页。我该怎么做?
对于.NET 的桌面版本:
System.Diagnostics.Process.Start("http://www.webpage.com");
为了。NET 核心,ProcessStartInfo.UseShellExecute的默认值已经从 true变成了 false,所以你必须显式地将它设置为 true才能正常工作:
ProcessStartInfo.UseShellExecute
true
false
System.Diagnostics.Process.Start(new ProcessStartInfo { FileName = "http://www.webpage.com", UseShellExecute = true });
更复杂的是,对于 UWP 应用程序,这个属性 不能被设置为 true(因此这些解决方案都不适用于 UWP)。
我一直用这句话来启动默认浏览器:
System.Diagnostics.Process.Start("http://www.google.com");
您不能从提升的应用程序启动网页。这将引发一个0x800004005异常,可能是因为 Explorer.exe 和浏览器运行时没有升级。
若要在非升级的浏览器中从升级的应用程序启动网页,请使用 由 Mike Feng 编写的代码。我试图将 URL 传递给 lpApplicationName,但是没有成功。当我使用 CreateProcessWithTokenW 和 lpApplicationName = “ Explorer. exe”(或 iexpre.exe)和 lpCommandLine = url 时,也不会发生这种情况。
下面的解决方案确实有效: 创建一个只有一个任务的小 EXE 项目: Process。Start (url) ,使用 CreateProcessWithTokenW 运行此命令。EXE.在我的 Windows8RC 上,这个工作很好,可以在谷歌浏览器中打开网页。
这里是我的完整代码如何打开。
有两种选择:
使用默认浏览器打开(行为类似于在浏览器窗口中打开)
通过默认命令选项打开(行为类似于使用“ RUN.EXE”命令)
通过“ Explorer”打开(行为就像你在文件夹窗口中写了 url 一样)
[可选建议]4. 使用 iExplorer 进程位置打开所需的 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; }
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; }
希望我帮上忙了。
我有解决这个问题的办法,因为我今天有一个类似的问题。
假设您希望从具有管理特权的应用程序中打开 http://google.com:
ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe", "http://www.google.com/"); Process.Start(startInfo);
传统的方式;)
public static void openit(string x) { System.Diagnostics.Process.Start("cmd", "/C start" + " " + x); }
用途: openit("www.google.com");
openit("www.google.com");
虽然已经给出了一个很好的答案(使用 Process.Start) ,但是将其封装在一个检查传递的字符串是否确实是 URI 的函数中更安全,以避免意外地在机器上启动随机进程。
Process.Start
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; }
接受的答案在 .NET Core 3上不再起作用。要使其起作用,请使用以下方法:
var psi = new ProcessStartInfo { FileName = url, UseShellExecute = true }; Process.Start (psi);
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); }