Launching default browser with html from file, then jump to specific anchor
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"; Sys开发者_Python百科tem.Diagnostics.Process.Start(Anchor);
However as soon as the browser launches it drops the anchor. Any suggestions?
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");
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);
精彩评论