C#中读写配置参数文件(利用Windows的API)

 
读配置文件与写配置文件的核心代码如下:
 
 1 [DllImport("kernel32")]
 2         //                        读配置文件方法的6个参数:所在的分区(section)、键值、     初始缺省值、     StringBuilder、   参数长度上限、配置文件路径
 3         private static extern int GetPrivateProfileString(string section, string key, string deVal, StringBuilder retVal,
 4             int size, string filePath);
 5 
 6         [DllImport("kernel32")]
 7         //                            写配置文件方法的4个参数:所在的分区(section)、  键值、     参数值、        配置文件路径
 8         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
 9 
10         public static void SetValue(string section, string key, string value)
11         {
12             //获得当前路径,当前是在Debug路径下
13             string strPath = Environment.CurrentDirectory + "\\system.ini";
14             WritePrivateProfileString(section, key, value, strPath);
15         }
16 
17         public static string GetValue(string section, string key)
18         {
19             StringBuilder sb = new StringBuilder(255);
20             string strPath = Environment.CurrentDirectory + "\\system.ini";
21             //最好初始缺省值设置为非空,因为如果配置文件不存在,取不到值,程序也不会报错
22             GetPrivateProfileString(section, key, "配置文件不存在,未取到参数", sb, 255, strPath);
23             return sb.ToString();
24 
25         }
26         
27         
28         

 

 
【应用举例】

功能说明:程序加载时,创建配置文件并往里面写入波特率参数。(配置文件不需要事先存在,此Windows的API会自动创建)。点击button1,将取到的波特率显示到textBox1中。

完整代码如下:  

 

 1 using System;
 2 using System.CodeDom;
 3 using System.Collections.Generic;
 4 using System.ComponentModel;
 5 using System.Data;
 6 using System.Drawing;
 7 using System.IO;
 8 using System.Linq;
 9 using System.Runtime.InteropServices;
10 using System.Text;
11 using System.Threading.Tasks;
12 using System.Windows.Forms;
13 
14 namespace WindowsFormsApplication1
15 {
16     public partial class Form1 : Form
17     {
18         public Form1()
19         {
20             InitializeComponent();
21         }
22 
23 
24         [DllImport("kernel32")]
25         //                        读配置文件方法的6个参数:所在的分区(section)、键值、     初始缺省值、     StringBuilder、   参数长度上限、配置文件路径
26         private static extern int GetPrivateProfileString(string section, string key, string deVal, StringBuilder retVal,
27             int size, string filePath);
28 
29         [DllImport("kernel32")]
30         //                            写配置文件方法的4个参数:所在的分区(section)、  键值、     参数值、        配置文件路径
31         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
32 
33         public static void SetValue(string section, string key, string value)
34         {
35             //获得当前路径,当前是在Debug路径下
36             string strPath = Environment.CurrentDirectory + "\\system.ini";
37             WritePrivateProfileString(section, key, value, strPath);
38         }
39 
40         public static string GetValue(string section, string key)
41         {
42             StringBuilder sb = new StringBuilder(255);
43             string strPath = Environment.CurrentDirectory + "\\system.ini";
44             //最好初始缺省值设置为非空,因为如果配置文件不存在,取不到值,程序也不会报错
45             GetPrivateProfileString(section, key, "配置文件不存在,未取到参数", sb, 255, strPath);
46             return sb.ToString();
47 
48         }
49 
50         private void Form1_Load(object sender, EventArgs e)
51         {
52             SetValue("参数","波特率","9600");
53         }
54 
55         private void button1_Click(object sender, EventArgs e)
56         {           
57             textBox1.Text = GetValue("参数", "波特率");
58         }
59 
60 
61        
62     }
63 }

 

程序界面:

 

 

转载于:https://www.cnblogs.com/xh6300/p/5895759.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值