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 ...
随机推荐
- Exception in thread "main" java.lang.UnsupportedClassVersionError: com/crack
执行一个jar文件的时候抛异常了 Exception in thread "main" java.lang.UnsupportedClassVersionError: com/cr ...
- 使用Apache POI操作Excel文件---在已有的Excel文件中插入一行新的数据
package org.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundEx ...
- FPGA常用设计思想与基本模块划分
常用设计思想与技巧 (1)乒乓操作; (2)串并转换; (3)流水线操作; (4)异步时钟域数据同步.是指如何在两个时钟不同步的数据域之间可靠地进行数据交换的问题.数据时钟域不同步主要有两种情况: ① ...
- 笔记:LNK2001不代表链接器真的需要链接相关符号
环境:VS2008 我们都知道,链接器在生成可执行程序时,会忽略那些没有用到的符号.但是昨天遇到一个链接问题,看起来与这条基本策略并不相符.首先看一个静态链接库的结构: lib | |---- ...
- 使用BeanShell 对比取出来的值
描述: 使用BeanShell 对比取出来的值,如不一致,报错 步骤一: 使用json Extractor后置处理器,取出"登入成功" 使用BeanS hell断言: 语法: if ...
- Linux学习笔记 - Shell 控制语句
if 语句 语法: #!/bin/bash a= b= if [ $a -eq $b ] then echo "a 等于 b" elif [ $a -gt $b ] then ec ...
- node的close
在http.ServerResponse对象的end方法被调用之前,如果连接被中断,将触发http.ServerResponse对象的close事件. var http=require("h ...
- 5月9日上课笔记-网页定位、网页动画【HTML5】
一.网页定位 position: static (默认值) relative 相对定位(相对原来的位置) right left botton top absolute 绝对定位 fixed: 固定定位 ...
- 在线pubmed
ESearch(文本搜索) eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi http://eutils.ncbi.nlm.nih.gov/entr ...
- 爬虫中urllib库
一.urllib库 urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urllib. ...