zoukankan      html  css  js  c++  java
  • C#操作INI配置文件示例

    源文件地址:http://pan.baidu.com/share/link?shareid=2536126078&uk=1761850335
    创建如图所示的控件: 

    源代码:

    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 = "";

            }

        }

    }

     

     运行结果:

  • 相关阅读:
    做到就得到,人生成功的启示
    这个世界没什么过不去的事情,记我的经历
    要想富,先读书,没有文化要吃一辈子的亏
    git学习笔记11-git多人协作-实际多人怎么开发
    git学习笔记10-新开发的功能不想要了-强行删除分支
    git学习笔记09-bug分支-自己的分支改到一半了-要去改bug怎么办?
    git学习笔记08-分支管理策略-实际上我们应该怎么应用分支
    git学习笔记07-冲突了怎么办-那就解决冲突呗
    git学习笔记06-创建分支合并分支-比svn快多了,因为只有指针在改变
    git学习笔记05-从远程库克隆
  • 原文地址:https://www.cnblogs.com/cncc/p/3415694.html
Copyright © 2011-2022 走看看