繁体   English   中英

使用文件中的html启动默认浏览器,然后跳转到特定锚点

[英]Launching default browser with html from file, then jump to specific anchor

我需要从程序根目录打开一个html文件,让它跳转到指定的锚点。 我可以通过简单的方式完美地打开文件

System.Diagnostics.Process.Start( “site.html”)

但是一旦我尝试将锚添加到最后,它就不再能够找到该文件了。

我能够把锚放在那里,仍然可以启动它

字符串锚

Anchor =“file:///”+ Environment.CurrentDirectory.ToString()。Replace(“\\”,“/”)+“/ site.html#Anchor”; System.Diagnostics.Process.Start(锚);

但是,只要浏览器启动,它就会删除锚点。 有什么建议?

using Microsoft.Win32;  // for registry call.

private string GetDefaultBrowserPath()
{
   string key = @"HTTP\shell\open\command";
   using(RegistryKey registrykey = Registry.ClassesRoot.OpenSubKey(key, false))
   {
      return ((string)registrykey.GetValue(null, null)).Split('"')[1];
   }
}

private void GoToAnchor(string url)
{
   System.Diagnostics.Process p = new System.Diagnostics.Process();
   p.StartInfo.FileName = GetDefaultBrowserPath();
   p.StartInfo.Arguments = url;
   p.Start();
}

// use:
GoToAnchor("file:///" + Environment.CurrentDirectory.ToString().Replace("\", "/") + "/site.html#Anchor");

您可能需要将整个URL括在引号中以保留任何特殊字符(例如#)或空格。

尝试:

string Anchor = String.Format("\"file:///{0}/site.html#Anchor\"", Environment.CurrentDirectory.ToString().Replace("\\", "/"));
System.Diagnostics.Process.Start(Anchor);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM