簡體   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