this seems to work OK
but I think it is easier to BinaryFormatter.Deserialize the mru.dat
file and scan it myself ... that is all ConnectionDlg.dll does anyway
string[] GetMRU_CopyDLL() {
const string regPathBinnFolder = @"SOFTWARE\Microsoft\Microsoft SQL
Server\90\Tools\ClientSetup";
const string assName = "ConnectionDlg";
const string dllName = assName + ".dll";
const string typeNamePersonalization =
"Microsoft.SqlServer.Management.UI.ConnectionDlg.Personalization";
const string typeNameSqlServerType =
"Microsoft.SqlServer.Management.UI.ConnectionDlg.SqlServerType";
try {
string folderBinn;
// open
using (RegistryKey regKeyBinnFolder =
Registry.LocalMachine.OpenSubKey(regPathBinnFolder)) {
//
if (regKeyBinnFolder == null) return null; // nothing to do
// create path to dll from registry key etc.
folderBinn = (string)regKeyBinnFolder.GetValue("Path");
}//using
// calculate paths
string folderConnDlg = Path.Combine(folderBinn, @"VSShell\Common7\IDE
\");
string pathConnDlg = Path.Combine(folderConnDlg, dllName);
// check dll exists
if (!File.Exists(pathConnDlg)) return null; // nothing to do
// create trusted path for dll
string trustedPathConnDlg = Path.Combine
(AppDomain.CurrentDomain.BaseDirectory, dllName);
// todo: worry about locks on the file from ssms etc.
File.Copy(pathConnDlg, trustedPathConnDlg, true);
//
Assembly assCurrent = Assembly.GetExecutingAssembly();
Assembly assConnDlg = Assembly.Load(assName, assCurrent.Evidence);
//
Type typePersonalization = assConnDlg.GetType
(typeNamePersonalization);
//
//Guid guidServerType = new Guid(@"8c91a03d-f9b4-46c0-a305-
b5dcc79ff907");
Guid guidServerType = (Guid)assConnDlg.GetType
(typeNameSqlServerType).GetField("ServerType").GetValue(null);
//
//BindingFlags bindFlags = BindingFlags.Public | BindingFlags.Static
| BindingFlags.InvokeMethod;
BindingFlags bindFlags = BindingFlags.InvokeMethod;
int count = (int)typePersonalization.InvokeMember
("GetCountByServerType", bindFlags, null, null, new object[]
{ guidServerType });
//
string[] engines = new string[count];
typePersonalization.InvokeMember("GetStringsByServerType",
bindFlags, null, null, new object[] { guidServerType, engines,
count });
//
return engines;
} catch (Exception exc) {
// oh well
return null;
}//try
}//method