使用文件中的html启动默认浏览器,然后跳转到特定锚点
我需要从程序根目录打开一个html文件,让它跳转到指定的锚点。 我可以用一个简单的方法完美地打开文件
System.Diagnostics.Process.Start( “site.html”)
但是一旦我尝试将锚添加到最后,它就不再能够找到该文件了。
我能够把锚放在那里,仍然可以启动它
字符串锚
Anchor =“file:///”+ Environment.CurrentDirectory.ToString()。Replace(“\”,“/”)+“/ site.html#Anchor”; System.Diagnostics.Process.Start(锚);
但是,只要浏览器启动,它就会删除锚点。 有什么建议?
您可能需要将整个URL括在引号中以保留任何特殊字符(例如#)或空格。
尝试:
string Anchor = String.Format("\"file:///{0}/site.html#Anchor\"", Environment.CurrentDirectory.ToString().Replace("\\", "/")); System.Diagnostics.Process.Start(Anchor);
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");