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 }

程序界面:

 

作者:xh6300

--------------------------------------------

本文系原创作品,转载请注明出处。如果您认为文章对您有帮助,可以点击下方的【好文要顶】或【关注我】;如果您想进一步表示感谢,可以通过网页右侧的【打赏】功能对我进行打赏。感谢您的支持,我会继续写出更多对大家有帮助的文章!文章有不理解的地方欢迎跟帖交流,博主经常在线!^_^

原文地址:https://www.cnblogs.com/xh6300/p/5895759.html