C# 对Ini文件读写

一、INI文件读写类INICLass
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace IniOperator
{
    public class INIClass
    {
        public string inipath;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public INIClass(string INIPath)
        {
            inipath = INIPath;
        }
        ///
        /// 写入INI文件
        ///
        /// 项目名称(如 [TypeName] )
        /// 键
        /// 值
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.inipath);
        }
        ///
        /// 读出INI文件
        ///
        /// 项目名称(如 [TypeName] )
        /// 键
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(500);
            int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
            return temp.ToString();
        }
        ///
        /// 验证文件是否存在
        ///
        /// 布尔值
        public bool ExistINIFile()
        {
            return File.Exists(inipath);
        }
    }
}

二、应用测试
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace IniOperator
{
    public partial class Form1 : Form
    {
        private INIClass inic;
        public Form1()
        {
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Ini文件(*.ini)|*.ini|所有文件(*.*)|*.*";
            ofd.FilterIndex = 0;
            ofd.Title = "打开Ini文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName;
                inic = new INIClass(textBox1.Text);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            if (inic.ExistINIFile())
            {
                try
                {
                    inic.IniWriteValue(textBox2.Text, textBox3.Text, textBox4.Text);
                    this.toolStripStatusLabel1.Text = "写入成功";
                }
                catch (Exception ex)
                {
                    this.toolStripStatusLabel1.Text = ex.Message;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                textBox4.Text = inic.IniReadValue(textBox2.Text, textBox3.Text);
                this.toolStripStatusLabel1.Text = textBox4.Text;
            }
            catch (Exception ex)
            {
                this.toolStripStatusLabel1.Text = ex.Message;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string name = Dns.GetHostName();
            this.toolStripStatusLabel1.Text = name;
            IPHostEntry me = Dns.GetHostByName(name);
            foreach (IPAddress ip in me.AddressList)
            { this.toolStripStatusLabel1.Text = this.toolStripStatusLabel1.Text+"  IP:"+ip.ToString(); }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值