我一直在使用注册表来检查是否存在使用代码的给定DSN:
private const string ODBC_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBC.INI\\";
public static bool DSNExists(string dsnName)
{
var sourcesKey = Registry
.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + "ODBC Data Sources");
if (sourcesKey == null)
throw new Exception("ODBC Registry key for sources does not exist");
string[] blah = sourcesKey.GetValueNames();
Console.WriteLine("length: " + blah.Length); //prints: 0
return sourcesKey.GetValue(dsnName) != null;
}
肯定没有0个DSN和我传入的DSN,因为参数确实存在但它返回false。我无法弄清楚为什么?
答案 0 :(得分:3)
首先,您必须想知道运行程序的机器是x86还是x64:
/// <summary>
/// Determines if the current application is 32 or 64-bit.
/// se retorno == 32 = x86 else x64
/// </summary>
public int Bits()
{
return IntPtr.Size * 8;
}
之后,设置注册表路径。为我的应用程序所做的角色我使用变量is64作为int。如果为变量赋值32,则系统为x86,否则x64为
/// <summary>
/// Construtor da classe
/// a variavel is64 vai receber um numero inteiro (32 ou 64) representando a arquitetura da maquina
/// </summary>
/// <param name="is64"></param>
public Odbc(int is64)
{
if (is64 == 32)
{
this.ODBC_INI_REG_PATH = "Software\\ODBC\\ODBC.INI\\";
this.ODBCINST_INI_REG_PATH = "Software\\ODBC\\ODBCINST.INI\\";
}
else
{
this.ODBC_INI_REG_PATH = "Software\\Wow6432Node\\ODBC\\ODBC.INI\\";
this.ODBCINST_INI_REG_PATH = "Software\\Wow6432Node\\ODBC\\ODBCINST.INI\\";
}
}
最后我修改了你放在主题上的功能。修改是:
Registry.LocalMachine.CreateSubKey =&gt; Registry.LocalMachine.OpenSubKey
public bool DSNExists(string dsnName)
{
try
{
var sourcesKey = Registry.LocalMachine.OpenSubKey(this.ODBC_INI_REG_PATH + "ODBC Data Sources");
if (sourcesKey == null)
{
throw new Exception("ODBC Registry key for sources does not exist");
}
string[] blah = sourcesKey.GetValueNames();
Console.WriteLine("length: " + blah.Length); //prints: 0
return sourcesKey.GetValue(dsnName) != null;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
答案 1 :(得分:3)
我正在寻找如何在PowerShell中执行此操作,虽然不是严格意义上对此问题的回答,但因为它确实出现在我的搜索中,我想我可以在这里留言:
if ( (Get-OdbcDsn -Name $DSN -CimSession $OnHost) -neq $null)
{
# Code here runs if the DSN exists.
}
可以添加其他参数来检查特定平台。
答案 2 :(得分:0)
DSN不是该路径的值,它们是子键。你可能想要这样的东西:
private const string ODBC_INI_REG_PATH = "SOFTWARE\\ODBC\\ODBC.INI\\";
public static bool DSNExists(string dsnName)
{
var sourcesKey = Registry
.LocalMachine.CreateSubKey(ODBC_INI_REG_PATH + "ODBC Data Sources");
if (sourcesKey == null)
throw new Exception("ODBC Registry key for sources does not exist");
string[] blah = sourcesKey.GetSubKeyNames();
Console.WriteLine("length: " + blah.Length);
var dsnKey = sourcesKey.OpenSubKey(dsnName);
bool exists = (dsnKey != null);
if (exists) dsnKey.Close();
sourcesKey.Close();
return exists;
}
请注意