Csharp/C#教程:当路径包含url片段时,如何在Web浏览器中打开本地html文件分享


当路径包含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 = @"HTTPshellopencommand"; 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 = @"HTTPshellopencommand"; 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编码

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

上述就是C#学习教程:当路径包含url片段时,如何在Web浏览器中打开本地html文件分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

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

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1012774.html

(0)
上一篇 2021年12月30日
下一篇 2021年12月30日

精彩推荐