在Oracle10g上測試過了,其它用戶端應該差不多的。代碼
RegistryKey hklm = Registry.LocalMachine;
RegistryKey hkSoftware = hklm.OpenSubKey("Software");
RegistryKey hkOracle = hkSoftware.OpenSubKey("Oracle");
if (hkOracle == null)
{
MessageBox.Show("未找到Oracle用戶端安裝路徑資訊。");
return;
}
string OraClient = null;
foreach (string tmp in hkOracle.GetSubKeyNames())
{
if (tmp.Contains("KEY_OraClient"))
{
OraClient = tmp;
break;
}
}
if (OraClient == null)
{
MessageBox.Show("未找到Oracle用戶端安裝路徑資訊。");
return;
}
String path = (string)hkOracle.OpenSubKey(OraClient).GetValue("ORACLE_HOME");
string tns = File.ReadAllText(Path.Combine(path, @"NETWORK\ADMIN\tnsnames.ora"));
if (!Regex.IsMatch(tns, @"(?s)km\s*=\s*\(.+\)"))
tns += @"
km =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.192.23)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = km)
)
)
";
File.WriteAllText(Path.Combine(path, @"NETWORK\ADMIN\tnsnames.ora"), tns);