Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Open special section of a local HTML file in default browser

I want to open the documentation, which is stored in a single local html file, when the user clicks on certain wpf controls. Normally, the file can be navigated using HTML sections, and links like file:///C:/Users/uname/Desktop/Tool/DOCUMENTATION.html#section work (page is automatically scrolled so the section is in view) when I copy them into the address field, but when I try to open the browser by starting a new process using that same URL, the section at the end always gets removed and the page is shown from the top. This is the code I use to open the browser:

Process.Start(new ProcessStartInfo
            {
                FileName = "cmd.exe",
                Arguments = "/C start " + "file://" + AppDomain.CurrentDomain.BaseDirectory + "DOCUMENTATION.html" + "#" + section,
                WindowStyle = ProcessWindowStyle.Hidden,
                CreateNoWindow = true
            });

I first tried to open the file directly as a new process (Process.Start("path\to\file.html")) but I obviously cannot auto-navigate to sections that way.

Is there any way to achieve this "focus" on a certain section?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Back in 2010, this question was answered as follows (in VisualBasic):

    Dim BrowserRegistryString As String = My.Computer.Registry.ClassesRoot.OpenSubKey("\http\shell\open\command\").GetValue("").ToString
    Dim DefaultBrowserPath As String = System.Text.RegularExpressions.Regex.Match(BrowserRegistryString, "(\"".*?\"")").Captures(0).ToString
    Dim LocalURL As String = "file:///C:/users/matt/desktop/page.htm#test"
    Process.Start(DefaultBrowserPath, LocalURL)

Try to migrate that to C#.

The cmd shell does not pass the #test anchor tag, because the tag does not belong to the object to be started by the shell. Therefore, you probably have to explicitely start a browser. Either you encode your favorite browser, or you extract the default browser from the registry as shown in the example.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading