我需要一种方法来读取StringBuilder变量中的ini文件的所有部分/键:
[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,returnString,32768,iniFile); ... }
在returnString中只是第一个关键值!如何可以立即获取所有内容并将其写入StringBuilder和List?
感谢您的帮助!
迎接leon22
解决方法
可能解决方案
[DllImport("kernel32.dll")] private static extern int GetPrivateProfileSection(string lpAppName,byte[] lpszReturnBuffer,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; }