我用下面的代碼來獲取我的64位代碼中的32位提供程序。使用相同的方法獲取32位代碼中的64位提供程序。我不太確定這個代碼的普遍性,但它適用於我嘗試過的每臺機器。
_dtProviders = DBCore.GetAvailableProviders(); // Col 2 is Name, Col 1 is description
List<string> providers = new List<string>();
string version = System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion();
if (Environment.Is64BitProcess)
{
foreach (DataRow item in _dtProviders.Rows)
{
providers.Add(item.ItemArray[2].ToString());
}
string fPath = "C:\\Windows\\Microsoft.NET\\Framework\\" + version + "\\Config\\machine.config";
if (File.Exists(fPath))
{
XmlDocument doc = new XmlDocument();
doc.Load(fPath);
XmlNode node = doc.SelectSingleNode("//configuration//system.data//DbProviderFactories");
foreach (XmlNode nod in node.ChildNodes)
{
var attr = nod.Attributes["invariant"].Value;
if (attr != null)
{
if (!providers.Contains(attr))
{
providers.Add(attr + " (32 Bit)");
}
}
}
}
else
{
textBox1.Text = "Warning: Could not locate machine config file load 32-Bit providers. No file located at '" + fPath + "'" + Environment.NewLine + Environment.NewLine;
}
}