日期:2014-05-18 浏览次数:21326 次
public class IniFile
        {
            public string Path;
            [DllImport("kernel32")]
            private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
            [DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
            public IniFile(string inipath)
            {
                Path = inipath;
            }
            public void IniWriteValue(string Section, string Key, string Value)
            {
                WritePrivateProfileString(Section, Key, Value, this.Path);
            }
            public string IniReadValue(string Section, string Key)
            {
                StringBuilder temp = new StringBuilder(255);
                int i = GetPrivateProfileString(Section, Key, "无法读取对应数值!", temp, 255, this.Path);
                return temp.ToString();
            }
            public bool ExistINIFile()
            {
                return File.Exists(this.Path);
            }
        }
 
StreamReader sr = null;
try
{
                bool section = false;
                sr = new StreamReader(@"..\yourFile.ini", System.Text.Encoding.Default);
                while (sr.Peek() > -1)//从文件中读取行,一直读到文件尾
                  {
                   string rl = sr.ReadLine();
                   if(rl == "[Section]")section = true;
                   if(!section) continue;
                   if(rl.IndexOf('[')== 0) break;
                   listbox1.Items.Add(rl);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if(sr != null) sr.Close();
            }
------解决方案--------------------
如果符合ini文件格式,
Read/Write XML files, Config files, INI files, or the Registry
By Alvaro Mendez
http://www.codeproject.com/KB/cs/readwritexmlini.aspx