I found an easier way to open the standard browser of a pc and scroll to a object with a specified ID regardless of what browser the user is using!
And the best part: No command line hacks or other stuff needed!
First we need the browser Id from the registry:
private string GetDefaultBrowserId()
{
const string userChoice = @"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice";
string progId;
//BrowserApplication browser;
using (RegistryKey userChoiceKey = Registry.CurrentUser.OpenSubKey(userChoice))
{
if (userChoiceKey == null)
{
throw new NotSupportedException("No standard browser id found in registry");
//break;
}
object progIdValue = userChoiceKey.GetValue("Progid");
if (progIdValue == null)
{
throw new NotSupportedException("No standard browser id found in registry");
//break;
}
progId = progIdValue.ToString();
return progId;
}
}
Then we look up the browser path by the browser id up in the registry:
private FileInfo GetDefaultBrowserPath(string defaultBrwoserId)
{
const string exeSuffix = ".exe";
string path = defaultBrwoserId + @"\shell\open\command";
FileInfo browserPath = null;
using (RegistryKey pathKey = Registry.ClassesRoot.OpenSubKey(path))
{
if (pathKey == null)
{
throw new NotSupportedException("Standard browser not properly registred in registry!");
//return;
}
// Trim parameters.
try
{
path = pathKey.GetValue(null).ToString().ToLower().Replace("\"", "");
if (!path.EndsWith(exeSuffix))
{
path = path.Substring(0, path.LastIndexOf(exeSuffix, StringComparison.Ordinal) + exeSuffix.Length);
browserPath = new FileInfo(path);
}
}
catch
{
// Assume the registry value is set incorrectly, or some funky browser is used which currently is unknown.
throw new NotSupportedException("Standard browser not properly registred in registry!");
}
}
return browserPath;
}
Now we have the path to the browser executable. Something like C:\Program Files\Google\Chrome\Application\chrome.exe
After that we need to convert our file path e.g C:\My Docs\Docs of important Software\Mydoc.html
to a readable format for the browser because certain characters need to be escaped in the url or replaced by others.
First we need to convert the local file path and escape characters like empty spaces " "
:
private string ConvertFilesystemStringToBrowserString(string filePath)
{
filePath = filePath.Replace("\\", "/");
filePath = Uri.EscapeUriString(filePath);
//filePath = filePath.Replace(" ", "%20");
return filePath;
}
Now we have the escaped file path e.g C:/My%20Docs/Docs%20of%20important%20Software/Mydoc.html
. We still need to convert it to a browser command and pass the ID of the html object:
private string GenerateFileUri(string uri, string elementId)
{
string localUri = $"file:///{uri}#{elementId}";
return localUri;
}
Now we have the finished command with the id e.g file:///C:/My%20Docs/Docs%20of%20important%20Software/Mydoc.html#Headline-5
, in this case Headline-5
Now we only need to call
Process.Start(defaultBrowserPath.Value, fileUri);
And the default browser opens and scrolls down to the element with the right ID!
This post helped me out to get the registry keys for the browser.