当路径包含url片段时,如何在Web浏览器中打开本地html文件

我试图通过以下方法打开Web浏览器。 但是,当浏览器打开url / file路径时,片段会被破坏(从“#anchorName”到“%23anchorName”),似乎没有得到处理。 基本上,文件打开但不会跳转到文档中的适当位置。 有谁知道如何打开文件并处理片段? 任何有关这方面的帮助将不胜感激。

打开的示例路径是“c:\ MyFile.Html#middle”

// calls out to the registry to get the default browser private static string GetDefaultBrowserPath() { string key = @"HTTP\shell\open\command"; using(RegistryKey registrykey = Registry.ClassesRoot.OpenSubKey(key, false)) { return ((string)registrykey.GetValue(null, null)).Split('"')[1]; } } // creates a process and passes the url as an argument to the process private static void Navigate(string url) { Process p = new Process(); p.StartInfo.FileName = GetDefaultBrowserPath(); p.StartInfo.Arguments = url; p.Start(); } 

感谢所有试图帮助我解决这个问题的人。 我已经找到了一个有效的解决方案。 我在下面发布了它。 您需要做的就是使用包含片段的本地文件路径调用导航。 干杯!

  private static 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 static void Navigate(string url) { Process.Start(GetDefaultBrowserPath(), "file:///{0}".FormatWith(url)); } 

所有你需要的是:

 System.Diagnostics.Process.Start(url); 

我不是C#程序员,但在PHP中我会做一个urlencode。 当我在C#和urlencode上进行谷歌搜索时,它在StackOverflow上给出了这个页面… 使用C#进行url编码

尝试依靠系统正确解决问题:

  static void Main(string[] args) { Process p = new Process(); p.StartInfo.UseShellExecute = true; p.StartInfo.FileName = "http://stackoverflow.com/questions/tagged/c%23?sort=newest&pagesize=50"; p.StartInfo.Verb = "Open"; p.Start(); }