源文件地址: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. Bootstrap_Javascript_固定定位

    Affix 插件可以对任何元素进行固定定位,其中比较简单的方法,就是通过自定义属性 data 来触发.其主要包括两个参数: 1.data-spy:取值 affix,表示元素固定不变的. 2.data- ...

  2. ecshop数据表

    ecs_account_log:账户变动日志(注册用户充值.支付等记录信息) ecs_ad:广告表 ecs_admin_action:管理员权限表(定义了128项功能操作) ecs_admin_log ...

  3. 史上最强NDK入门项目实战

    目标: 利用NDK生成SO库,使用SO库进行JNI调用,在Android sdcard创建文件并写入数据. 工具: NDK1.5 R1, android SDK1.5 R1, SDCARD, Ecli ...

  4. 【面霸1】php知识点

    PHP简介 Hypertext Preprocessor,超文本预处理器的缩写,主要是用于服务端的脚本程序 PHP 标记风格 1.xml风格   < ? php ? > 2.脚本风格  & ...

  5. vm拷贝cloudera-scm-agent造成问题

    ------------网络问题---------- ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: service network restartShutting  ...

  6. BZOJ 2423 最长公共子序列

    Description 字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0, ...

  7. python MySQLdb segmentation fault

    import MySQLdb conn = MySQLdb.connect(host=_host,user=_user,passwd=_passwd,db=_db,charset=_charset,p ...

  8. Task schedule

    hdu4907:http://acm.hdu.edu.cn/showproblem.php?pid=4907 题意:中文题. 题解:这一道水题,自己调了很久,并且没有注意到序列可能是乱序的,wa了好几 ...

  9. java根据本地Ip获取mac地址

    import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; impo ...

  10. Cent OS 命令行和窗口界面默认登录切换方法

    在 CentOS 中的修改方法如下: 1. root登陆,免得老是sudo 2. 打开/etc/inittab 文件     #vim /etc/inittab 3. 在默认的 run level 设 ...