DWORD GetPrivateProfileSection(
LPCTSTR lpAppName,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);lpReturnedString
[out] Pointer to a buffer that receives the key name and value pairs associated with the named section. The buffer is filled with one or more null-terminated strings; the last string is followed by a second null character. lpReturnedString 应该如何映射为C#类型
如果映射为StringBuilder,只能取得第一对key value
LPCTSTR lpAppName,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);lpReturnedString
[out] Pointer to a buffer that receives the key name and value pairs associated with the named section. The buffer is filled with one or more null-terminated strings; the last string is followed by a second null character. lpReturnedString 应该如何映射为C#类型
如果映射为StringBuilder,只能取得第一对key value
解决方案 »
- 如何获得一个网页的一部分内容
- 如何保存Winform窗体关闭或退出前的大小和位置,使下次加载该窗体时同上次关闭或退出前的大小和位置一致。
- 页面中 动态内容静态化。
- 将txt文件数据导入二维数组,循环溢出?????
- C#如何通过PictureBox继承产生一个新的控件,PictureBox下方包含Label
- 求{堆和栈}的区别???????????
- 如何获取一个Model3DGroup对象(WPF)
- 不简单的正则表达式问题
- 急救 !!!!!!!!水晶报表 问题!!!!!!!
- .NET chart画出的图形怎么打印?
- 在C#中assembly和namespace到底有什么区别?
- C#中多个窗体之间的交互操作问题
EntryPoint = "GetPrivateProfileSection")]
protected internal static extern int
GetPrivateProfileSection(string lpAppName,
byte[] lpReturnedString, int nSize,
string lpFileName);
public static StringCollection GetINISection(
String filename, String section)
{
StringCollection items = new StringCollection();
byte[] buffer = new byte[32768];
int bufLen = 0;
bufLen = GetPrivateProfileSection(section, buffer,
buffer.GetUpperBound(0), filename);
if (bufLen > 0)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bufLen; i++)
{
if (buffer[i] != 0)
{
sb.Append((char)buffer[i]);
}
else
{
if (sb.Length > 0)
{
items.Add(sb.ToString());
sb = new StringBuilder();
}
}
}
}
return items;
}
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 最新版本:20070212http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html
目前使用了unsafe的方法,目的在于在sbyte与char之间转换,不知道有没有更好的方法 public class IniFile
{
private const uint StringBufferSize = 32768; public IniFile(string filePath)
{
FilePath = filePath;
Enc = Encoding.Default;
} public IniFile(string filePath, Encoding enc)
{
FilePath = filePath;
Enc = enc;
} [DllImport("kernel32.dll",
EntryPoint = "GetPrivateProfileSectionNames")]
private static extern uint GetPrivateProfileSectionNames(
sbyte[] lpszReturnBuffer, uint nSize, string lpFileName); public List<string> GetSectionNames()
{
sbyte[] lpszReturnBuffer = new sbyte[StringBufferSize];
uint len = GetPrivateProfileSectionNames(lpszReturnBuffer,
(uint)lpszReturnBuffer.Length, FilePath); List<string> sections = new List<string>();
unsafe
{
fixed (sbyte* pBuf = lpszReturnBuffer)
{
uint i = 0;
while (i < len)
{
uint start = i;
uint length = 0; while (i < len && lpszReturnBuffer[i] != 0)
{
++i;
} length = i - start;
sections.Add(new string(pBuf,
(int)start, (int)length, Enc)); ++i;
if (i < len && lpszReturnBuffer[i] == 0)
{
break;
}
}
}
} return sections;
} [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSection")]
private static extern uint GetPrivateProfileSection(string lpAppName,
sbyte[] lpReturnedString, uint nSize, string lpFileName); public List<string> GetSection(string section)
{
sbyte[] lpReturnedString = new sbyte[StringBufferSize];
uint len = GetPrivateProfileSection(section,
lpReturnedString, (uint)lpReturnedString.Length, FilePath); List<string> items = new List<string>();
unsafe
{
fixed (sbyte* pBuf = lpReturnedString)
{
uint i = 0;
while (i < len)
{
uint start = i;
uint length = 0; while (i < len && lpReturnedString[i] != 0)
{
++i;
} length = i - start;
items.Add(new string(pBuf,
(int)start, (int)length, Enc)); ++i;
if (i < len && lpReturnedString[i] == 0)
{
break;
}
}
}
} return items;
} [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
private static extern uint GetPrivateProfileString(
string lpAppName, string lpKeyName, string lpDefault,
StringBuilder lpReturnedString, uint nSize, string lpFileName); public string GetString(string section, string key, string def)
{ StringBuilder value = new StringBuilder((int)StringBufferSize);
GetPrivateProfileString(section, key,
def, value, StringBufferSize, FilePath);
return value.ToString();
} [DllImport("kernel32")]
private static extern bool WritePrivateProfileSection(
string lpAppName,
string lpString,
string lpFileName
); public bool WriteSection(string section, List<string> lstKeyValue)
{
string lpString = "";
for (int i = 0; i < lstKeyValue.Count; ++i)
{
lpString += lstKeyValue[i];
lpString += "\0";
} lpString += "\0"; return WritePrivateProfileSection(section, lpString, FilePath);
} [DllImport("kernel32", EntryPoint = "WritePrivateProfileString")]
private static extern bool WritePrivateProfileString(string lpAppName,
string lpKeyName, string lpString, string lpFileName); public bool WriteString(string section, string key, string value)
{
return WritePrivateProfileString(section, key, value, FilePath);
} private string FilePath;
private Encoding Enc;
}