C#操作INI配置文件示例详解
作者:cnc 发布时间:2021-11-06 11:03:01
标签: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 = "";
}
}
}
运行结果:
0
投稿
猜你喜欢
- Java执行hadoop的基本操作实例代码向HDFS上传本地文件public static void uploadInputFile(Str
- 背景众所周知,所有被打开的系统资源,比如流、文件或者Socket连接等,都需要被开发者手动关闭,否则随着程序的不断运行,资源泄露将会累积成重
- 一、前言文稿扫描大家用的都比较频繁、想是各种证件、文件都可以通过扫描文稿功能保存到手机。相比直接拍照,在扫描文稿时,程序会对图像进行一些矫正
- 一、对AOP的初印象首先先给出一段比较专业的术语:在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向
- 本文实例为大家分享了Java实现五子棋网络版的具体代码,供大家参考,具体内容如下需求分析:对于网络五子棋而言,在普通五子棋的基础上需要添加以
- 这篇文章主要介绍了Jmeter如何添加循环控制器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以
- 1、dose not point to a valid jvm installation出错问题按照以下方法设置一定可以不会出现这个错误。我
- 什么是Dozer?Dozer是一种Java Bean到Java Bean的映射器,递归地将数据从一个对象复制到另一个对象,它是一个强大的,通
- 使用Spring Boot 与Dubbo集成,这里我之前尝试了使用注解的方式,简单的使用注解注册服务其实是没有问题的,但是当你涉及到使用注解
- 下面我们就字符串连接方面分析。1.String打开String的源码,如图所示会发现存储字符串的字符数值是final常量。再看String的
- 一、背景Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE
- 部分同学在使用 idea 时可能会遇到输入 sout 无法出现自动补全 System.out.println();的情况,其实 idea 默
- Java IO 转化流乱码引起转换流读取乱码读取电脑磁盘上的Java.txt文件内容,文件路径: e:\Java\Java.txt
- SQL注入是一种很简单的攻击手段,但直到今天仍然十分常见。究其原因不外乎:No patch for stupid。为什么这么说,下面就以JA
- 一.MyBatis简介1)MyBatis 是一款优秀的持久层框架2)MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结
- 英文设置加粗可以在xml里面设置: <SPAN style="FONT-SIZE: 18px">androi
- @Transactional跟@DS动态数据源注解冲突背景前阵子写一个项目时,有个需求是要往3个库,3个表里插入数据,在同一个方法里,公司是
- 目录简介springfox大致原理:SpringBoot整合Swagger2引入依赖编写配置类配置SwaggerSwagger2常用注解使用
- 每次写批量的时候,都要在网上搜索一下,虽然都做过多次了,但具体的自己还是记不住(汗颜),所以索性今天就记录下来。前期说明:foreach的主
- 1.新建文件上传页面在static目录中新建upload-test.html,上传页面代码如下所示:<!DOCTYPE html>