C# ini (配置文件)API调用以及例子

首先是INI 类例子里面包含了INI用到的API的声明以及 读写两个方法:

 1 public class Ini//文件读写
 2         {
 3             // 声明INI文件的写操作函数 WritePrivateProfileString()
 4 
 5             [System.Runtime.InteropServices.DllImport("kernel32")]
 6 
 7             private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
 8 
 9             // 声明INI文件的读操作函数 GetPrivateProfileString()
10 
11             [System.Runtime.InteropServices.DllImport("kernel32")]
12 
13             private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
14 
15 
16             private string sPath = null;
17             public Ini(string path)
18             {
19                 this.sPath = path;
20             }
21 
22             public void Writue(string section, string key, string value)
23             {
24 
25                 // section=配置节,key=键名,value=键值,path=路径
26 
27                 WritePrivateProfileString(section, key, value, sPath);
28 
29             }
30             public string ReadValue(string section, string key)
31             {
32 
33                 // 每次从ini中读取多少字节
34 
35                 System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
36 
37                 // section=配置节,key=键名,temp=上面,path=路径
38 
39                 GetPrivateProfileString(section, key, "", temp, 255, sPath);
40 
41                 return temp.ToString();
42 
43             }
44         }

然后是方法调用例子:

            写入:

            string Current;

            Current = Directory.GetCurrentDirectory();//获取当前根目录

            Ini ini = new Ini(Current + "/config.ini");

            ini.Writue(string 标签,string 识别键, value 保存参数);

           读取:

            string Current;

            Current = Directory.GetCurrentDirectory();//获取当前根目录

            Ini ini = new Ini(Current + "/config.ini");

            string a = ini.Readvalue(string 标签,string 识别键);

 

转载于:https://www.cnblogs.com/dongzhaosheng/archive/2012/11/20/2779163.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值