Starting a default browser with html from a file, then navigating to a specific anchor

I need to open the html file from the root directory of the program and go to the specified anchor. I can fully open the file with a simple

System.Diagnostics.Process.Start ("site.html")

but as soon as I try to add the anchor to the end, it stops finding the file.

I was able to anchor there and still launch it with

string Anchor

Anchor = "file: ///" + Environment.CurrentDirectory.ToString (). Replace ("\", "/") + "/site.html#Anchor"; System.Diagnostics.Process.Start (Anchor);

However, as soon as the browser launches, it drops to the anchor. Any suggestions?

+4
c #
source share
2 answers

You may need to enclose the entire URL in quotation marks to preserve any special characters (such as #) or spaces.

Try:

string Anchor = String.Format("\"file:///{0}/site.html#Anchor\"", Environment.CurrentDirectory.ToString().Replace("\\", "/")); System.Diagnostics.Process.Start(Anchor); 
+2
source share
 using Microsoft.Win32; // for registry call. private 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 void GoToAnchor(string url) { System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = GetDefaultBrowserPath(); p.StartInfo.Arguments = url; p.Start(); } // use: GoToAnchor("file:///" + Environment.CurrentDirectory.ToString().Replace("\", "/") + "/site.html#Anchor"); 
+5
source share

All Articles