首页 > 编程 > .NET > 正文

ASP.NET技巧:获取本机上配置好的Oracle服务名

2024-07-10 13:09:43
字体:
来源:转载
供稿:网友
注册会员,创建你的web开发资料库,

        /*
        获取oracle服务名
        1。查询注册表,获取oracle安装根目录
            如:hkey_local_machine/software/oracle/oracle_home   reg_sz  e:/oracle/ora92
            得知oracle服务名文件所在地:根目录/network/admin/tnsnames.ora
           
        2。解析该文件,该文件结构如
            # ------------------------------------------------
            portal =
            (description =
                (address_list =
                (address = (protocol = tcp)(host = 134.104.52.6)(port = 1521))
                )
                (connect_data =
                (service_name = portal)
                )
            )

        3。解析要点:
            一行一行获取再解析
            跳过以#打头的注释行
            过滤掉空格后,首字符在a~z之间的行就包含了oracle服务名
            截取该行第一个“=”号左边的字符串,trim处理后就是oracle服务名了
        */
        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;
            }
        }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表