zoukankan      html  css  js  c++  java
  • C#操作INI文件

    C#操作INI文件

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

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

    窗体的大致布局如下

    clip_image001

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

    clip_image002

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

    clip_image003

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

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

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

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

    clip_image004

    clip_image005clip_image006声明变量

    clip_image004[1]

    #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

    clip_image004[2]

    clip_image004[3]

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

    clip_image005[1]clip_image006[1]写入事件

    clip_image004[4]

    //写入按钮事件
    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());
    }
    }

    clip_image004[5]

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

    clip_image005[2]clip_image006[2]读取事件

    clip_image004[6]

    //读取按钮事件
    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文件不存在");
    }
    }

    clip_image004[7]

    在读取的时候用到了自定义读取函数的方法,在该方法中调用了系统函数,

    clip_image005[3]clip_image006[3]View Code

    clip_image004[8]

    }
    /// <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();
    }

    clip_image004[9]

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

  • 相关阅读:
    同步手绘板——将View的内容映射成Bitmap转图片导出
    同步手绘板——关于二维码连接
    同步手绘板——PC端实现画板
    同步手绘板——二维码验证登录
    同步手绘板——android端取色
    同步手绘板——android端下笔后颜色变化
    同步手绘板——重点难点及实现想法
    Beta版总结会议
    团队成员绩效评分
    2015/6/9 站立会议以及索引卡更新
  • 原文地址:https://www.cnblogs.com/webcyz/p/3941702.html
Copyright © 2011-2022 走看看