基本信息
源码名称:C#操作INF文件(读写文件)示例源码
源码大小:0.04M
文件格式:.rar
开发语言:C#
更新时间:2017-09-22
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

  C#操作INF文件

//窗体加载时事件
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        //读取INI文件中的内容的方法
        private string ContentValue(string Section,string key) {

            StringBuilder temp = new StringBuilder(1024);
            GetPrivateProfileString(Section, key, "", temp, 1024, strFilePath);
            return temp.ToString();
        }
        //写入按钮事件
        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());
            
            }
        }
        //读取按钮事件
        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文件不存在");
            
            }
        }