Ini*_*eer 251
System.Diagnostics.Process.Start("http://www.webpage.com");
Run Code Online (Sandbox Code Playgroud)
许多方法之一.
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)
您无法从提升的应用程序启动网页.这将引发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中打开网页.
这是我如何打开的完整代码。
有2个选项:
使用默认浏览器打开(行为就像在浏览器窗口中打开一样)
通过默认命令选项打开(行为就像你使用“RUN.EXE”命令)
通过“资源管理器”打开(行为就像你在文件夹窗口 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)
希望我有所帮助。
| 归档时间: |
|
| 查看次数: |
211988 次 |
| 最近记录: |