ini配置文件读写-示例
类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using IniTool; 7 using System.Drawing; 8 using Utils; 9 using System.Collections; 10 11 namespace AppConfig 12 { 13 public class Config 14 { 15 private static string file = Environment.CurrentDirectory + @"\config.ini"; 16 private static Config _Config = new Config(); 17 private bool _isInit = false; 18 19 private enum Control 20 { 21 ADD, 22 READ, 23 WRITE, 24 } 25 26 public enum Sections 27 { 28 SerialConfig, 29 ActionConfig, 30 ColorConfig, 31 DrawConfig 32 } 33 34 public enum Idents 35 { 36 Baud, 37 DataBits, 38 Parity, 39 StopBits, 40 CloseAction, 41 CanvasColor, 42 GridColor, 43 PointColor, 44 LocateColor, 45 DrawRows, 46 DrawCols 47 } 48 49 public bool IsInit 50 { 51 get{ return _isInit; } 52 private set { _isInit = value; } 53 } 54 55 public class Data 56 { 57 private Sections _section; 58 private Idents _ident; 59 private string _val; 60 61 public Sections section { get { return _section; } set { _section = value; } } 62 public Idents ident { get { return _ident; } set { _ident = value; } } 63 public string val { get { return _val; } set { _val = value; } } 64 65 public Data(Sections section, Idents ident, string val) 66 { 67 this.section = section; 68 this.ident = ident; 69 this.val = val; 70 } 71 } 72 73 private List<Data> ConfigList = new List<Data>(); 74 private List<string> DataSections = new List<string>(); 75 private List<string> DataIdents = new List<string>(); 76 77 public Config() 78 { 79 80 } 81 82 public static Config Cfg 83 { 84 get { return _Config; } 85 } 86 87 public void InitConfig() 88 { 89 Console.WriteLine("配置初始化"); 90 91 //检测配置文件是否存在并加载 92 bool result = ini_RW.CheckIniFile(file); 93 //初始化数据 94 InitData(); 95 96 if (result) 97 { 98 this.IsInit = true; 99 //增加 100 AddConfig(); 101 //删除失效配置 102 UpdateConfig(); 103 //读取 104 ReadConfig(); 105 } 106 } 107 108 private void InitData() 109 { 110 //初始化串口参数 111 ConfigList.Add(new Data(Sections.SerialConfig, Idents.Baud, "9600")); 112 ConfigList.Add(new Data(Sections.SerialConfig, Idents.DataBits, "8")); 113 ConfigList.Add(new Data(Sections.SerialConfig, Idents.Parity, "None")); 114 ConfigList.Add(new Data(Sections.SerialConfig, Idents.StopBits, "One")); 115 116 //初始化关闭按钮功能 117 ConfigList.Add(new Data(Sections.ActionConfig, Idents.CloseAction, "True")); 118 119 //初始化颜色数据 120 ConfigList.Add(new Data(Sections.ColorConfig, Idents.CanvasColor, "White")); 121 ConfigList.Add(new Data(Sections.ColorConfig, Idents.GridColor, "Gray")); 122 ConfigList.Add(new Data(Sections.ColorConfig, Idents.PointColor, "DarkOrange")); 123 ConfigList.Add(new Data(Sections.ColorConfig, Idents.LocateColor, "Blue")); 124 125 //初始化绘制参数 126 ConfigList.Add(new Data(Sections.DrawConfig, Idents.DrawRows, "128")); 127 ConfigList.Add(new Data(Sections.DrawConfig, Idents.DrawCols, "128")); 128 129 } 130 131 private void AddConfig() 132 { 133 //DataSections.Clear(); 134 //DataIdents.Clear(); 135 Manage(Control.ADD); 136 } 137 138 private void UpdateConfig() 139 { 140 //检测是否有更改 141 var Sections = ini_RW.ReadSections(); 142 foreach (var section in Sections) 143 { 144 //检测节点是否存在 145 if (!DataSections.Exists(x => x == section)) 146 { 147 //不存在删除 148 ini_RW.DeleteSection(section); 149 } 150 else 151 { 152 //检测数据是否存在 153 var idents = ini_RW.ReadIdents(section); 154 foreach (var ident in idents) 155 { 156 if (!DataIdents.Exists(y => y == ident)) 157 { 158 //不存在删除 159 ini_RW.DeleteIdent(section, ident); 160 } 161 } 162 } 163 } 164 } 165 166 private void ReadConfig() 167 { 168 Manage(Control.READ); 169 } 170 171 public void WriteConfig() 172 { 173 Manage(Control.WRITE); 174 } 175 176 private void Manage(Control type) 177 { 178 (from item in ConfigList 179 select item). 180 ToList(). 181 ForEach(x => { 182 183 switch (type) 184 { 185 case Control.ADD: 186 { 187 ini_RW.AddConfigInfo(x.section.ToString(), x.ident.ToString(), x.val); 188 189 DataSections.Add(x.section.ToString()); 190 DataIdents.Add(x.ident.ToString()); 191 } 192 break; 193 case Control.READ: 194 { 195 x.val = ini_RW.ReadIni(x.section.ToString(), x.ident.ToString()); 196 } 197 break; 198 case Control.WRITE: 199 { 200 ini_RW.WriteIni(x.section.ToString(), x.ident.ToString(), x.val); 201 } 202 break; 203 } 204 }); 205 } 206 207 public Data GetConfig(Sections section, Idents ident) 208 { 209 return (from item in ConfigList 210 where (item.section == section && item.ident == ident) 211 select item).FirstOrDefault(); 212 } 213 214 215 //############################################################### 216 //private static Config _Config = null; 217 //private static object Config_Lock = new object(); 218 219 //public static Config Init() 220 //{ 221 // if (_Config == null) 222 // { 223 // lock (Config_Lock) 224 // { 225 // if (_Config == null) 226 // { 227 // _Config = new Config(); 228 // } 229 // } 230 // } 231 // return _Config; 232 //} 233 //################################################################ 234 235 } 236 }
调用:
1 //程序打开时初始化 2 Config.Cfg.InitConfig(); 3 if (!Config.Cfg.IsInit) 4 { 5 MessageBox.Show("配置文件不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); 6 } 7 8 //程序退出时保存 9 if (Config.Cfg.IsInit) 10 { 11 Config.Cfg.WriteConfig(); 12 }