.NET(C#) 读取Oracle的tnsnames.ora中key和value的方法及示例代码

本文主要介绍.NET(C#)中,读取tnsnames.ora配置文件中key和value的方法,以及相关的示例代码。

1、读取tnsnames.ora的配置中的key

1)通过Split方法

public static List<string> ReadTextFile(string FP)
{
    string inputString;
    List<string> List = new List<string>();
    try
    {
        StreamReader streamReader = File.OpenText(FP.Trim()); // FP is the filepath of TNS file
        inputString = streamReader.ReadToEnd();
        string[] temp = inputString.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        for (int i = 0; i < temp.Length; i++)
        {
            if (temp[i].Trim(' ', '(').Contains("DESCRIPTION"))
            {
                string DS = temp[i - 1].Trim('=', ' ');
                List.Add(DS);
            }
        }
        streamReader.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return List;
}

2)通过Stack

public static string[] GetDatabases()
{
    #region 读取TNS文件
    string output = "";
    string fileLine;
    Stack parens = new Stack();
    StringBuilder stringBuilder = new StringBuilder();
    // open tnsnames.ora 
    StreamReader sr;
    try
    {
        sr = new StreamReader(@"tnsnames.ora");
    }
    catch (System.IO.FileNotFoundException ex)
    {
        throw ex;
    }
    #endregion
    // 读取文件的第一行
    fileLine = sr.ReadLine();
    string[] t = null;
    #region
    // 循环,读取每一行
    while (fileLine != null)
    {
        //  如行的第一个字符为“#”忽略这一行。直接读下一行。
        if (fileLine.Length > 0 && fileLine.Trim().Substring(0, 1) != "#")
        {
            // Read through the input line character by character 
            char lineChar;
 
            for (int i = 0; i < fileLine.Length; i++)
            {
                lineChar = fileLine[i];

                if (lineChar == '(')
                {
                    // 如果第一个字符是 "(" 整行放入 堆栈。
                    parens.Push(lineChar);
                }
                else if (lineChar == ')')
                {
                    // if the char is a ), pop the stack 如果字符是")",一个一个移出  (注:POP可在 Stack 的顶部移除一个元素)
                    parens.Pop();
                }
                else
                {
                    if (parens.Count == 0)
                    {
                        output += lineChar;
                    }
                }
            }             
        }
        fileLine = sr.ReadLine();
    }
   
    // Close the stream reader 
    sr.Close();
    #endregion

    #region 处理=号
    // 
    string[] split = output.Split('=');

    //  以"="号为分隔符。截掉,放入split内
    for (int i = 0; i < split.Length; i++)
    {
        split[i] = split[i].Trim();
    }

    Array.Sort(split);

    return split;
    #endregion
}

2、读取tnsnames.ora的配置中的key和value

public static Dictionary<string, string> GetDatabases()
{
    #region 读取TNS文件
    string output = "";
    string fileLine;
    Stack parens = new Stack();
    Dictionary<string, string> keyValues = new Dictionary<string, string>();
    StringBuilder stringBuilder = new StringBuilder();
    // open tnsnames.ora 
    StreamReader sr;
    try
    {
        sr = new StreamReader(@"tnsnames.ora");
    }
    catch (System.IO.FileNotFoundException ex)
    {
        throw ex;
    }
    #endregion
    // 读取文件的第一行
    fileLine = sr.ReadLine();
    string[] t = null;
    #region
    // 循环,读取每一行
    while (fileLine != null)
    {
        // if the first non whitespace character is a #, ignore the line 
        // and go to the next line in the file 如行的第一个字符为“#”忽略这一行。直接读下一行。
        if (fileLine.Length > 0 && fileLine.Trim().Substring(0, 1) != "#")
        {
            // Read through the input line character by character 
            char lineChar;
 
            for (int i = 0; i < fileLine.Length; i++)
            {
                lineChar = fileLine[i];

                if (lineChar == '(')
                {
                    // //如果第一个字符是 "(" 整行放入 堆栈。
                    parens.Push(lineChar);
                }
                else if (lineChar == ')')
                {
                    // 如果字符是")",一个一个移出  (注:POP可在 Stack 的顶部移除一个元素)
                    parens.Pop();
                }
                else
                {
                    if (parens.Count == 0)
                    {
                        
                        if (stringBuilder.Length > 0)
                        {
                            t = output.Split('=');
                            keyValues.Add((t.Length>2?t[t.Length-2]:t[0]).Trim(), stringBuilder.ToString());
                            stringBuilder.Clear();
                        }
                        output += lineChar;
                    }
                }
            }
            if (fileLine.IndexOf("(") > -1 || fileLine.IndexOf(")") > -1)
            {
                stringBuilder.Append(fileLine);
            }
        }
        fileLine = sr.ReadLine();
    }
   
    // Close the stream reader 
    sr.Close();
    return keyValues;
    #endregion
}
推荐阅读
cjavapy编程之路首页