C# 如何从我的应用程序打开网页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/502199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open a web page from my application?
提问by Alex Baranosky
I want to make my WPF application open the default browser and go to a certain web page. How do I do that?
我想让我的 WPF 应用程序打开默认浏览器并转到某个网页。我怎么做?
采纳答案by Inisheer
System.Diagnostics.Process.Start("http://www.webpage.com");
One of many ways.
多种方式之一。
回答by ajma
I've been using this line to launch the default browser:
我一直在使用这一行来启动默认浏览器:
System.Diagnostics.Process.Start("http://www.google.com");
回答by lvmeijer
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.
您无法从提升的应用程序启动网页。这将引发 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 浏览器中从提升的应用程序启动网页,请使用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 上,这可以正常工作并在 Google Chrome 中打开网页。
回答by mr.baby123
Here is my complete code how to open.
这是我如何打开的完整代码。
there are 2 options:
有2个选项:
open using default browser (behavior is like opened inside the browser window)
open through default command options (behavior is like you use "RUN.EXE" command)
open through 'explorer' (behavior is like you wrote url inside your folder window url)
使用默认浏览器打开(行为就像在浏览器窗口中打开一样)
通过默认命令选项打开(行为就像你使用“RUN.EXE”命令)
通过“资源管理器”打开(行为就像您在文件夹窗口网址中写入网址一样)
[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.
希望我有所帮助。
回答by mloskot
Microsoft explains it in the KB305703article on How to start the default Internet browser programmatically by using Visual C#.
Microsoft 在关于如何使用 Visual C# 以编程方式启动默认 Internet 浏览器的KB305703文章中对此进行了解释。
Don't forget to check the Troubleshooting section.
不要忘记检查故障排除部分。
回答by winsetter
I have the solution for this due to I have a similar problem today.
我有这个解决方案,因为我今天有一个类似的问题。
Supposed you want to open http://google.comfrom an app running with admin priviliges:
假设您想从以管理员权限运行的应用程序打开http://google.com:
ProcessStartInfo startInfo = new ProcessStartInfo("iexplore.exe", "http://www.google.com/");
Process.Start(startInfo);
回答by Moh.Kirkuk
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");
回答by cdiggins
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;
}
回答by Alexander Smirnov
Accepted answer no longer works on .NET Core 3. To make it work, use the following method:
接受的答案不再适用于.NET Core 3。要使其工作,请使用以下方法:
var psi = new ProcessStartInfo
{
FileName = url,
UseShellExecute = true
};
Process.Start (psi);