您的位置:首页 > 数据库 > Oracle

C#获取本机连接的Oracle服务名

2008-10-09 17:50 281 查看
要加入的命名空间(using Microsoft.Win32;
using System.IO;using System.Collections; )

public static string[] GetOracleTnsNames()
{
try
{
// 查询注册表,获取oracle服务文件路径

RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("ORACLE");
string home = (string)key.GetValue("ORACLE_HOME");
string file = home + @"/network/ADMIN/tnsnames.ora";

// 解析文件
string line;

ArrayList arr = new ArrayList();
StreamReader sr = new StreamReader(file);
while ((line = sr.ReadLine()) != null)
{
line = line.Trim();
if (line != "")
{
char c = line[0];
if ( c>= 'A' && c<='z')
arr.Add(line.Substring(0, line.IndexOf(' ')));
}
}
sr.Close();

// 返回字符串数组
return (string[])arr.ToArray(typeof(string));
}
catch (Exception ex)
{
return null;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: