Parsing tnsnames.ora in Visual C# 2008

后端 未结 3 1832
慢半拍i
慢半拍i 2020-12-11 20:26

How I parse tnsnames.ora file using Visual C# (Visual Studio 2008 Express edition) to get the tnsnames ? For instance, my tnsnames.ora file contains

ORCL =
         


        
相关标签:
3条回答
  • 2020-12-11 20:48

    together with those provided by Sathya, create a method:

    StringBuilder strTns = new StringBuilder ();
    
    foreach ( var fileLine in System.IO.File.ReadAllLines(fiTNS.FullName ) )
    {
        if ( (fileLine.Length > 0 
               && fileLine.Trim().Substring(0,1) != "#" 
              )
              && ( fileLine.Length > 0 
                    && fileLine.Trim ().Substring (0,1) != "/" 
                  )
            )
    
            {
               strTns.AppendFormat("{0}{1}", fileLine, Environment.NewLine);
            }
    }
    
    //TNSNamesValues = System.IO.File.ReadAllText (fiTNS.FullName).ToString ().Replace ("\n", "" ).Replace ("\r", "");
    String[] splitSeparator = LoadTNSNames (OracleHomeRegistryKey).ToArray ();
    String[] TNS = strTns.ToString().Split (splitSeparator, StringSplitOptions.None);
    
    0 讨论(0)
  • 2020-12-11 20:53

    First of all, you will need the syntax rules for this file.

    There is probably a hack for this, but I would personally go with a full parser, like ANTLR combined with the proper grammar (a complete list of ANTLR grammars can be found here).

    0 讨论(0)
  • 2020-12-11 20:54
    public 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)
        {
        }
    
    
        return List;
    
    }
    
    0 讨论(0)
提交回复
热议问题