<p>I have some code that uses SMO to populate a list of available SQL Servers and databases.  While we no longer support SQL Server 2000, it's possible that the code could get run on a machine that SQL Server 2000 and not have the SMO library installed.  I would perfer to check for SMO first and degrade the functionality gracefully instead of blowing up in the user's face.  What is best way to detect whether or not SMO is available on a machine?</p>

<p>Every example that I have seen through a quick Google scan was a variation of "look for C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies\Microsoft.SqlServer.Smo.dll".  The problem with that approach is that it only works with SQL Server 2005.  If SQL Server 2008 is the only SQL Server installed then the path will be different.</p>

[{'answer_id': 42577, 'author': 'Chris Miller', 'author_id': 206, 'author_profile': 'https://Stackoverflow.com/users/206', 'pm_score': 2, 'selected': False, 'text': '<p>This is kind of clunky, but a quick check of the registry seems to work.  Under HKEY_CLASSES_ROOT, a large number of classes from the SMO assemblies will be registered.  All I needed to do was to pick one of the SMO classes and check for the existence of the key with the same name.  The following function will return true if SMO has been installed, false if otherwise.</p>\n\n<pre><code>private bool CheckForSmo()\n{\n    string RegKeyName = @"Microsoft.SqlServer.Management.Smo.Database";\n    bool result = false;\n    Microsoft.Win32.RegistryKey hkcr = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(RegKeyName);\n    result = hkcr != null;\n\n    if (hkcr != null)\n    {\n        hkcr.Close();\n    }\n\n    return result;\n}\n</code></pre>\n'}, {'answer_id': 291736, 'author': 'Mostlyharmless', 'author_id': 12881, 'author_profile': 'https://Stackoverflow.com/users/12881', 'pm_score': 2, 'selected': False, 'text': '<p>What I do is just try to create an instance of some SMO object. If it fails, its not there.</p>\n'}, {'answer_id': 9847223, 'author': 'Zar Shardan', 'author_id': 913845, 'author_profile': 'https://Stackoverflow.com/users/913845', 'pm_score': 3, 'selected': False, 'text': "<p>I had a look at the SharedManagementObjects.msi from the SQL2008 R2 feature pack and my Windows Registry (SQL2008 R2 Dev is installed on this machine) and I believe these are the reg keys one should use to detect SMO (All under HKLM):</p>\n\n<p>SOFTWARE\\Microsoft\\Microsoft SQL Server\\SharedManagementObjects\\CurrentVersion - this is apparently the main key, indicating that some version of SMO is installed.</p>\n\n<p>SOFTWARE\\Microsoft\\Microsoft SQL Server 2008 Redist\\SharedManagementObjects\\1033\\CurrentVersion - this one probably means 2008 English is installed. Probably just checking for the presence of SOFTWARE\\Microsoft\\Microsoft SQL Server 2008 Redist\\SharedManagementObjects would suffice.</p>\n\n<p>Same applies to SQL2012: \nHKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SQL Server 2012 Redist\\SharedManagementObjects\\1033\\CurrentVersion</p>\n\n<p>But NOT SQL2005! even though I do have 2005 installed on this machine as well.</p>\n\n<p>One more thing, You'd normally want Microsoft SQL Server System CLR Types as well, since SMO depends on them. \nThe SQLSysClrTypes.msi has only one registry key: \nSOFTWARE\\Microsoft\\Microsoft SQL Server\\RefCount\\SQLSysClrTypes</p>\n"}, {'answer_id': 16062310, 'author': 'Manni', 'author_id': 509535, 'author_profile': 'https://Stackoverflow.com/users/509535', 'pm_score': 1, 'selected': False, 'text': '<p>Solution for SQL Server 2012:</p>\n\n<pre><code>HKLM\\SOFTWARE\\Microsoft\\Microsoft SQL Server\\SharedManagementObjects\\CurrentVersion\\Version\n</code></pre>\n\n<p>You can check if this key exists (and check if the value is greater than 11).</p>\n'}, {'answer_id': 30641990, 'author': 'Saragani', 'author_id': 1045301, 'author_profile': 'https://Stackoverflow.com/users/1045301', 'pm_score': 0, 'selected': False, 'text': "<p>Just a quick note: \nHKLM\\SOFTWARE\\Microsoft\\Microsoft SQL Server\\SharedManagementObjects\\CurrentVersion\\Version doesn't represent the current version that is installed, because there could be several versions installed.</p>\n\n<p>The registry key above is being updated when you install a version, so if you've installed SMO 2014 then you should see 12.x, but if afterwards you install SMO 2012, then this version would change to 11.x\nIf you then decides to repair the 2014 installtion, then the version would be again 12.x</p>\n\n<p>You should better look at:\nHKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SQL Server 2012 Redist\\SharedManagementObjects\\1033\\CurrentVersion</p>\n\n<p>or HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SQL Server 2014 Redist\\SharedManagementObjects\\1033\\CurrentVersion</p>\n\n<p>Does someone knows if the 1033 is guaranteed?\n(Meaning only english version)</p>\n"}]
['https://Stackoverflow.com/questions/42438', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/206/']