| 项目搜索 |
| |
|
代码片段库:
查看代码片段
浏览
| 提交新的代码片段
| 创建代码包
ini文件读写类
|
类型:
Class |
类别:
File Management
|
许可证:
GNU General Public License |
语言:
C#
|
描述:
ini文件读写类
|
该代码片段的版本系列:
片段ID |
下载版本 |
提交时间 |
提交人 |
删除 |
4871 | 1.0 | 2010-02-07 13:19 | guocong89 | |
点击"下载版本"来下载该代码片段.
最新版本的代码片段: 1.0
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace DaBaoKu
{
/// <summary>
/// 读写ini配置文件类
/// </summary>
class IniFile
{
#region 成员变量
public string filePath = ""; //文件路径
static private uint MaxBufferSize = 32767;//缓存大小
#endregion
#region 导入API
[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);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, IntPtr retVal, int size, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileInt(string section, string key, int def, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileSection(string section,IntPtr lpReturnedString,uint nSize,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer,uint nSize,string filepath);
#endregion
#region 构造函数
/// <summary>
/// 构造函数.
/// </summary>
/// <PARAM name="INIPath"></PARAM>
public IniFile(string filename)
{
filePath = System.IO.Path.GetFullPath(filename);
}
#endregion
#region 读取数据
public string GetString(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.filePath);
return temp.ToString();
}
public int GetInt(string Section, string Key)
{
return GetPrivateProfileInt(Section, Key, 0, this.filePath);
}
public List<KeyValuePair<string,string>> GetValueSetList(string Section) //读取一段内的所有数据
{
List<KeyValuePair<string, string>> retval;
string[] keyValuePairs;
string key, value;
int equalSignPos;
//申请空间
IntPtr ptr = Marshal.AllocCoTaskMem((int)IniFile.MaxBufferSize);
try
{
int len = GetPrivateProfileSection(Section,ptr,MaxBufferSize,filePath);
keyValuePairs = ConvertNullSeperatedStringToStringArray(ptr, len);
}
finally
{
Marshal.FreeCoTaskMem(ptr);
}
//输出结果
retval = new List<KeyValuePair<string, string>>(keyValuePairs.Length);
for (int i = 0; i < keyValuePairs.Length; ++i)
{
equalSignPos = keyValuePairs[i].IndexOf('=');
key = keyValuePairs[i].Substring(0, equalSignPos);
value = keyValuePairs[i].Substring(equalSignPos + 1,keyValuePairs[i].Length - equalSignPos - 1);
retval.Add(new KeyValuePair<string, string>(key, value));
}
return retval;
}
public string[] GetSectionNames() //获得所有段名
{
string[] retval;
int len;
IntPtr ptr = Marshal.AllocCoTaskMem((int)IniFile.MaxBufferSize);
try
{
len = GetPrivateProfileSectionNames(ptr,IniFile.MaxBufferSize, filePath);
retval = ConvertNullSeperatedStringToStringArray(ptr, len);
}
finally
{
Marshal.FreeCoTaskMem(ptr);
}
return retval;
}
public string[] GetKeyNames(string Section) //获得一段内所有关键码
{
int len;
string[] retval;
IntPtr ptr = Marshal.AllocCoTaskMem((int)IniFile.MaxBufferSize);
try
{
len = GetPrivateProfileString(Section, null, null, ptr, (int)IniFile.MaxBufferSize, filePath);
retval = ConvertNullSeperatedStringToStringArray(ptr, len);
}
finally
{
Marshal.FreeCoTaskMem(ptr);
}
return retval;
}
private static string[] ConvertNullSeperatedStringToStringArray(IntPtr ptr, int valLength) //转换字符串指针空间为字符串数组
{
string[] retval;
if (valLength == 0)
{
retval = new string[0];
}
else
{
string buff = Marshal.PtrToStringAuto(ptr, valLength - 1);
retval = buff.Split('
如果您修改了一个代码片段并且觉得很应该让别人共享,您可以把这作为这个代码片段的最新版本提交上来. |
|