正文
1.IntPtr转换成byte[]
public byte[] ConvertToBytes(IntPtr dataBuf, int length)
{
byte[] byteBuf = new byte[length];
Marshal.Copy(dataBuf, byteBuf, 0, length);
return byteBuf;
}
2.读写 INI文件
一般用于读写配置文件
/// <summary>
/// 读写INI文件
/// </summary>
public class IniFile
{
/// <summary>
/// 文件INI名称
/// </summary>
public string Path;
/// <summary>
/// 声明读写INI文件的API函数
/// </summary>
/// <param name="section"></param>
/// <param name="key"></param>
/// <param name="val"></param>
/// <param name="filePath"></param>
/// <returns></returns>
[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);
/// <summary>
/// 类的构造函数,传递INI文件名
/// </summary>
/// <param name="inipath"></param>
public IniFile(string inipath)
{
//
// TODO: Add constructor logic here
//
Path = inipath;
}
/// <summary>
/// 写INI文件
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.Path);
}
/// <summary>
/// 读取INI文件指定
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(5000);
int i = GetPrivateProfileString(Section, Key, "", temp, 5000, this.Path);
return temp.ToString();
}
}