C# ini文件读写实例

ini文件一般用于保存当前运行的程序或者一些临时的配置属性的文件。也有时用于保存一定的数据以便于临时或者配置上的需要。

文本格式如下:

 

[Section1 Name] ---------用 []括起来,其包含多个key
KeyName1=value1 ------格式是 Key=value。
KeyName2=value2  
... 

[Section2 Name] 
KeyName1=value1 
KeyName2=value2
其中有专门读写ini文件的windows方法:
 [DllImport("kernel32")]
// 写入ini文件操作section,key,value
 private static extern long WritePrivateProfileString(string section,string key, string val, string filePath);
 [DllImport("kernel32")]
// 读取ini文件操作section,key,和返回的keyvalue
 private static extern int GetPrivateProfileString(string section,string key, string def, StringBuilder retVal,int size, string filePath);
C#操作时代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Runtime.InteropServices;
//using System.Collections;
//using System.Collections.Specialized;


namespace iniTest1
{
    public partial class Form1 : Form
    {

        [DllImport("kernel32")]

        // 写入ini文件操作section,key,value

        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]

        // 读取ini文件操作section,key,和返回的keyvalue

        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);



        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.ShowDialog();

                textBox1.Text = openFileDialog1.FileName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }  
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string FileName = textBox1.Text;

            string section = textBox2.Text;

            string key = textBox3.Text;

            string keyValue = textBox4.Text;
            try
            {
                //写入ini文件属性  
                WritePrivateProfileString(section, key, keyValue, FileName);

                MessageBox.Show("成功写入INI文件!", "信息");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }  
        }

        private void button3_Click(object sender, EventArgs e)
        {
            StringBuilder temp = new StringBuilder(255);

            string FileName = textBox1.Text;

            string section = textBox2.Text;

            string key = textBox3.Text;
            //读取ini文件属性值---赋予当前属性上temp  
            int i = GetPrivateProfileString(section, key, "无法读取对应数值!", temp, 255, FileName);

            //显示读取的数值  
            textBox4.Text = temp.ToString();  
        }
    }
}

 

转载于:https://www.cnblogs.com/s7ven/articles/3575235.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值