Source.Library.Common.FileHelper.IniFile

/**
* <p>Description: (IniFile)</p>
* <p>@version 1.0.0</p>
* <p>Modifaction:(Date-Version-Author-Description)</p>
* <p>------------------------------------------------------------------------</p>
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Source.Library.Common.FileHelper
{
    public class IniFile
    {
        public string path;
        public IniFile(string INIPath)
        {
            path = 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);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);
        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <param name="Value"></param>
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="Section"></param>
        /// <param name="Key"></param>
        /// <returns></returns>
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
            return temp.ToString();
        }
        public byte[] IniReadValues(string section, string key)
        {
            byte[] temp = new byte[255];
            int i = GetPrivateProfileString(section, key, "", temp, 255, this.path);
            return temp;
        }
        /// <summary>
        /// 删除ini文件下所有段落
        /// </summary>
        public void ClearAllSection()
        {
            IniWriteValue(null, null, null);
        }
        /// <summary>
        /// 删除ini文件下personal段落下的所有键
        /// </summary>
        /// <param name="Section"></param>
        public void ClearSection(string Section)
        {
            IniWriteValue(Section, null, null);
        }
    }
}

转载于:https://www.cnblogs.com/TNTZWC/archive/2011/01/26/1945452.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c#.net完美读取及设置INI文件 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; namespace yjjk { public partial class setfdbs : Form { public setfdbs() { InitializeComponent(); } private void setfdbs_Load(object sender, EventArgs e) { string Current; Current = Directory.GetCurrentDirectory();//获取当前根目录 Console.WriteLine("Current directory {0}", Current); Ini ini = new Ini(Current + "/config.ini"); // 读取ini string fdbs = ini.ReadValue("Setting", "fdbs"); string fdname = ini.ReadValue("Setting", "fdname"); tbfdbs.Text = fdbs; tbfdname.Text = fdname; } private void button1_Click(object sender, EventArgs e) { string fdbs, fdname; fdbs = Common.ChkTextInput(tbfdbs.Text.ToString()); fdname = Common.ChkTextInput(tbfdname.Text.ToString()); string Current; Current = Directory.GetCurrentDirectory();//获取当前根目录 Console.WriteLine("Current directory {0}", Current); // 写入ini try { Ini ini = new Ini(Current + "/config.ini"); ini.Writue("Setting", "fdbs", fdbs); ini.Writue("Setting", "fdname", fdname); MessageBox.Show(" 保存成功!!", "提示"); main.fdbs123 = fdbs; this.Close(); } catch { MessageBox.Show(" 配置错误!!", "提示"); } } private void button2_Click(object sender, EventArgs e) { this.Close(); } private void tbfdbs_DoubleClick(object sender, EventArgs e) { selfd selfd = new selfd(); selfd.getstr += new selfd.GetString(selfd_getstr); selfd.ShowDialog(); } void selfd_getstr(string fd, string fdname) { tbfdbs.Text = fd; tbfdname.Text = fdname; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值