C#读取写入配置文件Cinfig.ini方法

本文介绍了一个C#类库ConfiginiHelp,用于读取和写入.ini配置文件。通过WritePrivateProfileString和GetPrivateProfileString的DllImport方法,实现对ini文件的便捷操作。提供了多种写入和读取的重载方法,适用于不同场景。
摘要由CSDN通过智能技术生成

                  在winfrom项目中经常有一些不是很重要系统配置信息数据需要保存,且数据量不是很大,采用Cinfig.ini文件保存方式是首选。下面就本人改进的读取写入.ini文件方法粘出大家参考:


 /// <summary>
    /// 导入读取写入Cinfig,ini文件dll
    /// </summary>
    public class ConfiginiHelp
    {
        //WritePrivateProfileString方法说明:
        //功能:将信息写入ini文件
        //返回值:long,如果为0则表示写入失败,反之成功。
        //参数1(section):写入ini文件的某个小节名称(不区分大小写)。
        //参数2(key):上面section下某个项的键名(不区分大小写)。
        //参数3(val):上面key对应的value
        //参数4(filePath):ini的文件名,包括其路径(example: "c:\config.ini")。如果没有指定路径,仅有文件名,系统会自动在windows目录中查找是否有对应的ini文件,如果没有则会自动在当前应用程序运行的根目录下创建ini文件。

        //申明INI文件的写入操作函数WritePrivateProfileString()
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //GetPrivateProfileString方法使用说明:
        //功能:从ini文件中读取相应信息
        //返回值:返回所取信息字符串的字节长度
        //参数1(section):某个小节名(不区分大小写),如果为空,则将在retVal内装载这个ini文件的所有小节列表
        //参数2(key):欲获取信息的某个键名(不区分大小写),如果为空,则将在retVal内装载指定小节下的所有键列表。
        //参数3(def):当指定信息,未找到时,则返回def,可以为空。
        //参数4(retVal):一个字串缓冲区,所要获取的字符串将被保存在其中,其缓冲区大小至少为size。
        //参数5(size):retVal的缓冲区大小(最大字符数量)。
        //参数6(filePath):指定的ini文件路

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值