读写INI:
 首先,得到当前exe的运行路径   
  ApplicationPath   =   Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);   
  下面是操作INI文件的类   
 using System;
using System;
 using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
 using System.Text;
using System.Text;
 using System.IO;
using System.IO;

 namespace Ini
namespace Ini


 {
{

 /**////   <summary>
    /**////   <summary>   
 ///   Ini的摘要说明。
    ///   Ini的摘要说明。   
 ///   </summary>
    ///   </summary>   
 public class IniFile
    public class IniFile

 
     {
{

 public string path;
        public string path;


 写入信息文件#region   写入信息文件
       写入信息文件#region   写入信息文件
 private void WritePrivateProfileString(string ApplicationName, string KeyName, string Value, string FileName)
        private void WritePrivateProfileString(string ApplicationName, string KeyName, string Value, string FileName)

 
         {
{
 string[] iniItems = new string[0];
            string[] iniItems = new string[0];
 string iniLines = null;
            string iniLines = null;
 string iniLine = null;
            string iniLine = null;
 string iniItem = null;
            string iniItem = null;
 int i, j;
            int i, j;
 try
            try

 
             {
{
 //读取INI文件;
                //读取INI文件;   
 System.IO.StreamReader iniFile = new System.IO.StreamReader(FileName, System.Text.Encoding.Default);
                System.IO.StreamReader iniFile = new System.IO.StreamReader(FileName, System.Text.Encoding.Default);
 iniLines = iniFile.ReadToEnd();
                iniLines = iniFile.ReadToEnd();
 iniFile.Close();
                iniFile.Close();
 }
            }
 catch
            catch

 
             {
{
 }
            }

 //如果信息文件还沒有任何內容,將节点写到第一行
            //如果信息文件还沒有任何內容,將节点写到第一行   
 if (iniLines == null)
            if (iniLines == null)

 
             {
{
 StreamWriter CreatFile = File.CreateText(FileName);
                StreamWriter CreatFile = File.CreateText(FileName);
 CreatFile.Close();
                CreatFile.Close();
 StreamWriter FileWriter = new StreamWriter(FileName, true, System.Text.Encoding.Default);
                StreamWriter FileWriter = new StreamWriter(FileName, true, System.Text.Encoding.Default);
 FileWriter.WriteLine("[" + ApplicationName + "]");
                FileWriter.WriteLine("[" + ApplicationName + "]");
 FileWriter.WriteLine(KeyName + "=" + Value);
                FileWriter.WriteLine(KeyName + "=" + Value);
 FileWriter.Close();
                FileWriter.Close();
 return;
                return;
 }
            }
 else
            else

 
             {
{
 //否则的话得到已有的节点
                //否则的话得到已有的节点   
 iniItems = System.Text.RegularExpressions.Regex.Split(iniLines, "\r\n");
                iniItems = System.Text.RegularExpressions.Regex.Split(iniLines, "\r\n");
 }
            }

 //以回车符分割,得到每一行
            //以回车符分割,得到每一行   
 bool HaveParentNode = false;
            bool HaveParentNode = false;
 string IniContent = "";
            string IniContent = "";
 int IndexLength = 0;
            int IndexLength = 0;
 if (iniItems[iniItems.Length - 1] == "")
            if (iniItems[iniItems.Length - 1] == "")

 
             {
{
 IndexLength = iniItems.Length - 1;
                IndexLength = iniItems.Length - 1;
 }
            }
 else
            else

 
             {
{
 IndexLength = iniItems.Length;
                IndexLength = iniItems.Length;
 }
            }
 for (i = 0; i < IndexLength; i++)
            for (i = 0; i < IndexLength; i++)

 
             {
{
 iniItem = iniItems[i].Trim();
                iniItem = iniItems[i].Trim();
 if (iniItem[0] == '[' && iniItem[iniItem.Length - 1] == ']')
                if (iniItem[0] == '[' && iniItem[iniItem.Length - 1] == ']')

 
                 {
{
 if (IniContent == "")
                    if (IniContent == "")

 
                     {
{
 IniContent = iniItems[i].ToString();
                        IniContent = iniItems[i].ToString();
 }
                    }
 else
                    else

 
                     {
{
 IniContent += "\r\n" + iniItems[i].ToString();
                        IniContent += "\r\n" + iniItems[i].ToString();
 }
                    }
 if (iniItems[i].ToString() == "[" + ApplicationName + "]")
                    if (iniItems[i].ToString() == "[" + ApplicationName + "]")

 
                     {
{
 HaveParentNode = true;
                        HaveParentNode = true;
 bool HaveNode = false;
                        bool HaveNode = false;
 try
                        try

 
                         {
{
 //找到相对应的父节点的话,查找是否有对应的子节点
                            //找到相对应的父节点的话,查找是否有对应的子节点   
 for (j = (i + 1); j < IndexLength; j++)
                            for (j = (i + 1); j < IndexLength; j++)

 
                             {
{
 iniLine = iniItems[j].Trim();
                                iniLine = iniItems[j].Trim();
 if (iniLine[0] == '[' && iniLine[iniLine.Length - 1] == ']')
                                if (iniLine[0] == '[' && iniLine[iniLine.Length - 1] == ']')

 
                                 {
{
 if (!HaveNode)
                                    if (!HaveNode)

 
                                     {
{
 HaveNode = true;
                                        HaveNode = true;
 IniContent += "\r\n" + KeyName + "=" + Value;
                                        IniContent += "\r\n" + KeyName + "=" + Value;
 }
                                    }
 break;
                                    break;
 }
                                }

 iniLine = iniItems[j].TrimStart().Replace("   ", "");
                                iniLine = iniItems[j].TrimStart().Replace("   ", "");
 if (iniLine.Substring(0, Math.Min(KeyName.Length + 1, iniLine.Length)).ToUpper() == KeyName.ToUpper() + "=")
                                if (iniLine.Substring(0, Math.Min(KeyName.Length + 1, iniLine.Length)).ToUpper() == KeyName.ToUpper() + "=")

 
                                 {
{
 //如果找到了Key匹配
                                    //如果找到了Key匹配   
 HaveNode = true;
                                    HaveNode = true;
 IniContent += "\r\n" + KeyName + "=" + Value;
                                    IniContent += "\r\n" + KeyName + "=" + Value;
 }
                                }
 else
                                else

 
                                 {
{
 IniContent += "\r\n" + iniItems[j].ToString();
                                    IniContent += "\r\n" + iniItems[j].ToString();
 }
                                }
 }
                            }
 }
                        }
 catch (System.Exception ex)
                        catch (System.Exception ex)

 
                         {
{
 ex.ToString();
                            ex.ToString();
 }
                        }

 if (!HaveNode)
                        if (!HaveNode)

 
                         {
{
 HaveNode = true;
                            HaveNode = true;
 IniContent += "\r\n" + KeyName + "=" + Value;
                            IniContent += "\r\n" + KeyName + "=" + Value;
 }
                        }
 }
                    }
 else
                    else

 
                     {
{
 for (j = (i + 1); j < IndexLength; j++)
                        for (j = (i + 1); j < IndexLength; j++)

 
                         {
{
 iniLine = iniItems[j].Trim();
                            iniLine = iniItems[j].Trim();
 if (iniLine[0] == '[' && iniLine[iniLine.Length - 1] == ']')
                            if (iniLine[0] == '[' && iniLine[iniLine.Length - 1] == ']')

 
                             {
{
 break;
                                break;
 }
                            }
 else
                            else

 
                             {
{
 IniContent += "\r\n" + iniItems[j].ToString();
                                IniContent += "\r\n" + iniItems[j].ToString();
 }
                            }
 }
                        }
 }
                    }
 }
                }
 }
            }

 if (!HaveParentNode)
            if (!HaveParentNode)

 
             {
{
 IniContent += "\r\n[" + ApplicationName + "]";
                IniContent += "\r\n[" + ApplicationName + "]";
 IniContent += "\r\n" + KeyName + "=" + Value;
                IniContent += "\r\n" + KeyName + "=" + Value;
 }
            }

 StreamWriter ReplaceFile = File.CreateText(FileName);
            StreamWriter ReplaceFile = File.CreateText(FileName);
 ReplaceFile.Close();
            ReplaceFile.Close();
 StreamWriter ReplaceWriter = new StreamWriter(FileName, true, System.Text.Encoding.Default);
            StreamWriter ReplaceWriter = new StreamWriter(FileName, true, System.Text.Encoding.Default);
 ReplaceWriter.Write(IniContent);
            ReplaceWriter.Write(IniContent);
 ReplaceWriter.Close();
            ReplaceWriter.Close();
 }
        }
 #endregion
        #endregion


 读取信息文件#region   读取信息文件
        读取信息文件#region   读取信息文件
 private string GetPrivateProfileString(string ApplicationName, string KeyName, string Default, string FileName)
        private string GetPrivateProfileString(string ApplicationName, string KeyName, string Default, string FileName)

 
         {
{
 string[] iniItems = new string[0];
            string[] iniItems = new string[0];
 string iniLines;
            string iniLines;
 string iniLine;
            string iniLine;
 int i, j;
            int i, j;
 try
            try

 
             {
{
 //读取INI文件;
                //读取INI文件;   
 System.IO.StreamReader iniFile = new System.IO.StreamReader(FileName, System.Text.Encoding.Default);
                System.IO.StreamReader iniFile = new System.IO.StreamReader(FileName, System.Text.Encoding.Default);
 iniLines = iniFile.ReadToEnd();
                iniLines = iniFile.ReadToEnd();
 iniFile.Close();
                iniFile.Close();
 }
            }
 catch
            catch

 
             {
{
 return Default;
                return Default;
 }
            }
 //以回车符分割,得到每一行
            //以回车符分割,得到每一行   
 iniItems = System.Text.RegularExpressions.Regex.Split(iniLines, "\r\n"); ;
            iniItems = System.Text.RegularExpressions.Regex.Split(iniLines, "\r\n"); ;
 //遍历每一行
            //遍历每一行   
 for (i = 0; i < iniItems.GetLength(0); i++)
            for (i = 0; i < iniItems.GetLength(0); i++)

 
             {
{
 //找到匹配值
                //找到匹配值   
 if (iniItems[i].Trim().ToUpper() == '[' + ApplicationName.Trim().ToUpper() + ']')
                if (iniItems[i].Trim().ToUpper() == '[' + ApplicationName.Trim().ToUpper() + ']')

 
                 {
{
 //从下一行开始搜索
                    //从下一行开始搜索   
 for (j = i + 1; j < iniItems.GetLength(0); j++)
                    for (j = i + 1; j < iniItems.GetLength(0); j++)

 
                     {
{
 iniLine = iniItems[j].Trim();
                        iniLine = iniItems[j].Trim();
 if (iniLine.Length > 0)
                        if (iniLine.Length > 0)

 
                         {
{
 //如果找到了另一个段,那么就越段了,则返回默认值
                            //如果找到了另一个段,那么就越段了,则返回默认值   
 if (iniLine[0] == '[' && iniLine[iniLine.Length - 1] == ']') return Default;
                            if (iniLine[0] == '[' && iniLine[iniLine.Length - 1] == ']') return Default;
 }
                        }
 //去掉所有空格
                        //去掉所有空格   
 iniLine = iniItems[j].TrimStart().Replace("   ", "");
                        iniLine = iniItems[j].TrimStart().Replace("   ", "");
 if (iniLine.Substring(0, Math.Min(KeyName.Length + 1, iniLine.Length)).ToUpper() == KeyName.ToUpper() + "=")
                        if (iniLine.Substring(0, Math.Min(KeyName.Length + 1, iniLine.Length)).ToUpper() == KeyName.ToUpper() + "=")

 
                         {
{
 //如果找到了Key匹配
                            //如果找到了Key匹配   
 return iniItems[j].Substring(iniItems[j].IndexOf('=') + 1);
                            return iniItems[j].Substring(iniItems[j].IndexOf('=') + 1);
 }
                        }
 }
                    }
 return Default;//没有找到key匹配的,则返回默认值
                    return Default;//没有找到key匹配的,则返回默认值   
 }
                }
 }
            }
 return Default;//返回默认值
            return Default;//返回默认值   
 }
        }
 #endregion
        #endregion

 public IniFile(string INIPath)
        public IniFile(string INIPath)

 
         {
{
 path = INIPath;
            path = INIPath;
 }
        }

 public void IniWriteValue(string Section, string Key, string Value)
        public void IniWriteValue(string Section, string Key, string Value)

 
         {
{
 WritePrivateProfileString(Section, Key, Value, this.path);
            WritePrivateProfileString(Section, Key, Value, this.path);
 }
        }

 public string IniReadValue(string Section, string Key)
        public string IniReadValue(string Section, string Key)

 
         {
{
 return GetPrivateProfileString(Section, Key, "", this.path);
            return GetPrivateProfileString(Section, Key, "", this.path);
 }
        }

 }
    }
 }
}

使用方法   
  写信息文件   
  IniFile   NewIni   =   new   IniFile(ApplicationPath   +   "\\Test.ini");   
  NewIni.IniWriteValue("测试信息","测试一","1");   
    
  读取   
  IniFile   NewIni   =   new   IniFile(ApplicationPath   +   "\\Test.ini");   
  string   a   =   NewIni   .IniReadValue("测试信息","测试一");   
    
  读取结果   
  a   =   1
 备注:以上代码在HP   2490(WM5)上测试通过