הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Wednesday, July 28, 2010 6:52 PM
Hello,
I want to open a local html file in the user's default Web browser, and jump to an anchor within that file. In other words, I would like to have a result similar to clicking a link to localfile.html#my_anchor.
I've been using the System.Diagnostics.Process.Start(String) method to open html files, and this works fine. In the following example, I open a file named "info.html" in the "SomeInfo" subfolder of the folder containing the application:
Dim
ProcID As
Process
Dim
DirPath As
String
= "SomeInfo\"
Dim
FileName As
String
= "info.html"
Try
ProcID = Process.Start(DirPath & FileName)
Catch
ex As
Exception
MsgBox("Problem opening file."
, MsgBoxStyle.Exclamation)
End
Try
Unless the folder or filename specified doesn't exist, the default browser opens a new window and loads the HTML file.
However, this does not allow for the addition of the anchor (e.g., info.html#GoHere), since the system interprets the anchor name as part of the file name. I don't want to use one of the other Start() methods to specify the name of the browser executable, since I just want the system to use the default browser.
Is there any way to achieve this?
All replies (8)
Thursday, July 29, 2010 3:27 PM ✅Answered | 1 vote
You can look in the registry for the program that is to handle the HTTP protocol, which of course should be a webbrowser.
Normally (at least with IE8 and Firefox 3) the value stored there is the full path in quotes, plus any command line params (both IE and firefox have additional params that are passed). So you should take that entire string, just grab the full path, and then use that to launch, along with your local URL as the param.
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)
I would also employ some error handling of course ;)Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com
Wednesday, July 28, 2010 9:11 PM
Determine which browser is the default browser, and then call that exe specifically, passing your URL with the anchor as an argument. It works when you do it that way, it doesn't when you just try to process.start() on the URL.
So if your concern is you don't want to just assume IE is the users browser, there are ways to check what browser is the default browser.
Or if you just need to display these documents, you might want to consider just using a webbrowser control inside the application directly.
Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com
Thursday, July 29, 2010 3:31 AM
Matt,
Thanks for the reply. You're right, I don't want to make any assumptions regarding which browser is the user's default, so your suggested solution is great. What is the way to check what browser is the default browser?
thanks!
--Dave
Thursday, July 29, 2010 3:30 PM
Cool, I'll check into it. Thanks very much for your help, Matt.
best,
--Dave
Thursday, July 29, 2010 3:54 PM
No problem. For the record, I tested with IE and Firefox set as default browsers, and the code worked on both.Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com
Thursday, February 23, 2012 4:22 PM
Hallo Kleinma,
Almost after 3 year, your solution is helpful for others like me. It is working in IE but not for firefox and convert the string:
"file:///C:/users/matt/desktop/page.htm%23test"
when i tried to run on browser, its working. would you please what can be the reason.
thanks
Janjua
Monday, July 23, 2012 6:04 PM
Thanx for the hint. The Web browser component was really a decent solution. What I needed the browser for, was just to show the user's manual (that was written with some html wysiwyg editor) and move to a specific page (target). With Visual Basic it was like this:
From main window I call the the browser form like this (DocName is the help file name and path, TargetName is the anchor in the html document):
frmBrowser.ShowURL(DocName & "#" & TargetName)
And in the browser form(frmBrowser) I have a method written as follows (wbrBrowser is the browser component):
Public Sub ShowURL(ByVal sURL As String)
Try
Me.Text = sURL
Me.wbrBrowser.Url = New System.Uri(sURL)
Me.Show()
If Me.WindowState = FormWindowState.Minimized Then
Me.WindowState = FormWindowState.Normal
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Monday, July 23, 2012 7:27 PM
Almost after 3 year, your solution is helpful for others like me. It is working in IE but not for firefox and convert the string:
"file:///C:/users/matt/desktop/page.htm%23test"
when i tried to run on browser, its working. would you please what can be the reason.
Dim s As String = "file:///D:/tmp/test.html%23test"
s = uri.UnescapeDataString(s)
Process.Start(s)
Oh, I see this was not the new reply in the thread. Anyway, if it helps...
Armin