Is there a way to get a list of ODBC drivers that are installed on a Windows XP machine from .NET?
I basically would like to see (in .NET) what is in:
Control Panel->Administrative Tools->Data Sources (ODBC)->"Drivers" Tab.
Open the Windows Control Panel. Open the Administrative Tools folder. Double-click Data Sources (ODBC) to open the ODBC Data Source Administrator window. Click the Drivers tab and locate the SQL Server entry in the list of ODBC drivers to confirm that the driver is installed on your system.
The ODBC driver must be installed locally. The . dsn file is a text file that you can view in any text editor, such as Microsoft Notepad. The File DSNs are stored by default in the following location:Program Files\Common Files\Odbc\Data Sources folder.
To open the ODBC Data Source Administrator in Windows 7On the Start menu, click Control Panel. In Control Panel, click Administrative Tools. In Administrative Tools, click Data Sources (ODBC).
It is not necessary to open every intermediate subkey. Reading the registry key to get the ODBC driver names can be done in a much more compact fashion as follows:
/// <summary>
/// Gets the ODBC driver names from the registry.
/// </summary>
/// <returns>a string array containing the ODBC driver names, if the registry key is present; null, otherwise.</returns>
public static string[] GetOdbcDriverNames()
{
string[] odbcDriverNames = null;
using (RegistryKey localMachineHive = Registry.LocalMachine)
using (RegistryKey odbcDriversKey = localMachineHive.OpenSubKey(@"SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers"))
{
if (odbcDriversKey != null)
{
odbcDriverNames = odbcDriversKey.GetValueNames();
}
}
return odbcDriverNames;
}
You can also implement the function by doing a P/Invoke to SQLGetInstalledDriversW:
[DllImport("odbccp32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool SQLGetInstalledDriversW(char[] lpszBuf, ushort cbufMax, out ushort pcbBufOut);
/// <summary>
/// Gets the ODBC driver names from the SQLGetInstalledDrivers function.
/// </summary>
/// <returns>a string array containing the ODBC driver names, if the call to SQLGetInstalledDrivers was successfull; null, otherwise.</returns>
public static string[] GetOdbcDriverNames()
{
string[] odbcDriverNames = null;
char[] driverNamesBuffer = new char[ushort.MaxValue];
ushort size;
bool succeeded = SQLGetInstalledDriversW(driverNamesBuffer, ushort.MaxValue, out size);
if (succeeded == true)
{
char[] driverNames = new char[size - 1];
Array.Copy(driverNamesBuffer, driverNames, size - 1);
odbcDriverNames = (new string(driverNames)).Split('\0');
}
return odbcDriverNames;
}
I also call the function and use the results as follows to gracefully degrade to prior versions of the SQL driver when creating ODBC data sources:
/// <summary>
/// Gets the name of an ODBC driver for Microsoft SQL Server giving preference to the most recent one.
/// </summary>
/// <returns>the name of an ODBC driver for Microsoft SQL Server, if one is present; null, otherwise.</returns>
public static string GetOdbcSqlDriverName()
{
List<string> driverPrecedence = new List<string>() { "SQL Server Native Client 11.0", "SQL Server Native Client 10.0", "SQL Server Native Client 9.0", "SQL Server" };
string[] availableOdbcDrivers = GetOdbcDriverNames();
string driverName = null;
if (availableOdbcDrivers != null)
{
driverName = driverPrecedence.Intersect(availableOdbcDrivers).FirstOrDefault();
}
return driverName;
}
See this or this
Basically system stores the ODBC Driver's information here
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers
You can use this or similar code to find out the installed ODBC Drivers. This code basically reads the drivers info from registry
public static List<String> GetSystemDriverList()
{
List<string> names = new List<string>();
// get system dsn's
Microsoft.Win32.RegistryKey reg = (Microsoft.Win32.Registry.LocalMachine).OpenSubKey("Software");
if (reg != null)
{
reg = reg.OpenSubKey("ODBC");
if (reg != null)
{
reg = reg.OpenSubKey("ODBCINST.INI");
if (reg != null)
{
reg = reg.OpenSubKey("ODBC Drivers");
if (reg != null)
{
// Get all DSN entries defined in DSN_LOC_IN_REGISTRY.
foreach (string sName in reg.GetValueNames())
{
names.Add(sName);
}
}
try
{
reg.Close();
}
catch { /* ignore this exception if we couldn't close */ }
}
}
}
return names;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With