C#修改INI文件时,只会新增新节点。读取节点都为空问题

解决办法:

用记事本打开INI文件,点击菜单栏文件-->另存为-->编码选择UTF-8,然后保存就可以了。

 

C#操作INI工具类:

 

 

 

  1 using System;
  2 using System.Runtime.InteropServices;
  3 using System.Text;
  4 using System.IO;
  5 using System.Collections;
  6 using System.Collections.Generic;
  7 
  8 namespace Common
  9 {
 10     /// <summary>
 11     /// INI文件读写类。  
 12     /// </summary>
 13     public class INIFile
 14     {
 15         public string path;
 16 
 17         public INIFile(string INIPath)
 18         {
 19             path = INIPath;
 20         }
 21 
 22         [DllImport("kernel32")]
 23         private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
 24 
 25         [DllImport("kernel32")]
 26         private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
 27 
 28 
 29         [DllImport("kernel32")]
 30         private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal, int size, string filePath);
 31 
 32         [DllImport("kernel32.dll")]
 33         private static extern int GetPrivateProfileInt(string lpAppName, string lpKeyName, int nDefault, string lpFileName);
 34 
 35         [DllImport("kernel32.dll", EntryPoint = "GetPrivateProfileSectionNames", CharSet = CharSet.Ansi)]
 36         private static extern int GetPrivateProfileSectionNames(IntPtr lpszReturnBuffer, int nSize, string filePath);
 37 
 38         [DllImport("KERNEL32.DLL ", EntryPoint = "GetPrivateProfileSection", CharSet = CharSet.Ansi)]
 39         private static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string filePath);
 40 
 41         /// <summary>
 42         /// 写INI文件
 43         /// </summary>
 44         /// <param name="Section"></param>
 45         /// <param name="Key"></param>
 46         /// <param name="Value"></param>
 47         public void IniWriteValue(string Section, string Key, string Value)
 48         {
 49             WritePrivateProfileString(Section, Key, Value, this.path);
 50         }
 51 
 52         /// <summary>
 53         /// 读取INI文件
 54         /// </summary>
 55         /// <param name="Section"></param>
 56         /// <param name="Key"></param>
 57         /// <returns></returns>
 58         public string IniReadValue(string Section, string Key)
 59         {
 60             StringBuilder temp = new StringBuilder(1024);
 61             int i = GetPrivateProfileString(Section, Key, "", temp, 1024, this.path);
 62             return temp.ToString();
 63         }
 64 
 65         public string IniReadValue(string Section, string Key, int size)
 66         {
 67             StringBuilder temp = new StringBuilder(size);
 68             int i = GetPrivateProfileString(Section, Key, "", temp, size, this.path);
 69             return temp.ToString();
 70         }
 71 
 72         public int ReadInt(string Section, string Key)
 73         {
 74             int def = 0;
 75             return GetPrivateProfileInt(Section, Key, def, this.path);
 76         }
 77 
 78         public byte[] IniReadValues(string section, string key)
 79         {
 80             byte[] temp = new byte[1024];
 81             int i = GetPrivateProfileString(section, key, "", temp, 1024, this.path);
 82             return temp;
 83 
 84         }
 85 
 86 
 87         /// <summary>
 88         /// 删除ini文件下所有段落
 89         /// </summary>
 90         public void ClearAllSection()
 91         {
 92             IniWriteValue(null, null, null);
 93         }
 94         /// <summary>
 95         /// 删除ini文件下personal段落下的所有键
 96         /// </summary>
 97         /// <param name="Section"></param>
 98         public void ClearSection(string Section)
 99         {
100             IniWriteValue(Section, null, null);
101         }
102 
103         /// <summary>
104         /// 返回指定配置文件下的节名称列表
105         /// </summary>
106         /// <returns></returns>
107         public List<string> GetAllSectionNames()
108         {
109             List<string> sectionList = new List<string>();
110             int MAX_BUFFER = 32767;
111             IntPtr pReturnedString = Marshal.AllocCoTaskMem(MAX_BUFFER);
112             int bytesReturned = GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, this.path);
113             if (bytesReturned != 0)
114             {
115                 string local = Marshal.PtrToStringAnsi(pReturnedString, (int)bytesReturned).ToString();
116                 Marshal.FreeCoTaskMem(pReturnedString);
117                 sectionList.AddRange(local.Substring(0, local.Length - 1).Split('\0'));
118             }
119             return sectionList;
120         }
121 
122         /// <summary>
123         /// 得到某个节点下面所有的key和value组合
124         /// </summary>
125         /// <param name="section">指定的节名称</param>
126         /// <param name="keys">Key数组</param>
127         /// <param name="values">Value数组</param>
128         /// <param name="path">INI文件路径</param>
129         /// <returns></returns>
130         public Hashtable GetAllKeyValues(string section)
131         {
132             Hashtable ht = new Hashtable();
133 
134             byte[] b = new byte[65535];//配置节下的所有信息
135             GetPrivateProfileSection(section, b, b.Length, this.path);
136             string s = System.Text.Encoding.Default.GetString(b);//配置信息
137             string[] tmp = s.Split((char)0);//Key\Value信息
138             List<string> result = new List<string>();
139             foreach (string r in tmp)
140             {
141                 if (r != string.Empty)
142                     result.Add(r);
143             }
144 
145             for (int i = 0; i < result.Count; i++)
146             {
147                 string[] item = result[i].Split(new char[] { '=' });//Key=Value格式的配置信息
148                 string key = item[0].Trim();
149 
150                 //Value字符串中含有=的处理,
151                 //一、Value加"",先对""处理
152                 //二、Key后续的都为Value
153                 if (item.Length > 2)
154                 {
155                     ht.Add(key, result[i].Substring(key.Length + 1));
156                 }
157                 if (item.Length == 2)//Key=Value
158                 {
159                     ht.Add(key, item[1].Trim());
160                 }
161                 else if (item.Length == 1)//Key=
162                 {
163                     ht.Add(key, "");
164                 }
165                 else if (item.Length == 0)
166                 {
167                     //ht.Add("", "");
168                 }
169             }
170             return ht;
171         }
172 
173     }
174 
175 }
View Code

 

posted @ 2022-09-13 15:07  ziff123  阅读(139)  评论(0编辑  收藏  举报