编程环境:VS.NET 2003
语言:C#
目的:操作INI文件
/* INI文件是文本文件,由若干节(section)组成,在每个带方括号的标题下面,
* 是若干个关键词(key)及其对应的值(Value)
* [Section]
* Key=Value
*/
************************************************
******Class Code:*********
************************************************
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace it_clientupd.lib
{
/// <summary>
/// ReadIniFile 的摘要说明。
/// Created by Jason
/// Date:2006-10-26
/// Create a new ini file to store
/// or load data from a existing ini file
/// </summary>
public class ReadIniFile
{
private string ls_iniFileFullPath;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,string key, string val, string filePath);
/// or load data from a existing ini file
/// </summary>
public class ReadIniFile
{
private string ls_iniFileFullPath;
[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);
private static extern int GetPrivateProfileString(string section,string key, string def, StringBuilder retVal,int size, string filePath);
public ReadIniFile()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void IniFile(string ls_inifile_path)
{
/// <summary>
/// the full path of the ini file
/// </summary>
this.ls_iniFileFullPath = ls_inifile_path;
}
{
/// <summary>
/// the full path of the ini file
/// </summary>
this.ls_iniFileFullPath = ls_inifile_path;
}
/// <summary>
/// Write data to the ini file
/// <PARAM name="iniSection">the value of iniSection</PARAM>
/// <PARAM name="iniKey">the value of iniKey</PARAM>
/// <PARAM name="iniValue">the value of iniValue</PARAM>
/// </summary>
public void Write(string iniSection, string iniKey, string iniValue)
{
WritePrivateProfileString(iniSection, iniKey, iniValue, this.ls_iniFileFullPath);
}
/// Write data to the ini file
/// <PARAM name="iniSection">the value of iniSection</PARAM>
/// <PARAM name="iniKey">the value of iniKey</PARAM>
/// <PARAM name="iniValue">the value of iniValue</PARAM>
/// </summary>
public void Write(string iniSection, string iniKey, string iniValue)
{
WritePrivateProfileString(iniSection, iniKey, iniValue, this.ls_iniFileFullPath);
}
/// <summary>
/// Read data value From the Ini File
/// <PARAM name="iniSection"></PARAM>
/// <PARAM name="iniKey"></PARAM>
/// <returns>the iniValue of the specified iniSection & iniKey</returns>
/// </summary>
public string Read(string iniSection, string iniKey)
{
StringBuilder resultValue = new StringBuilder(255);
int i = GetPrivateProfileString(iniSection, iniKey, "", resultValue,255, this.ls_iniFileFullPath);
return resultValue.ToString();
}
}
}
.................................................................................................................
/// Read data value From the Ini File
/// <PARAM name="iniSection"></PARAM>
/// <PARAM name="iniKey"></PARAM>
/// <returns>the iniValue of the specified iniSection & iniKey</returns>
/// </summary>
public string Read(string iniSection, string iniKey)
{
StringBuilder resultValue = new StringBuilder(255);
int i = GetPrivateProfileString(iniSection, iniKey, "", resultValue,255, this.ls_iniFileFullPath);
return resultValue.ToString();
}
}
}
.................................................................................................................
.................................................................................................................
页面中引用,实例:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using it_clientupd.lib;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using it_clientupd.lib;
namespace it_clientupd.page
{
/// <summary>
/// main 的摘要说明。
/// </summary>
public class main : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
ReadIniFile fileExample = new ReadIniFile();
fileExample.IniFile(@"C:/check_class.ini");
fileExample.Write("sectionA", "keyA", "valueA");
fileExample.Read("sectionA", "keyA");
}
{
/// <summary>
/// main 的摘要说明。
/// </summary>
public class main : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
ReadIniFile fileExample = new ReadIniFile();
fileExample.IniFile(@"C:/check_class.ini");
fileExample.Write("sectionA", "keyA", "valueA");
fileExample.Read("sectionA", "keyA");
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
#endregion
}
}
参考资料
: