C#操作INI文件

在很多的程序中,我们都会看到有以.ini为后缀名的文件,这个文件可以很方便的对程序配置的一些信息进行设置和读取,比如说我们在做一个程序后台登陆的时候,需要自动登录或者是远程配置数据库连接,及保存密码设置等等(在Winform程序中),若在ASP.NET程序中有另外的解决方法,此C#操作INI文件的文章仅在winform程序中进行写入和读取操作。

为了方便起见,现在以一个简单的小实例来对C#操作INI文件进行讲解:

窗体的大致布局如下

当点击写入按钮的时候就会把文本框中输入的值写入到INI文件中,结果会如图所示

当点击读取按钮的时候就会把INI文件中的节点信息的值填充到窗体中的文本框中

以上就是用C#操作INI文件的整个流程,现在来介绍后台代码是怎样实现的:

在项目名称空间的上方要添加以下的引用:

using System.Runtime.InteropServices;//引用命名空间

然后再程序的后台声明一些系统函数的变量,代码如下

声明变量
#region "声明变量"

/// <summary>
/// 写入INI文件
/// </summary>
/// <param name="section">节点名称[如[TypeName]]</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);
/// <summary>
/// 读取INI文件
/// </summary>
/// <param name="section">节点名称</param>
/// <param name="key"></param>
/// <param name="def"></param>
/// <param name="retval">stringbulider对象</param>
/// <param name="size">字节大小</param>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retval,int size,string filePath);

private string strFilePath = Application.StartupPath + "\\FileConfig.ini";//获取INI文件路径
private string strSec =""; //INI文件名

#endregion

先说明下我的INI配置文件是放在程序的Debug文件夹下的,然后单击写入按钮,在写入前没有进行写入数值的验证,代码如下:

写入事件
//写入按钮事件
private void btnWrite_Click(object sender, EventArgs e)
{
try
{

//根据INI文件名设置要写入INI文件的节点名称
//此处的节点名称完全可以根据实际需要进行配置
strSec = Path.GetFileNameWithoutExtension(strFilePath);
WritePrivateProfileString(strSec,
"Name", txtName.Text.Trim(), strFilePath);
WritePrivateProfileString(strSec,
"Sex", txtSex.Text.Trim(), strFilePath);
WritePrivateProfileString(strSec,
"Age", txtAge.Text.Trim(), strFilePath);
WritePrivateProfileString(strSec,
"Address", txtAddress.Text.Trim(), strFilePath);
MessageBox.Show(
"写入成功");

}
catch(Exception ex){
MessageBox.Show(ex.Message.ToString());

}
}

此时运行此实例就会把数值写入到INI文件中,写入的结果就像第二个截图效果显示的那样。然后我们在单击读取按钮事件,把INI文件中的信息填充到窗体的文本框中,代码如下:

读取事件
//读取按钮事件
private void btnRead_Click(object sender, EventArgs e)
{
if (File.Exists(strFilePath))//读取时先要判读INI文件是否存在
{

strSec
= Path.GetFileNameWithoutExtension(strFilePath);
txtName.Text
= ContentValue(strSec, "Name");
txtSex.Text
= ContentValue(strSec, "Sex");
txtAge.Text
= ContentValue(strSec, "Age");
txtAddress.Text
= ContentValue(strSec, "Address");

}
else {

MessageBox.Show(
"INI文件不存在");

}
}
在读取的时候用到了自定义读取函数的方法,在该方法中调用了系统函数,
View Code
}
/// <summary>
/// 自定义读取INI文件中的内容方法
/// </summary>
/// <param name="Section"></param>
/// <param name="key"></param>
/// <returns></returns>
private string ContentValue(string Section,string key) {

StringBuilder temp
= new StringBuilder(1024);
GetPrivateProfileString(Section, key,
"", temp, 1024, strFilePath);
return temp.ToString();
}

以上所述的就是简单的用C#语言操作INI文件的过程,只用到了系统函数中的两个(写入函数和读取函数)还有其他的函数比如说时删除INI文件函数等等,删除INI文件函数其实就是把键对应的值设置为null就可以了。

自动登录和连接设置都用到了INI文件,文章到此结束。

posted @ 2011-04-25 18:29  亿典通柄棋  阅读(35278)  评论(6编辑  收藏  举报