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 ...
随机推荐
- sphinx-1.3.0扩展在pPHP 7.0.7版本编译不通过
在这个网友也是在php7上面编译插件不通过 https://bugs.php.net/bug.php?id=71586 下php7对应的扩展文件即可 http://git.php.net/?p=pec ...
- MOSS 2013研究系列---隐藏Ribbon
我们在开发Sharepoint 2013 的时候,常常需要隐藏Ribbon,那个Ribbon是属于Office的特征,但是我们做门户的时候,大家都不希望看见到它,但是我们又离不开它,以管理的身份进行设 ...
- sed命令n,N,d,D,p,P,h,H,g,G,x解析
1.sed执行模板=sed '模式{命令1;命令2}'即逐行读入模式空间,执行命令,最后输出打印出来2.为方便下面,先说下p和P,p打印当前模式空间内容,追加到默认输出之后,P打印当前模式空间开端至\ ...
- linux下踢出已登录用户
通过xshell登录到linux,看到如下所示,有3个用户,但是前面两个不知在哪登录的了,那就踢出吧. 先确认一下自己是哪个 顺便注意一下“whoami”和“who am i”的不同 然后踢出前面两个 ...
- LogStation 支持浏览器实时查看日志
我们在logback 分布式日志汇总中已经将日志输出到了all.logs,LogStation支持浏览器实时查看日志,适合研发和运维彼此独立的场景:研发没有服务器权限,却想看日志实时输出.再配合ngi ...
- 高德js API根据出行方式和出现策略由起始点经纬度实现路线规划
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http ...
- 二、Spark在Windows下的环境搭建
由于Spark是用Scala来写的,所以Spark对Scala肯定是原生态支持的,因此这里以Scala为主来介绍Spark环境的搭建,主要包括四个步骤,分别是:JDK的安装,Scala的安装,Spar ...
- Zabbix 添加 WEB 监控
添加 WEB Monitorings Web Monitoring是用来监控web程序的,可以监控到web程序的下载速度.返回码及响应时间,还支持把一组连续的web动作作为一个整体来监控. 下面我们以 ...
- SQLSERVER出错提示:此上下文中不允许使用''。此处只允许使用常量、表达式或变量。不允许使用列名。
在执行一段SQL语句时出现了这样的一段错误提示,在网上找了不少答案,都说的不是很详细,反复修改试验,最终解决了此问题.原SQl语句为: insert into shoufei(djbh,sflb,jk ...
- OD 实验(四) - 去除 NAG 窗口的几种方法
程序: 运行 弹出一个窗口,说要注册 点击确定,到主窗口 关闭主窗口 然后弹出提醒注册的对话框 逆向程序 用 OD 打开程序 GetModuleHandleA 获取程序模块的句柄,程序在内存中的基址 ...