c#读写ini

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Runtime.InteropServices;
 5 using System.Text;
 6 
 7 namespace ConsoleApp1
 8 {
 9     class iniClass
10     {
11         public string inipath;
12 
13         //声明API函数
14 
15         [DllImport("kernel32")]
16         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
17         [DllImport("kernel32")]
18         private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
19         /// <summary> 
20         /// 构造方法 
21         /// </summary> 
22         /// <param name="INIPath">文件路径</param> 
23         public iniClass(string INIPath)
24         {
25             inipath = INIPath;
26             if(!File.Exists(inipath))
27             {
28                 File.Create(inipath);
29             }
30         }
31 
32         public iniClass() { }
33 
34         /// <summary> 
35         /// 写入INI文件 
36         /// </summary> 
37         /// <param name="Section">项目名称(如 [TypeName] )</param> 
38         /// <param name="Key"></param> 
39         /// <param name="Value"></param> 
40         public void IniWriteValue(string Section, string Key, string Value)
41         {
42             if (string.IsNullOrEmpty(Section))
43             {
44                 throw new ArgumentException("必须指定节点名称", "Section");
45             }
46             if (string.IsNullOrEmpty(Key))
47             {
48                 throw new ArgumentException("必须指定键名称", "Key");
49             }
50             if (Value == null)
51             {
52                 throw new ArgumentException("值不能为null", "Value");
53             }
54 
55             WritePrivateProfileString(Section, Key, Value, this.inipath);
56         }
57         /// <summary> 
58         /// 读出INI文件 
59         /// </summary> 
60         /// <param name="Section">项目名称(如 [TypeName] )</param> 
61         /// <param name="Key"></param> 
62         public void IniReadValue(string Section, string Key, ref string Value)
63         {
64             StringBuilder temp = new StringBuilder(500);
65             if (string.IsNullOrEmpty(Section))
66             {
67                 throw new ArgumentException("必须指定节点名称", "Section");
68             }
69             if (string.IsNullOrEmpty(Key))
70             {
71                 throw new ArgumentException("必须指定键名称", "Key");
72             }
73 
74             int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.inipath);
75             if (i != 0)
76             {
77                 Value = temp.ToString();
78             }
79             temp = null;
80         }
81 
82 
83         /// <summary> 
84         /// 验证文件是否存在 
85         /// </summary> 
86         /// <returns>布尔值</returns> 
87         public bool ExistINIFile()
88         {
89             return File.Exists(inipath);
90         }
91     }
92 
93 }

使用方法

 1             iniClass ini = new iniClass("./parameter.ini");
 2             ini.IniWriteValue("登入详情", "用户名", "test");
 3             ini.IniWriteValue("登入详情", "中文账户", "password");
 4 
 5             ini.IniWriteValue("记录信息", "ReceiveAutoReturn", "1");
 6             ini.IniWriteValue("记录信息", "FixedCheckSum", "14");
 7 
 8             if (ini.ExistINIFile())
 9             {
10                 string value=null;
11                ini.IniReadValue("登入详情", "用户名",ref value);
12                 string value2 = null;
13                 ini.IniReadValue("记录信息", "ReceiveAutoReturn", ref value2);
14                 
15             }

原文地址:https://www.cnblogs.com/sclu/p/13140514.html