For my recent Launcher project, I was constructing a ribbon that would allow the user to search the web via their engines installed in Internet Explorer. The engines themselves are an XML document following the OpenSearch schema. For firefox, these xml documents just get stored locally. What does IE do with theirs?
Well, IE sticks them in the registry. Why? Maybe someone else can answer that. Here's a brief look at how to get the search engines out of the registry. The following snippets of code come from the launcher project. If you want to see how I abstracted the engines and the complete code, you can download it from Codeplex. If you want to critique the code or make suggestions on how I can code better, leave me a comment here. I'm not the best programmer and I'm starving to learn.
First off, lets give a simple answer to this post: How do I enumerate the IE search engines? The search engines are located in Software\Microsoft\Internet Explorer\SearchScopes, the rest of the task is just enumerating the keys and values to extract important information.
First off, I made a straightforward structure to represent my individual search engine:
Public Structure SearchEngine
Private m_DisplayName As String
Public Property DisplayName() As String
Get
Return m_DisplayName
End Get
Set(ByVal value As String)
m_DisplayName = value
End Set
End Property
Private m_SearchString As String
Public Property SearchString() As String
Get
Return m_SearchString
End Get
Set(ByVal value As String)
m_SearchString = value
End Set
End Property
Private m_isDefault As Boolean
Public Property isDefault() As Boolean
Get
Return m_isDefault
End Get
Set(ByVal value As Boolean)
m_isDefault = value
End Set
End Property
Public Overrides Function ToString() As String
Return m_DisplayName.ToString
End Function
End Structure
This structure just contains the display name, the search string and whether or not it is the default.
I fill a list of search engines as so:
Private Sub GatherEnginesFromRegistry()
'Clear list
m_engineList.Clear()
Dim regKey As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Internet Explorer\SearchScopes", False)
'Gather Default key
Dim defaultKey As String = regKey.GetValue("DefaultScope")
'Gather all search engines
For Each subKey As String In regKey.GetSubKeyNames
Dim engine As New SearchEngine
Dim engineKey As RegistryKey = regKey.OpenSubKey(subKey)
If engineKey.GetValue("") = String.Empty Then
engine.DisplayName = engineKey.GetValue("DisplayName")
Else
engine.DisplayName = engineKey.GetValue("")
End If
engine.SearchString = engineKey.GetValue("URL")
If (subKey = defaultKey) Then
engine.isDefault = True
Else
engine.isDefault = False
End If
m_engineList.Add(engine)
engineKey.Close()
Next
regKey.Close()
End Sub
Here you see regKey as a key to the SearchScopes subkey within CURRENT_USER. Within SearchScopes the DefaultScope value contains the subkey name for the default search engine.
After fetching the name of the default engine, I just iterate through eachsubKey and convert it into my structure, and add it to a generic list. The important values being DisplayName and URL. However, if the (Default) value for the subkey is specified, I take it instead of the DisplayName. Why? because LiveSearch does this oddity:
The DisplayName is a DLL call of sorts and the (Default) is the name? What's up with that? :\
And finally, to use the search within an application just replace the {searchTerms} in the URL with the appropriate URI formatted text and send it on its way via Process.Start!
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnSearch.Click
Process.Start(searchURL.Replace("{searchTerms}", ConvertStringToURI(searchText)))
End Sub