zoukankan      html  css  js  c++  java
  • ini文件的读取

    写了个读取
    形如

    [Database Info]
    GroupNum 
    = 3
    CharSet 
    = 3383

    [drivers]
    wave
    =mmdrv.dll
    timer
    =timer.drv
    ini配置文件读取的方法如下:
    Hashtable ReadingINI(string fileName)
    {
        Hashtable htSection 
    = new Hashtable();
        Hashtable htKey 
    = new Hashtable();
        System.Text.Encoding encode 
    = System.Text.Encoding.GetEncoding("GB2312");

        
    string fileDataLine = null;
        Match match;
        Regex regSection 
    = new Regex(@"^\[(?<key>.+)\]$");
        Regex keySection 
    = new Regex(@"^(?<key>.+)=(?<value>.+)$");

        StreamReader streamReader 
    = new StreamReader(fileName, encode);
        fileDataLine 
    = streamReader.ReadLine();

        
    while (fileDataLine != null)
        
    {

            
    if (!(fileDataLine.StartsWith("#"|| fileDataLine.StartsWith(";") ))
            
    {
                match 
    = keySection.Match(fileDataLine);
                
    if (match.Success)
                
    {
                    htKey.Add(match.Result(
    "${key}"), match.Result("${value}"));
                }

                
    else
                
    {
                    match 
    = regSection.Match(fileDataLine);
                    
    if (match.Success)
                    
    {
                        htKey 
    = new Hashtable();
                        htSection.Add(match.Result(
    "${key}"), htKey);
                    }

                }

            }


            fileDataLine 
    = streamReader.ReadLine();
        }


        streamReader.Close();
        streamReader 
    = null;

        
    return htSection;
    }

    假设用上面的代码读上上面的配置文件, 返回值名字是 htINI
    则可用如下方法取出timer对应的值
    Hashtable htSection = htINI["drivers"as Hashtable;
    ifnull != htSection)
    {
        
    return htSection["timer"];
    }

    else
    {
        
    return string.Empty;
    }
  • 相关阅读:
    Java Spring MVC框架搭建(一)
    LeetCode 239. Sliding Window Maximum(Hard)
    LeetCode 238. Product of Array Except Self
    LeetCode 237 Delete Node in a Linked List
    LeetCode 236. Lowest Common Ancestor of a Binary Tree
    LeetCode 235 Lowest Common Ancestor of a Binary Search Tree
    LeetCode 234. Palindrome Linked List
    LeetCode 232. Implement Queue using Stacks
    LeetCode 231. Power of Two
    LeetCode 230. Kth Smallest Element in a BST
  • 原文地址:https://www.cnblogs.com/sun_moon_earth/p/592339.html
Copyright © 2011-2022 走看看