码迷,mamicode.com
首页 > Windows程序 > 详细

C#读写.ini文件

时间:2018-03-21 15:03:12      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:字符串   引入   color   lsp   标题   pad   初始化   add   nic   

   转载来源:

http://blog.csdn.net/source0573/article/details/49668079

https://www.cnblogs.com/wang726zq/archive/2012/07/30/ini.html

 

    INI文件是一种按照特点方式排列的文本文件,该文件主要存放用户所做的选择以及系统的各种参数。用户可以通过修改INI文件,来改变应用程序和系统的很多配置。但自从Windows 95的退出,在Windows系统中引入了注册表的概念,INI文件在Windows系统的地位就开始不断下滑,这是因为注册表的独特优点,使应用程序和系统都把许多参数和初始化信息放进了注册表中。但在某些场合,INI文件还拥有其不可替代的地位。

    INI文件的结构:INI文件是一种按照特点方式排列的文本文件。每一个INI文件构成都非常类似,由若干段落(section)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键词(keyword)和一个等号,等号右边的就是关键字对应的值(value)。其一般形式如下:

       [Section1]

  KeyWord1 = Valuel

  KeyWord2 = Value2

   ......

  [Section2]

  KeyWord3 = Value3

  KeyWord4 = Value4

    Windows系统自带的Win32的API函数GetPrivateProfileString()和WritePrivateProfileString()分别实现了对INI文件的读写操作,他们位于kernel32.dll下。

    但是令人遗憾的是C#所使用的.NET框架下的公共类库并没有提供直接操作INI文件的类,所以唯一比较理想的方法就是调用API函数。

    然后,.Net框架下的类库是基于托管代码的,而API函数是基于非托管代码的,(在运行库的控制下执行的代码称作托管代码。相反,在运行库之外运行的代码称作非托管代码。)如何实现托管代码与非托管代码之间的操作呢?.Net框架的System.Runtime.InteropServices命名空间下提供各种各样支持COM interop及平台调用服务的成员,其中最重要的属性之一DllImportAttribute可以用来定义用于访问非托管API的平台调用方法,它提供了对从非托管DLL导出的函数进行调用所必需的信息。下面就来看一下如何实现C#与API函数的互操作。

读操作:

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        //section:要读取的段落名
        //key: 要读取的键
        //defVal: 读取异常的情况下的缺省值
        //retVal: key所对应的值,如果该key不存在则返回空值
        //size: 值允许的大小
        //filePath: INI文件的完整路径和文件名

 

写操作:

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        //section: 要写入的段落名
        //key: 要写入的键,如果该key存在则覆盖写入
        //val: key所对应的值
        //filePath: INI文件的完整路径和文件名

 

为读写操作创建一个类,用于其他函数来访问:

 1 using System.Runtime.InteropServices;
 2     /*************读写.ini类*************/
 3     public class NiceIniWriteAndRead
 4     {
 5         [DllImport("kernel32")]
 6         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
 7         //section: 要写入的段落名
 8         //key: 要写入的键,如果该key存在则覆盖写入
 9         //val: key所对应的值
10         //filePath: INI文件的完整路径和文件名
11 
12         [DllImport("kernel32")]
13         private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
14         //section:要读取的段落名
15         //key: 要读取的键
16         //defVal: 读取异常的情况下的缺省值
17         //retVal: key所对应的值,如果该key不存在则返回空值
18         //size: 值允许的大小
19         //filePath: INI文件的完整路径和文件名
20 
21 
22         //------------【函数:将字符串写入ini】------------    
23 
24         //section: 要写入的段落名
25         //key: 要写入的键,如果该key存在则覆盖写入
26         //val: key所对应的值
27         //filePath: INI文件的完整路径和文件名
28         //------------------------------------------------------------------------
29         public static bool WriteToIni(string section, string key, string val, string filePath)
30         {
31             bool result = true;
32             try
33             {
34                 string mystr1 = NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly));
35                 if (mystr1 != "error")
36                 {
37                     WritePrivateProfileString(section,key,val,filePath);
38                 }
39                 else
40                 {
41                     result = false;
42                 }
43             }
44             catch
45             {
46                 result = false;
47             }
48             return result;
49         }
50 
51 
52         //------------【函数:从ini读取字符串】------------    
53 
54         //section:要读取的段落名
55         //key: 要读取的键
56         //defVal: 读取异常的情况下的缺省值
57 
58         //filePath: INI文件的完整路径和文件名
59         //------------------------------------------------------------------------
60         public static string ReadFromIni(string section, string key, string def, string filePath)
61         {
62             StringBuilder retVal = new StringBuilder();
63             GetPrivateProfileString(section, key, def, retVal, 500, filePath);
64             return retVal.ToString();
65         }
66     }

 

在其他函数中调用:

 1         private void btnTexRead_Click(object sender, EventArgs e)
 2         {
 3             //文件路径
 4             string filePath = @"E:\123\789.ini";
 5             string section = "A";
 6             string key = "keyword1";
 7             text1.Text=NiceIniWriteAndRead.ReadFromIni(section, key, "", filePath);
 8 
 9             toolStripStatusLabel1.Text = "读取成功!";
10         }
11 
12         private void btnTextWrite_Click(object sender, EventArgs e)
13         {
14             //文件路径
15             string filePath = @"E:\123\789.ini";
16             string section = "A";
17             string key = "keyword1";
18             string val = text1.Text;
19             if (NiceIniWriteAndRead.WriteToIni(section, key, val, filePath))
20             {
21                 toolStripStatusLabel1.Text = "写入成功!";
22             }
23             else
24             {
25                 toolStripStatusLabel1.Text = "写入失败!";
26             }
27         }

 

C#读写.ini文件

标签:字符串   引入   color   lsp   标题   pad   初始化   add   nic   

原文地址:https://www.cnblogs.com/nicewe/p/8616811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!