我只知道按照key值一个一个来读,但是我的Ini文件内容比较多,想依次全部读取,应该怎么办?

解决方案 »

  1.   

    你自己File.ReadAllLines不就拿到所有行?
    自己做又不复杂,为什么非要在Win 3.1时代的GetPrivateProfileString树上吊死?
      

  2.   

    可以用API GetPrivateProfileString先读所有section,再循环section读每个section内全部key value对
      

  3.   

    不就是参数变化下嘛
    GetPrivateProfileString(null, null, "", buff, buff.Length, filePath);取全部section
    GetPrivateProfileString(section, null, "", buff, buff.Length, filePath);取key
    GetPrivateProfileString(section, key, "", buff, buff.Length, filePath);取value
    不会递归就写几个循环,加一起也就十几二十行代码的事
      

  4.   

    foreach ( str , setting.childGroups()) {
            qDebug()<<QString::fromLocal8Bit( str.toLatin1() );
            setting.beginGroup( str );
            for( int i = 0; i < setting.allKeys().size(); i++ )
            {
                QString str1 = setting.allKeys().at(i);
                qDebug()<<i<<QString::fromLocal8Bit( str1.toLatin1() )
                       <<QString::fromLocal8Bit( setting.value( str1 ).toString().toLatin1() );
            }
            setting.endGroup();
        }
    亲测,可用
      

  5.   

    可以,读取中文ini
      

  6.   

    /// <summary>
            /// 获取配置文件中某个Key的值
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public static string GetConfigValue(string key)
            {
                try
                {
                    System.IO.FileInfo finfo = new System.IO.FileInfo(System.Reflection.Assembly.GetCallingAssembly().Location);
                    string path = finfo.DirectoryName + "\\config.ini";
                    if (!System.IO.File.Exists(path))
                    {
                        using (System.IO.File.Create(path))
                        {
                        }
                    }
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(path))
                    {
                        while (!sr.EndOfStream)
                        {
                            string line = sr.ReadLine().Trim();
                            if (line.StartsWith(key))
                            {
                                return line.Replace(key + "=", "");
                            }
                        }
                    }
                }
                catch (Exception ex)
                {            }
                return "";
            }