C#操作INI配置文件示例详解

 更新时间:2017年07月13日 11:41:41   作者:cnc  
这篇文章主要为大家详细介绍了C#操作INI配置文件示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#操作INI配置文件示例的具体代码,供大家参考,具体内容如下

源文件地址:C#操作INI配置文件示例

创建如图所示的控件:

源代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication3

{

  public partial class Form1 : Form

  {

    public Form1()

    {

      InitializeComponent();

    }

 

    [DllImport("kernel32.dll")]

    private static extern long WritePrivateProfileString(string section, string key, string value, string filepath);

 

    [DllImport("kernel32.dll")]

    private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder returnvalue,intbuffersize,string filepath);

 

    private string IniFilePath;
    private void Form1_Load(object sender, EventArgs e)

    {

      comboBox1.Text = "男";

      for (int i = 1; i <= 100; i++)

      {

        comboBox2.Items.Add(i.ToString());

      }

      comboBox2.Text = "18";

      IniFilePath = Application.StartupPath + "\\Config.ini";

    }

 

    private void button1_Click(object sender, EventArgs e)
    {
      if ((textBox1.Text.Trim() != "") && (textBox2.Text.Trim() != ""))
      {
        string Section = "Information";
        try

        {

          WritePrivateProfileString(Section, "Name", textBox1.Text.Trim(), IniFilePath);
          WritePrivateProfileString(Section, "Gender", comboBox1.Text, IniFilePath);
          WritePrivateProfileString(Section, "Age", comboBox2.Text, IniFilePath);
          WritePrivateProfileString(Section, "Region", textBox2.Text.Trim(), IniFilePath);

        }
        catch (Exception ee)

        {

          MessageBox.Show(ee.Message);

        }
      }

      else

      {

        MessageBox.Show("姓名或地区不能为空!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);

      }
    }

 

    private void button2_Click(object sender, EventArgs e)
    {
      string outString;
      try

      {
        GetValue("Information", "Name", out outString);
        textBox1.Text = outString;
        GetValue("Information", "Gender", out outString);
        comboBox1.Text = outString;
        GetValue("Information", "Age", out outString);
        comboBox2.Text = outString;
        GetValue("Information", "Region", out outString);
        textBox2.Text = outString;

      }

      catch (Exception ee)

      {

        MessageBox.Show(ee.Message);

      }

 

    }

 

    private void GetValue(string section,string key, out string value)
    {

      StringBuilder stringBuilder = new StringBuilder();
      GetPrivateProfileString(section, key, "", stringBuilder, 1024, IniFilePath);
      value = stringBuilder.ToString();

    }

 

    private void button3_Click(object sender, EventArgs e)

    {
      textBox1.Text = "";
      comboBox1.Text = "男";
      comboBox2.Text = "18";
      textBox2.Text = "";
    }

  }

} 

 运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C# 泛型集合类List<T>使用总结

    C# 泛型集合类List<T>使用总结

    本文主要主要介绍了C# 泛型集合类List<T>使用总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧<BR>
    2022-05-05
  • 基于WPF手写一个简单的消息对话框

    基于WPF手写一个简单的消息对话框

    消息对话框是UI界面中不可或缺的组成部分,用于给用户一些提示,警告或者询问的窗口,本文将使用WPF手写一个简单的消息对话框,感兴趣的小伙伴可以了解下
    2023-12-12
  • C#实现给DataGrid单元行添加双击事件的方法

    C#实现给DataGrid单元行添加双击事件的方法

    这篇文章主要介绍了C#实现给DataGrid单元行添加双击事件的方法,较为详细的分析了C#给DataGrid单元添加双击事件的步骤及相关实现代码,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • C#如何对多线程、多任务管理(demo)

    C#如何对多线程、多任务管理(demo)

    这篇文章主要通过一个小demo介绍了C#如何对多线程、多任务管理,需要的朋友可以参考下
    2015-07-07
  • 如何解决hash冲突

    如何解决hash冲突

    上篇文章 为什么哈希存取比较快?使用它需要付出什么代价 只是简单介绍了使用hash所带来的利与弊。并未涉及hash的技术细节,本文则着重学习一下如何解决哈希编址的冲突问题。
    2016-06-06
  • 浅谈对c# 面向对象的理解

    浅谈对c# 面向对象的理解

    这篇文章主要介绍了个人对c# 面向对象的理解,算是一个入门篇吧,给需要的小伙伴参考下,抛砖引玉。
    2014-12-12
  • C#中this的用法集锦

    C#中this的用法集锦

    本文给大家汇总介绍了C#中的几种this用法,相信大家应该有用过,但你用过几种?以下是个人总结的this几种用法,欢迎大家拍砖,废话少说,直接列出用法及相关代码。
    2015-06-06
  • 浅析C# 委托(Delegate)

    浅析C# 委托(Delegate)

    这篇文章主要介绍了C# 委托(Delegate)的相关资料,文中讲解非常详细,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • 深入浅析C#中单点登录的原理和使用

    深入浅析C#中单点登录的原理和使用

    这篇文章主要介绍了C#中单点登录的原理和使用,需要的朋友可以参考下
    2017-10-10
  • C#很简单而又很经典的一句代码实例

    C#很简单而又很经典的一句代码实例

    这篇文章主要给大家分享介绍了关于C#很简单而又很经典的一句代码,文中通过示例代码介绍的非常详细,对大家学习或者使用C#具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-07-07

最新评论