读写Ini文件

 public class IniFile
 {
  private System.Collections.SortedList m_SectionList = new System.Collections.SortedList();
  
  public IniFile()
  {
  }

  public IniFile(string fileName)
  {
   m_FileName = fileName;

   if( System.IO.File.Exists(fileName) )
   {
    LoadFrom(this.FileName);
   }
  }

  private string m_FileName;
  public string FileName
  {
   get
   {
    return m_FileName;
   }
   set
   {
    m_FileName = value;
   }
  }

  public void LoadFrom(string fileName)
  {
   string s1, s2, key, v;
   int index;
   string section = string.Empty;
   System.IO.StreamReader reader = new System.IO.StreamReader(fileName, System.Text.ASCIIEncoding.Default);
   try
   {
    System.Collections.SortedList thisSection = null;

    m_SectionList.Clear();

    while(reader.Peek() >= 0)
    {
     s1 = reader.ReadLine();
     s2 = s1.Trim();
     if( s2.Length < 2)
     {
      continue;
     }

     if( s2[0] == '[' && s2[s2.Length -1]==']')
     {
      //new section
      s1 = s2.Substring(1, s2.Length -2).Trim();
      if( s1.Length == 0 )
      {
       continue;
      }

      section = s1;

      thisSection = m_SectionList[section] as System.Collections.SortedList;
      if( thisSection == null )
      {
       thisSection = new System.Collections.SortedList();
       m_SectionList.Add(section, thisSection);
      }

      continue;
     }
     else
     {
      //old section
      if( section == string.Empty)
      {
       continue;
      }

      index = s1.IndexOf("=");
      if( index < 0 )
      {
       //no key or value
       continue;
      }

      key = s1.Substring(0, index).Trim();
      v = s1.Substring(index+1, s1.Length - index - 1);

      if( key.Length == 0 || v.Length == 0 )
      {
       continue;
      }

      if( thisSection[key] == null )
      {
       thisSection.Add(key, v);
      }
      else
      {
       thisSection[key] = v;
      }
     }
    }
   }
   finally
   {
    reader.Close();
   }
  }

  public void SaveTo(string fileName)
  {
   System.Collections.SortedList thisSection;
   string section, key, v;
   string sectionFmt = "[{0}]/r/n";
   string keyFmt = "{0}={1}/r/n";

   System.IO.FileStream stream = new System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
   try
   {
    System.IO.StreamWriter writer = new System.IO.StreamWriter(stream, System.Text.ASCIIEncoding.Default);
    try
    {
     for(int i=0; i<m_SectionList.Count; i++)
     {
      section = (string) m_SectionList.GetKey(i);
      writer.Write( string.Format(sectionFmt, section) );

      thisSection = m_SectionList.GetByIndex(i) as System.Collections.SortedList;
      for(int j=0; j<thisSection.Count; j++)
      {
       key = (string) thisSection.GetKey(j);
       v = (string) thisSection.GetByIndex(j);
       writer.Write( string.Format(keyFmt, key, v) );
      }
     }
    }
    finally
    {
     writer.Close();
    }
   }
   finally
   {
    stream.Close();
   }
  }

  public void Updates()
  {
   this.SaveTo(this.FileName);
  }

  public string ReadString(string section, string key)
  {
   System.Collections.SortedList thisSection = m_SectionList[section] as System.Collections.SortedList;
   if( thisSection == null )
   {
    return string.Empty;
   }
   else
   {
    object obj = thisSection[key];
    if( obj == null )
    {
     return string.Empty;
    }
    else
    {
     return (string) obj;
    }
   }
  }

  public void WriteString(string section, string key, string value)
  {
   section = section.Trim();
   key = key.Trim();
   if( section == string.Empty || key == string.Empty )
   {
    return;
   }

   System.Collections.SortedList thisSection = m_SectionList[section] as System.Collections.SortedList;
   if( thisSection == null )
   {
    thisSection = new System.Collections.SortedList();
    m_SectionList.Add(section, thisSection);
   }

   object obj = thisSection[key];
   if( obj == null )
   {
    thisSection.Add(key, value);
   }
   else
   {
    thisSection[key] = value;
   }
  }

  public void EraseSection(string section)
  {
   m_SectionList.Remove(section);
  }

  public void DeleteKey(string section, string key)
  {
   System.Collections.SortedList thisSection = m_SectionList[section] as System.Collections.SortedList;
   if( thisSection == null )
   {
    return;
   }

   thisSection.Remove(key);
  }
 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值