5

I need to open an html file from the program root directory and have it jump to a specified anchor. I can open the file perfectly fine with a simple

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

but as soon as I try to add the anchor to the end it ceases to be able to find the file.

I was able to put the anchor in there and still have it launch 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 the anchor. Any suggestions?

Holman716
  • 353
  • 1
  • 4
  • 11

2 Answers2

6
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");
JohnForDummies
  • 950
  • 4
  • 6
2

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

Try:

string Anchor = String.Format("\"file:///{0}/site.html#Anchor\"", Environment.CurrentDirectory.ToString().Replace("\\", "/"));
System.Diagnostics.Process.Start(Anchor);
JYelton
  • 35,664
  • 27
  • 132
  • 191