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 = "";
}
}
}
运行结果:
C#操作INI配置文件示例的更多相关文章
- Python中操作ini配置文件
这篇博客我主要想总结一下python中的ini文件的使用,最近在写python操作mysql数据库,那么作为测试人员测试的环境包括(测试环境,UAT环境,生产环境)每次需要连接数据库的ip,端口,都会 ...
- php操作ini配置文件
有些配置化的数据放到配置文件可以方便管理,比如数据库信息,路由信息,先建立配置文件,test.ini [database_setting] host=127.0.0.1 user=root passw ...
- c#读写ini配置文件示例
虽然c#里都是添加app.config 并且访问也很方便 ,有时候还是不习惯用他.那么我们来做个仿C++下的那种ini配置文件读写吧 其他人写的都是调用非托管kernel32.dll.我也用过 ...
- 操作ini配置文件设计一个最基本的可视化数据库系统
对于很多小项目来说,不需要搭建专门的数据库系统(例如用SQLite搭建本地数据库),这时可以用ini配置文件实现一个最基本的数据库,实现数据库最基本的增删改查功能. ini配置文件的用法参考我以前写的 ...
- VC++/MFC操作ini配置文件详解
在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下: 一.将信息写入.INI文件中. 1.所用的WINA ...
- C# 操作ini配置文件
最近使用Winform做一个小系统,由于需要保存一些默认配置项.自然就想到了轻量级的配置文件类型ini.在此也分享和记录一下实现方式,方便以后查询和使用. 废话不多说上代码: 实现公共函数↓ publ ...
- 【转】ini载入保存类,操作INI配置文件方便的很
/****************************************************************** * * ^_^ 恶猫 独门商标 挖哈哈 * * QQ:\> ...
- 【个人使用.Net类库】(1)INI配置文件操作类
开发接口程序时,对于接口程序配置的IP地址.端口等都需要是可配置的,而在Win Api原生实现了INI文件的读写操作,因此只需要调用Win Api中的方法即可操作INI配置文件,关键代码就是如何调用W ...
- python开发_configparser_解析.ini配置文件工具_完整版_博主推荐
# # 最近出了一趟差,是从20号去的,今天回来... # 就把最近学习的python内容给大家分享一下... # ''' 在python中,configparser模块提供了操作*.ini配置文件的 ...
随机推荐
- CSS3中动画transform必须要了解的Skew变化原理
transform是CSS3中比较安全的动画(对于性能来说),其中有一些动画属性,来执行不同的变化.今天我们来了解skew的变化原理. skew,其实使用的频率不是很高,当然也没有最低.但是往往,一直 ...
- Linux常用命令大全(2)
系统信息arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS / ...
- Javascript学习之函数(function)
在JS中,Function(函数)类型实际上是对象;每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象的指针. 一 函 ...
- TF-IDF与余弦相似性的应用
类似的算法已经被写成了工具,比如基于Java的Classifier4J库的SimpleSummariser模块.基于C语言的OTS库.以及基于classifier4J的C#实现和python实现.
- GPIO软件模拟I2C
/***************************************************************************** * * Filename: * ----- ...
- java的"=="与"equals"
equals 方法是 java.lang.Object 类的方法. 有两种用法说明: (1)对于字符串变量来说,使用“==”和“equals()”方法比较字符串时,其比较方法不同. “==”比较两个变 ...
- <未测>源码升级安装glibc和rpm升级glibc
源码升级安装glibc和rpm升级glibc http://jacklin9.spaces.live.com/blog/cns!A891B52E1182AFB2!346.entry http://bl ...
- 【转】Java集合框架List,Map,Set等全面介绍
原文网址:http://android.blog.51cto.com/268543/400557 Java Collections Framework是Java提供的对集合进行定义,操作,和管理的包含 ...
- Manacher Ural 1297 Palindrome
1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...
- Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
B. Complete The Graph time limit per test 4 seconds memory limit per test 256 megabytes input standa ...