【发布时间】:2010-10-04 20:53:43
【问题描述】:
我想让我的 WPF 应用程序打开默认浏览器并转到某个网页。我该怎么做?
【问题讨论】:
我想让我的 WPF 应用程序打开默认浏览器并转到某个网页。我该怎么做?
【问题讨论】:
对于 .NET 的桌面版本:
System.Diagnostics.Process.Start("http://www.webpage.com");
对于 .NET Core,ProcessStartInfo.UseShellExecute 的默认值已从 true 更改为 false,因此您必须将其显式设置为 true 才能正常工作:
System.Diagnostics.Process.Start(new ProcessStartInfo
{
FileName = "http://www.webpage.com",
UseShellExecute = true
});
更复杂的是,对于 UWP 应用,此属性不能设置为 true(因此这些解决方案都不适用于 UWP)。
【讨论】:
已接受的答案不再适用于 .NET Core 3。要使其工作,请使用以下方法:
var psi = new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
};
Process.Start (psi);
【讨论】:
我一直在使用这一行来启动默认浏览器:
System.Diagnostics.Process.Start("http://www.google.com");
【讨论】:
虽然给出了一个很好的答案(使用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;
}
【讨论】:
您无法从提升的应用程序启动网页。这将引发 0x800004005 异常,可能是因为 explorer.exe 和浏览器正在运行非提升。
要在非提升的 Web 浏览器中从提升的应用程序启动网页,请使用 code made by Mike Feng。我试图将 URL 传递给 lpApplicationName 但这不起作用。当我将 CreateProcessWithTokenW 与 lpApplicationName = "explorer.exe"(或 iexplore.exe)和 lpCommandLine = url 一起使用时也不会。
以下解决方法确实有效:创建一个具有一项任务的小型 EXE 项目:Process.Start(url),使用 CreateProcessWithTokenW 运行此 .EXE。在我的 Windows 8 RC 上,这可以正常工作并在 Google Chrome 中打开网页。
【讨论】:
Explorer.exe去提升:“很遗憾,Windows Shell团队已回复“Explorer.exe AppName.exe”的当前行为是一个错误并且可能无法在未来的 Windows 更新/版本中运行。应用程序不应依赖它。”
这是我的完整代码如何打开。
有两种选择:
使用默认浏览器打开(行为就像在浏览器窗口内打开)
通过默认命令选项打开(行为就像你使用“RUN.EXE”命令)
通过'explorer'打开(行为就像你在文件夹窗口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;
}
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;
}
希望我能帮上忙。
【讨论】:
老派的方式;)
public static void openit(string x) {
System.Diagnostics.Process.Start("cmd", "/C start" + " " + x);
}
使用:openit("www.google.com");
【讨论】:
由于我今天有类似的问题,我有解决方案。
假设您想从以管理员权限运行的应用打开 http://google.com:
ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe", "http://www.google.com/");
Process.Start(startInfo);
【讨论】:
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);
}
【讨论】: