INI文件操作类

  1 public class INIFileUtil
  2     {
  3         /// <summary>
  4         /// INI文件地址
  5         /// </summary>
  6         private string path;
  7 
  8         /// <summary>
  9         /// 初始化
 10         /// </summary>
 11         /// <param name="INIPath">路径</param>
 12         private INIFileUtil(string INIPath)
 13         {
 14             this.path = INIPath;
 15         }
 16 
 17         public static INIFileUtil LoadIniFile(string iniFile)
 18         {
 19             return new INIFileUtil(iniFile);
 20         }
 21         /// <summary>
 22         /// 构造函数
 23         /// </summary>
 24         private INIFileUtil()
 25         {
 26         }
 27 
 28         [DllImport("kernel32", EntryPoint = "WritePrivateProfileString")]
 29         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
 30         [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
 31         private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
 32         [DllImport("kernel32", EntryPoint = "GetPrivateProfileString")]
 33         private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);
 34         [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSectionNames", CharSet = CharSet.Ansi)]
 35         private static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, int nSize, string filePath);
 36         [DllImport("KERNEL32.DLL ", EntryPoint = "GetPrivateProfileSection", CharSet = CharSet.Ansi)]
 37         private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string filePath);
 38 
 39 
 40         /// <summary>
 41         /// 写INI文件
 42         /// </summary>
 43         /// <param name="Section">分组节点</param>
 44         /// <param name="Key">关键字</param>
 45         /// <param name="Value"></param>
 46         public void IniWriteValue(string Section, string Key, string Value)
 47         {
 48             INIFileUtil.WritePrivateProfileString(Section, Key, Value, this.path);
 49         }
 50 
 51         /// <summary>
 52         ///  读取INI文件
 53         /// </summary>
 54         /// <param name="Section">分组节点</param>
 55         /// <param name="Key">关键字</param>
 56         /// <returns></returns>
 57         public string IniReadValue(string Section, string Key)
 58         {
 59             StringBuilder stringBuilder = new StringBuilder(255);
 60             INIFileUtil.GetPrivateProfileString(Section, Key, "", stringBuilder, 255, this.path);
 61             return stringBuilder.ToString();
 62         }
 63 
 64         /// <summary>
 65         /// 读取INI文件
 66         /// </summary>
 67         /// <param name="section">分组节点</param>
 68         /// <param name="key">关键字</param>
 69         /// <returns></returns>
 70         public byte[] IniReadValues(string section, string key)
 71         {
 72             byte[] array = new byte[255];
 73             INIFileUtil.GetPrivateProfileString(section, key, "", array, 255, this.path);
 74             return array;
 75         }
 76 
 77         /// <summary>
 78         /// 删除ini文件下所有段落
 79         /// </summary>
 80         public void ClearAllSection()
 81         {
 82             this.IniWriteValue(null, null, null);
 83         }
 84 
 85         /// <summary>
 86         /// 删除ini文件下指定段落下的所有键
 87         /// </summary>
 88         /// <param name="Section">指定段</param>
 89         public void ClearSection(string Section)
 90         {
 91             this.IniWriteValue(Section, null, null);
 92         }
 93 
 94         /// <summary>
 95         /// 读取一个ini里面所有的节
 96         /// </summary>
 97         /// <param name="sections"></param>
 98         /// <param name="path"></param>
 99         /// <returns></returns>
100         public int GetAllSectionNames(out List<string> sections)
101         {
102             int MAX_BUFFER = 32767;
103             IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER);
104             int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, this.path);
105             if (bytesReturned == 0)
106             {
107                 sections = new List<string>();
108                 return -1;
109             }
110             string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
111             Marshal.FreeCoTaskMem(pReturnedString);
112             //use of Substring below removes terminating null for split
113             sections = local.Substring(0, local.Length - 1).Split('\0').ToList<string>();
114             return 0;
115         }
116 
117         /// <summary>
118         /// 得到某个节点下面所有的key和value组合
119         /// </summary>
120         /// <param name="section"></param>
121         /// <param name="keys"></param>
122         /// <param name="values"></param>
123         /// <param name="path"></param>
124         /// <returns></returns>
125         public int GetAllKeyValues(string section, out string[] keys, out string[] values)
126         {
127             byte[] b = new byte[65535];
128 
129             GetPrivateProfileSection(section, b, b.Length, this.path);
130             string s = System.Text.Encoding.Default.GetString(b);
131             string[] tmp = s.Split((char)0);
132             ArrayList result = new ArrayList();
133             foreach (string r in tmp)
134             {
135                 if (r != string.Empty)
136                     result.Add(r);
137             }
138             keys = new string[result.Count];
139             values = new string[result.Count];
140             for (int i = 0; i < result.Count; i++)
141             {
142                 string[] item = result[i].ToString().Split(new char[] { '=' });
143                 if (item.Length == 2)
144                 {
145                     keys[i] = item[0].Trim();
146                     values[i] = item[1].Trim();
147                 }
148                 else if (item.Length == 1)
149                 {
150                     keys[i] = item[0].Trim();
151                     values[i] = "";
152                 }
153                 else if (item.Length == 0)
154                 {
155                     keys[i] = "";
156                     values[i] = "";
157                 }
158             }
159             return 0;
160         }
161     }
原文地址:https://www.cnblogs.com/refresh/p/2521464.html