winfrom 操作 INI 文件

本文介绍了一个使用C#实现的INI文件读写方法,包括如何通过DllImport调用kernel32.dll中的函数来完成INI文件的数据存取操作。文中提供了完整的代码示例,并展示了如何在Windows平台上操作INI文件。
<strong><span style="font-size:18px;">(1)INI文件的名称:FileConfig.ini</span></strong>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:14px;"><strong>(2)实现代码</strong></span></span>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Runtime.InteropServices;   //加上两个引用
using System.IO;

namespace AboutIni {
    public partial class INI : Form {
        public INI() {
            InitializeComponent();
        }

        #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

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

        //读取
         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文件不存在");
                 return;

             }
         }

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

    

}
}

(3)图片


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值