源文件地址: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 = "";

}

}

}

运行结果:

C#操作INI配置文件示例的更多相关文章

  1. Python中操作ini配置文件

    这篇博客我主要想总结一下python中的ini文件的使用,最近在写python操作mysql数据库,那么作为测试人员测试的环境包括(测试环境,UAT环境,生产环境)每次需要连接数据库的ip,端口,都会 ...

  2. php操作ini配置文件

    有些配置化的数据放到配置文件可以方便管理,比如数据库信息,路由信息,先建立配置文件,test.ini [database_setting] host=127.0.0.1 user=root passw ...

  3. c#读写ini配置文件示例

    虽然c#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧     其他人写的都是调用非托管kernel32.dll.我也用过 ...

  4. 操作ini配置文件设计一个最基本的可视化数据库系统

    对于很多小项目来说,不需要搭建专门的数据库系统(例如用SQLite搭建本地数据库),这时可以用ini配置文件实现一个最基本的数据库,实现数据库最基本的增删改查功能. ini配置文件的用法参考我以前写的 ...

  5. VC++/MFC操作ini配置文件详解

    在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下: 一.将信息写入.INI文件中. 1.所用的WINA ...

  6. C# 操作ini配置文件

    最近使用Winform做一个小系统,由于需要保存一些默认配置项.自然就想到了轻量级的配置文件类型ini.在此也分享和记录一下实现方式,方便以后查询和使用. 废话不多说上代码: 实现公共函数↓ publ ...

  7. 【转】ini载入保存类,操作INI配置文件方便的很

    /****************************************************************** * * ^_^ 恶猫 独门商标 挖哈哈 * * QQ:\> ...

  8. 【个人使用.Net类库】(1)INI配置文件操作类

    开发接口程序时,对于接口程序配置的IP地址.端口等都需要是可配置的,而在Win Api原生实现了INI文件的读写操作,因此只需要调用Win Api中的方法即可操作INI配置文件,关键代码就是如何调用W ...

  9. python开发_configparser_解析.ini配置文件工具_完整版_博主推荐

    # # 最近出了一趟差,是从20号去的,今天回来... # 就把最近学习的python内容给大家分享一下... # ''' 在python中,configparser模块提供了操作*.ini配置文件的 ...

随机推荐

  1. js基础 - 兼容代码

    js基础 - 兼容代码 . scrollTop . chrome document.body.scrollTop . IE && firefox document.documentEl ...

  2. VLC命令参数(转载)

    转载自: http://blog.csdn.net/bytxl/article/details/6613449 http://www.cnblogs.com/MikeZhang/archive/201 ...

  3. 关于SQL server的一些知识点

    关于怎么打开xp_cmdshell的方法: exec sp_configure 'show advanced option',1reconfiguregoexec sp_configure 'xp_c ...

  4. Oracle数据库还原方法

    Win +X → 运行→cmd C:\Documents and Settings\Administrator>sqlplus /nolog SQL> connect sys/passwo ...

  5. Redmine配置

    官网步骤说明 http://www.redmine.org/projects/redmine/wiki/RedmineInstall 搭环境 1.MySql 2.RailsInstaller:Redm ...

  6. cf B. Number Busters

    http://codeforces.com/contest/382/problem/B 题意:给你Aa,b,w,x,c,然后每经过1秒,c=c-1;  如果b>=x,b=b-x;否则 a=a-1 ...

  7. Java Integer类分析

    public static final int   MIN_VALUE = 0x80000000;  -2^31 public static final int   MAX_VALUE = 0x7ff ...

  8. java学习之变量

    看完了常量,那我们来看下变量. 变量顾名思义,也就是能变化的量,也就是说已经定义之后它的值仍然是可以变的,不像常量一经定义便不能够改变了.比如说现在我们需要一个数,需要用户输入之后才能,确定这个数是几 ...

  9. BZOJ1631: [Usaco2007 Feb]Cow Party

    1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 459  Solved: 338[Submit ...

  10. 3 D. Least Cost Bracket Sequence

    题目大意: 这是一个规则的字符括号序列.一个括号序列是规则的,那么在序列里面插入‘+’ 和 ‘1’ 会得到一个正确的数学表达式. 合法:(())(), (),(()(())) 不合法:)(,((),( ...