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?
CodePudding user response:
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)
Migrated to C#, by Jakob Tinhofer:
string val = Registry.ClassesRoot.OpenSubKey(@"\http\shell\open\command")!.GetValue("")!.ToString()!;
pathToDefaultBrowser = val.Substring(1).Split('\"')[0];
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.
If you would prefer firefox
or chrome
, you can simply use:
cmd.exe /C start firefox file:///C:/users/matt/desktop/page.htm#test
or
cmd.exe /C start chrome file:///C:/users/matt/desktop/page.htm#test
Windows does know where to find them. But this does not work for every browser.