且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

你如何打开一个网页浏览器本地的HTML文件时,路径中包含一个URL片段

更新时间:2023-09-01 15:36:40

感谢所有试图帮助我解决这个问题,因为我已经找到一个可行的解决方案。我在下面贴吧,所有你需要做的就是调用包含片段的本地文件路径。干杯!


导航
 私有静态字符串GetDefaultBrowserPath()
{
字符串键= @HTTP\shell\open\command;
使用(的RegistryKey的RegistryKey = Registry.ClassesRoot.OpenSubKey(键,FALSE))
{
回报率((字符串)registrykey.GetValue(NULL,NULL))。斯普利特('')[1 ];
}
}

私有静态无效导航(字符串URL)
{
的Process.Start(GetDefaultBrowserPath()文件:/// { 0}FormatWith(URL))。
}


I am trying to open a web browser via the following methods. However, when the browser opens the url / file path, the fragment piece gets mangled (from "#anchorName" to "%23anchorName") which does not seem to get processed. So basically, the file opens but does not jump to the appropriate location in the document. Does anyone know how to open the file and have the fragment processed? Any help on this would be greatly appreciated.

an example path to open would be "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();
    }

Thanks to all that tried to help me with this issue. I have since found a solution that works. I have posted it below. All you need to do is call navigate with a local file path containing a fragment. Cheers!

    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));
    }