阅读与GetPrivateProfileString所有ini文件值 [英] Read all ini file values with GetPrivateProfileString

查看:133
本文介绍了阅读与GetPrivateProfileString所有ini文件值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来读取一个StringBuilder变量的所有章节/ ini文件中的键:

I need a way to read all sections/keys of ini file in a StringBuilder variable:

[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);

...

private List<string> GetKeys(string iniFile, string category)
{
    StringBuilder returnString = new StringBuilder(255);            

    GetPrivateProfileString(category, null, null, returnString, 32768, iniFile);

    ...
}

在returnString只是第一密钥值!它是如何可能得到一次全部,并将其写入到StringBuilder并列出?

In returnString is only the first key value! How it is possible to get all at once and write it to the StringBuilder and to List?

感谢您的帮助!

迎接leon22

推荐答案

可能的解决方法:

[DllImport("kernel32.dll")]
private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpszReturnBuffer, int nSize, string lpFileName);

private List<string> GetKeys(string iniFile, string category)
{

    byte[] buffer = new byte[2048];

    GetPrivateProfileSection(category, buffer, 2048, iniFile);
    String[] tmp = Encoding.ASCII.GetString(buffer).Trim('\0').Split('\0');

    List<string> result = new List<string>();

    foreach (String entry in tmp)
    {
        result.Add(entry.Substring(0, entry.IndexOf("=")));
    }

    return result;
}

这篇关于阅读与GetPrivateProfileString所有ini文件值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆