C#中读写配置参数文件(利用Windows的API)
[DllImport("kernel32")]
// 读配置文件方法的6个参数:所在的分区(section)、键值、 初始缺省值、 StringBuilder、 参数长度上限、配置文件路径
private static extern int GetPrivateProfileString(string section, string key, string deVal, StringBuilder retVal,
int size, string filePath);
[DllImport("kernel32")]
// 写配置文件方法的4个参数:所在的分区(section)、 键值、 参数值、 配置文件路径
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
public static void SetValue(string section, string key, string value)
{
//获得当前路径,当前是在Debug路径下
string strPath = Environment.CurrentDirectory + "\\system.ini";
WritePrivateProfileString(section, key, value, strPath);
}
public static string GetValue(string section, string key)
{
StringBuilder sb = new StringBuilder();
string strPath = Environment.CurrentDirectory + "\\system.ini";
//最好初始缺省值设置为非空,因为如果配置文件不存在,取不到值,程序也不会报错
GetPrivateProfileString(section, key, "配置文件不存在,未取到参数", sb, , strPath);
return sb.ToString();
}
功能说明:程序加载时,创建配置文件并往里面写入波特率参数。(配置文件不需要事先存在,此Windows的API会自动创建)。点击button1,将取到的波特率显示到textBox1中。
完整代码如下:
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} [DllImport("kernel32")]
// 读配置文件方法的6个参数:所在的分区(section)、键值、 初始缺省值、 StringBuilder、 参数长度上限、配置文件路径
private static extern int GetPrivateProfileString(string section, string key, string deVal, StringBuilder retVal,
int size, string filePath); [DllImport("kernel32")]
// 写配置文件方法的4个参数:所在的分区(section)、 键值、 参数值、 配置文件路径
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); public static void SetValue(string section, string key, string value)
{
//获得当前路径,当前是在Debug路径下
string strPath = Environment.CurrentDirectory + "\\system.ini";
WritePrivateProfileString(section, key, value, strPath);
} public static string GetValue(string section, string key)
{
StringBuilder sb = new StringBuilder();
string strPath = Environment.CurrentDirectory + "\\system.ini";
//最好初始缺省值设置为非空,因为如果配置文件不存在,取不到值,程序也不会报错
GetPrivateProfileString(section, key, "配置文件不存在,未取到参数", sb, , strPath);
return sb.ToString(); } private void Form1_Load(object sender, EventArgs e)
{
SetValue("参数","波特率","");
} private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = GetValue("参数", "波特率");
} }
}
程序界面:



C#中读写配置参数文件(利用Windows的API)的更多相关文章
- 读取xml文件中的配置参数实例_java - JAVA
文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 paras.xml文件 <?xml version="1.0" encoding=" ...
- oracle中有关初始化参数文件的几个视图对比
涉及oracle中有关初始化参数文件的几个视图主要有:v$paraemter,v$parameter2,v$system_parameter,v$system_parameter2,v$spparam ...
- oracle中的初始化参数文件
oracle初始化参数文件管理 oracle实例是指运行状态下的oracle软件,是由内存结构跟一些进程结构组成的,主要实现数据库的访问跟控制功能,是oracle的核心. 初始化参数文件是oracle ...
- vue-cli脚手架中webpack配置基础文件详解
一.前言 原文:https://segmentfault.com/a/1190000014804826 vue-cli是构建vue单页应用的脚手架,输入一串指定的命令行从而自动生成vue.js+wep ...
- vue-cli 脚手架中 webpack 配置基础文件详解
一.前言 vue-cli是构建vue单页应用的脚手架,输入一串指定的命令行从而自动生成vue.js+wepack的项目模板.这其中webpack发挥了很大的作用,它使得我们的代码模块化,引入一些插件帮 ...
- struts2 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...
- MFC读写配置ini文件
https://blog.csdn.net/naibozhuan3744/article/details/78783446 https://blog.csdn.net/rayborn1105/arti ...
- 笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用
Struts2登录 1. 需要注意:Struts2需要运行在JRE1.5及以上版本 2. 在web.xml配置文件中,配置StrutsPrepareAndExecuteFilter或FilterDis ...
- android的数据与访问(1)-我的app配置参数文件放在哪儿?
系统提供数据处理方式: 1.SharedPreferences 2.文件存储 3.轻量级的数据.如SQLLite 1.简单存储 是android提供的起来年纪的数据存储方式:SharedPerence ...
随机推荐
- 生产环境LNMP (交友)
一. 下载一键安装包 LNMP 官方地址为:http://lnmp.org/ 原生产环境为 : php5.4.32 mysql 5.5 nginx 1.40 我们用LNMP包安装相应的环境 ...
- python自动发送邮件
Python 的 smtplib 模块提供了发送电子邮件的功能.测试报告出来后,然后就把报告发送到邮箱. 一.先来看简单的列子 使用QQ邮箱发送邮件,使用的是授权码,需要先到QQ邮箱申请授权码. 邮箱 ...
- spring装载配置文件失败报错:org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException
Tomcat容器启动失败,找到 debug日志一看: Context initialization failed org.springframework. beans.factory.xml.XmlB ...
- celery制作定时任务
celery参考地址:http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#starting-the-schedu ...
- IIS注册Framework4.0
打开iis,确认一下framework4.0是否已经安装. 开始->控制面板->管理工具->Internet信息服务->应用程序池(左边栏)->观察右边主界面.net f ...
- struts2学习(4)struts2核心知识III
一.result配置: result - name 就是前面返回的值,success,error等. type: dispatcher. 默认是这个,底层是jsp的forward: redirect: ...
- java代码---数据类型的强制转换----不懂啊
总结:看写的测试代码 字符到整型必须进行强制转换 package com.a.b; //byte→int 可以 int范围大,不必转换 B.short→long //C.float→double 这个 ...
- Java-Runoob:Java 方法
ylbtech-Java-Runoob:Java 方法 1.返回顶部 1. Java 方法 在前面几个章节中我们经常使用到 System.out.println(),那么它是什么呢? println( ...
- 杂项:WWW
ylbtech-杂项:WWW WWW是环球信息网的缩写,(亦作“Web”.“WWW”.“'W3'”,英文全称为“World Wide Web”),中文名字为“万维网”,"环球网"等 ...
- iRedMail的搭建过程记录
iRedMail的搭建和注意事项 经过一段时间的折腾,终于将iRedMail搭建起来了,下面介绍一下搭建的过程,以及注意事项. 注意事项: 1. iRedMail不支持重复安装,如果安装错误,请重置 ...