Sco*_*ock 14 c# browser url process
我有一个URL,我想在默认浏览器中启动它.我试过两种方法:
Process.Start("http://stackoverflow.com");
Run Code Online (Sandbox Code Playgroud)
...以及使用ShellExecute 在另一个问题中详述的那个.
在这两种情况下我都会收到错误:Windows无法找到" http://stackoverflow.com ".确保正确键入名称,然后重试.
它不应该试图将其作为文件打开...虽然...从我的理解,它应该将其识别为URL并在默认浏览器中打开它.我错过了什么?
顺便说一下:OS = Vista,.NET = 3.5
编辑:
根据这篇MS知识库文章,由于Process.Start默认设置UseShellExecute,它应该启动默认浏览器.
编辑:
这是有用的:
System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\IExplore.exe", "http://stackoverflow.com");
Run Code Online (Sandbox Code Playgroud)
不幸的是,它确实没有启动默认浏览器,如果没有在"正常"位置安装IE,它也无法正常工作.我不知道该怎么做.
更多信息:
好的,所以我得到的错误是错误号-2147467259.看看谷歌,它似乎不是很具描述性.它可能是文件关联错误或其他什么.
情节变浓:
所以我检查了应该具有http的文件关联的注册表项:
KEY_CLASSES_ROOT\http\shell\open\command\default
Run Code Online (Sandbox Code Playgroud)
这是价值:
"C:\Program Files\Mozilla Firefox\firefox.exe" -requestPending -osint -url "%1"
Run Code Online (Sandbox Code Playgroud)
那讲得通.我实际上将此字符串复制到命令提示符中并将%1替换为http://stackoverflow.com并且它工作并打开了firefox.我只是不明白为什么Process.Start没有将URL与此命令关联...
maf*_*afu 10
这对我有用:
Process proc = new Process ();
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = "http://stackoverflow.com";
proc.Start ();
Run Code Online (Sandbox Code Playgroud)
如果要使用命令类型的自动识别(在本例中为http/browser),请不要忘记UseShellExecute.
编辑:如果你Win+R的网址是否有用?
好吧,它神秘地开始正常工作,没有做任何改变。我无法解释。然而,与此同时,我编写了另一种查找并执行默认浏览器的方法。这有点 hacky,但比默认加载 IE 好得多:
bool success = false;
RegistryKey httpKey = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command");
if (httpKey != null && httpKey.GetValue(string.Empty) != null)
{
string cmd = httpKey.GetValue(string.Empty) as string;
if (cmd != null)
{
try
{
if (cmd.Length > 0)
{
string[] splitStr;
string fileName;
string args;
if (cmd.Substring(0,1) == "\"")
{
splitStr = cmd.Split(new string[] { "\" " }, StringSplitOptions.None);
fileName = splitStr[0] + "\"";
args = cmd.Substring(splitStr[0].Length + 2);
}
else
{
splitStr = cmd.Split(new string[] { " " }, StringSplitOptions.None);
fileName = splitStr[0];
args = cmd.Substring(splitStr[0].Length + 1);
}
System.Diagnostics.Process.Start(fileName, args.Replace("%1","http://stackoverflow.com"));
success = true;
}
}
catch (Exception)
{
success = false;
}
}
httpKey.Close();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15324 次 |
| 最近记录: |