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

2024-02-16

我正在尝试通过以下方法打开网络浏览器。然而,当浏览器打开 url/文件路径时,片段片段被破坏(从“#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));
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐