IniFile 操作INI文件类 C#
IniFile 操作INI文件类 C#
扫一扫加微信
C# Code:
/// <summary>
/// 操作INI文件类
/// </summary>
public class IniFile
{
private string _path; //INI档案名
public string IniPath { get { return _path; } set { _path = value; } }
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct STRINGBUFFER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
public string szText;
}
//读写INI文件的API函数
[DllImport("kernel32", CharSet = CharSet.Auto)]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
//读写INI文件的API函数
[DllImport("kernel32", CharSet = CharSet.Auto)]
private static extern int GetPrivateProfileString(string section, string key, string def, out STRINGBUFFER retVal, int size, string filePath);
//类的构造函数,传递INI档案名
public IniFile(string INIPath)
{
_path = INIPath;
if (!File.Exists(_path)) CreateIniFile();
}
/// <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)
{
int i;
STRINGBUFFER RetVal;
i = GetPrivateProfileString(Section, Key, null, out RetVal, 1024, this._path);
string temp = RetVal.szText;
return temp.Trim();
}
/// <summary>
/// 读取INI文件指定关键字的值
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public string IniReadValue(string Section, string Key, string defaultValue)
{
int i;
STRINGBUFFER RetVal;
i = GetPrivateProfileString(Section, Key, null, out RetVal, 1024, this._path);
string temp = RetVal.szText.Trim();
return String.IsNullOrEmpty(temp) ? defaultValue : temp;
}
/// <summary>
/// 创建INI文件
/// </summary>
public void CreateIniFile()
{
StreamWriter w = File.CreateText(_path);
w.Write("");
w.Flush();
w.Close();
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
/// 操作INI文件类
/// </summary>
public class IniFile
{
private string _path; //INI档案名
public string IniPath { get { return _path; } set { _path = value; } }
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct STRINGBUFFER
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
public string szText;
}
//读写INI文件的API函数
[DllImport("kernel32", CharSet = CharSet.Auto)]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
//读写INI文件的API函数
[DllImport("kernel32", CharSet = CharSet.Auto)]
private static extern int GetPrivateProfileString(string section, string key, string def, out STRINGBUFFER retVal, int size, string filePath);
//类的构造函数,传递INI档案名
public IniFile(string INIPath)
{
_path = INIPath;
if (!File.Exists(_path)) CreateIniFile();
}
/// <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)
{
int i;
STRINGBUFFER RetVal;
i = GetPrivateProfileString(Section, Key, null, out RetVal, 1024, this._path);
string temp = RetVal.szText;
return temp.Trim();
}
/// <summary>
/// 读取INI文件指定关键字的值
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public string IniReadValue(string Section, string Key, string defaultValue)
{
int i;
STRINGBUFFER RetVal;
i = GetPrivateProfileString(Section, Key, null, out RetVal, 1024, this._path);
string temp = RetVal.szText.Trim();
return String.IsNullOrEmpty(temp) ? defaultValue : temp;
}
/// <summary>
/// 创建INI文件
/// </summary>
public void CreateIniFile()
{
StreamWriter w = File.CreateText(_path);
w.Write("");
w.Flush();
w.Close();
}
}
//来源:C/S框架网 | www.csframework.com | QQ:23404761
扫一扫加微信
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网