How to open html file with anchor(#) in C# with default browser

青春壹個敷衍的年華 提交于 2019-12-07 22:32:49

问题


I am trying to open a contextual help file in c#.

When i specify no anchor, it works perfectly.

Process.Start("C:/Help/Help.htm")

But when i specify anchor, it does not open

Process.Start("C:/Help/Help.htm#_Toc342057538")

Internally it changes '#' to '%23' and the path becomes "c:\Help.htm%23_Toc342057538" which browser is unable to recognize.

Browser is successfully opening the path "c:\Help.htm#_Toc342057538"

How to stop this automatic conversion by Process.Start. The same behavior is observed, if i give the anchor label as another argument, or use Uri class.

EDIT Same behavior is observed, when i enter the string in Window Run. Following command also convert # to %23, which browser cannot recognize.

chrome c:/Help.htm#_Toc342057538

回答1:


On my Windows 7 system, both of the following open C:\Help\Help.htm in Internet Explorer and scroll to the _Toc342057538 anchor:

Process.Start("iexplore", "file://C:/Help/Help.htm#_Toc342057538");
Process.Start("iexplore", @"C:\Help\Help.htm#_Toc342057538");

For Firefox and Chrome, only the file protocol seems to work:

Process.Start("firefox", "file://C:/Help/Help.htm#_Toc342057538");
Process.Start("chrome", "file://C:/Help/Help.htm#_Toc342057538");



回答2:


Try this out. I just did it myself and working in internet explorer

string s = "file:///D:/tmp/test.html%23test";
      s = uri.UnescapeDataString(s);

      Process.Start(s);

Please let me know if it is working or not for you.



来源:https://stackoverflow.com/questions/13852867/how-to-open-html-file-with-anchor-in-c-sharp-with-default-browser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!