发布网友 发布时间:2022-05-02 06:19
共2个回答
懂视网 时间:2022-05-02 10:41
public void GetAllSections(string iniFileName, out List<string> SectionsList) 2 { 3 SectionsList = new List<string>(); 4 string fileText = ""; 5 6 try 7 { 8 StreamReader sr = new StreamReader(iniFileName, Encoding.Default); 9 fileText = sr.ReadToEnd(); 10 sr.Close();//这一句一定要加 11 12 } 13 catch (Exception) 14 { 15 MessageBox.Show("文件" + iniFileName + "不存在或错误!"); 16 return; 17 } 18 19 20 try 21 { 22 string[] _split = fileText.Split(new Char[] { ‘[‘ }); 23 24 //由于上面是用“[”分割的,因此下面的i从1开始 25 for (int i = 1; i < _split.Count(); i++) 26 { 27 string[] split_temp = _split[i].Split(new Char[] { ‘]‘ }); 28 29 if (split_temp[0].Trim() != "") 30 { 31 SectionsList.Add(split_temp[0].Trim()); 32 } 33 } 34 35 } 36 catch (Exception) 37 { 38 //这里我就什么也不做,这是坠吼的! 39 } 40 }
2、获得指定section下的所有key值,并将它们添加到List列表中的代码实现:
1 public void GetAllKeys(string iniFileName, out List<string> keysList, int Section_index) 2 { 3 keysList = new List<string>(); 4 string fileText = ""; 5 6 try 7 { 8 StreamReader sr = new StreamReader(iniFileName, Encoding.Default); 9 fileText = sr.ReadToEnd(); 10 sr.Close();//这一句一定要加 11 12 } 13 catch (Exception) 14 { 15 MessageBox.Show("文件" + iniFileName + "不存在或错误!"); 16 return; 17 } 18 19 try 20 { 21 //先把所有的[section]去掉 22 string[] _split = fileText.Split(new Char[] { ‘[‘ }); 23 string[] split_temp = _split[Section_index + 1].Split(new Char[] { ‘]‘ }); 24 25 string text_key_value = split_temp[1];//获得section索引为Section_index下的key和value的文本 26 27 text_key_value = text_key_value.Replace(" ", "?");//将换行符换成?,便于后面进一步分割 28 string[] KeyLists_temp = text_key_value.Split(new char[] { ‘?‘ });//按‘?’分割,注意可能会有空字符串数组元素 29 30 //将分割出来的每一个非空key加入到keysList里面去 31 for (int i = 1; i < KeyLists_temp.Count(); i++) 32 { 33 if (KeyLists_temp[i].Trim() != "") 34 { 35 string _key = KeyLists_temp[i].Split(new char[] { ‘=‘ })[0]; 36 if (_key.Trim() != "") 37 { 38 keysList.Add(_key.Trim()); 39 } 40 } 41 } 42 43 } 44 catch (Exception) 45 { 46 MessageBox.Show("文件" + iniFileName + "不存在或错误!"); 47 } 48 49 50 51 }
3、将List中的值添加到ComboBox控件的Item中的代码实现:
1 public void AddList_ToCmb(List<string> _infoList, ComboBox _cmbBox) 2 { 3 for (int i = 0; i < _infoList.Count; i++) 4 { 5 _cmbBox.Items.Add(_infoList[i]); 6 } 7 }
整个程序完整代码如下:
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Runtime.InteropServices; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.Windows.Forms.VisualStyles; 9 10 namespace test1 11 { 12 public partial class CustomManage : Form 13 { 14 public CustomManage() 15 { 16 InitializeComponent(); 17 this.StartPosition = FormStartPosition.CenterScreen; 18 } 19 20 21 private void customManage_Load(object sender, EventArgs e) 22 { 23 List<string> _listSections = new List<string>(); 24 List<string> _listKeys = new List<string>(); 25 26 GetAllSections("companyInfo.ini", out _listSections); 27 AddList_ToCmb(_listSections, cmb_address); 28 29 GetAllKeys("companyInfo.ini", out _listKeys, 0); 30 AddList_ToCmb(_listKeys, cmb_name); 31 } 32 33 34 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 35 { 36 //清空原有数据 37 int index = cmb_address.SelectedIndex; 38 cmb_name.Items.Clear(); 39 cmb_name.Text = ""; 40 41 //重新获得对应section下的key值的列表,并添加到cmb_Name中 42 List<string> _nameList = new List<string>(); 43 GetAllKeys("companyInfo.ini", out _nameList, index); 44 AddList_ToCmb(_nameList, cmb_name); 45 } 46 47 48 //删除条目 49 private void button4_Click(object sender, EventArgs e) 50 { 51 string _fileName = "companyInfo.ini"; 52 try 53 { 54 StreamReader sr = new StreamReader(_fileName, Encoding.Default); 55 string str = sr.ReadToEnd(); 56 sr.Close(); 57 58 //该删除方式有缺陷,要求“=”两侧不能有空格,否则会删除失败(以后可以考虑用正则匹配一下) 59 string _deleteItem = cmb_name.Text + "=" + GetValue(_fileName, cmb_address.Text, cmb_name.Text); 60 str = str.Replace(_deleteItem, ""); 61 StreamWriter sw = new StreamWriter(_fileName, false, Encoding.Default); 62 63 sw.Write(str); 64 sw.Flush(); 65 sw.Close(); 66 67 MessageBox.Show("删除成功!"); 68 69 } 70 catch (Exception) 71 { 72 MessageBox.Show("文件" + _fileName + "不存在或错误!"); 73 } 74 } 75 76 77 //修改条目 78 private void button1_Click(object sender, EventArgs e) 79 { 80 string _fileName = "companyInfo.ini"; 81 SetValue(_fileName, cmb_address.Text.Trim(), cmb_name.Text.Trim(), tbx_value.Text.Trim()); 82 83 MessageBox.Show("修改成功!"); 84 } 85 86 /// <summary> 获得所有的section 87 /// 88 /// </summary> 89 /// <param name="iniFileName"></param> 90 /// <param name="SectionsList"></param> 91 public void GetAllSections(string iniFileName, out List<string> SectionsList) 92 { 93 SectionsList = new List<string>(); 94 string fileText = ""; 95 96 try 97 { 98 StreamReader sr = new StreamReader(iniFileName, Encoding.Default); 99 fileText = sr.ReadToEnd(); 100 sr.Close();//这一句一定要加 101 102 } 103 catch (Exception) 104 { 105 MessageBox.Show("文件" + iniFileName + "不存在或错误!"); 106 return; 107 } 108 109 110 try 111 { 112 string[] _split = fileText.Split(new Char[] { ‘[‘ }); 113 114 //由于上面是用“[”分割的,因此下面的i从1开始 115 for (int i = 1; i < _split.Count(); i++) 116 { 117 string[] split_temp = _split[i].Split(new Char[] { ‘]‘ }); 118 119 if (split_temp[0].Trim() != "") 120 { 121 SectionsList.Add(split_temp[0].Trim()); 122 } 123 } 124 125 } 126 catch (Exception) 127 { 128 //这里我就什么也不做,这是坠吼的! 129 } 130 } 131 132 133 /// <summary> 获得指定section下的所有key值 134 /// 135 /// </summary> 136 /// <param name="iniFileName"></param> 137 /// <param name="keysList"></param> 138 /// <param name="Section_index"></param> 139 public void GetAllKeys(string iniFileName, out List<string> keysList, int Section_index) 140 { 141 keysList = new List<string>(); 142 string fileText = ""; 143 144 try 145 { 146 StreamReader sr = new StreamReader(iniFileName, Encoding.Default); 147 fileText = sr.ReadToEnd(); 148 sr.Close();//这一句一定要加 149 150 } 151 catch (Exception) 152 { 153 MessageBox.Show("文件" + iniFileName + "不存在或错误!"); 154 return; 155 } 156 157 try 158 { 159 //先把所有的[section]去掉 160 string[] _split = fileText.Split(new Char[] { ‘[‘ }); 161 string[] split_temp = _split[Section_index + 1].Split(new Char[] { ‘]‘ }); 162 163 string text_key_value = split_temp[1];//获得section索引为Section_index下的key和value的文本 164 165 text_key_value = text_key_value.Replace(" ", "?");//将换行符换成?,便于后面进一步分割 166 string[] KeyLists_temp = text_key_value.Split(new char[] { ‘?‘ });//按‘?’分割,注意可能会有空字符串数组元素 167 168 //将分割出来的每一个非空key加入到keysList里面去 169 for (int i = 1; i < KeyLists_temp.Count(); i++) 170 { 171 if (KeyLists_temp[i].Trim() != "") 172 { 173 string _key = KeyLists_temp[i].Split(new char[] { ‘=‘ })[0]; 174 if (_key.Trim() != "") 175 { 176 keysList.Add(_key.Trim()); 177 } 178 } 179 } 180 181 } 182 catch (Exception) 183 { 184 MessageBox.Show("文件" + iniFileName + "不存在或错误!"); 185 } 186 } 187 188 189 /// <summary> 将字符串List中的值添加到ComboBox控件的Item中。 190 /// 191 /// </summary> 192 /// <param name="_infoList"></param> 193 /// <param name="_cmbBox"></param> 194 public void AddList_ToCmb(List<string> _infoList, ComboBox _cmbBox) 195 { 196 for (int i = 0; i < _infoList.Count; i++) 197 { 198 _cmbBox.Items.Add(_infoList[i]); 199 } 200 } 201 202 203 #region 读、写ini配置文件的方法 204 205 [DllImport("kernel32")] 206 private static extern int GetPrivateProfileString(string section, string key, string defVal, 207 StringBuilder retVal, int size, string filePath); 208 209 [DllImport("kernel32")] 210 private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); 211 212 213 public string GetValue(string parasFileName, string section, string key) 214 { 215 var sb = new StringBuilder(255); 216 string strPath = Environment.CurrentDirectory + "\" + parasFileName; 217 int i = GetPrivateProfileString(section, key, "该文件不存在", sb, 255, strPath); 218 return sb.ToString(); 219 } 220 221 222 public void SetValue(string parasFileName, string section, string key, string value) 223 { 224 //获得当前路径,当前是在Debug路径下 225 string strPath = Environment.CurrentDirectory + "\" + parasFileName; 226 WritePrivateProfileString(section, key, value, strPath); 227 } 228 229 #endregion 230 231 232 233 234 } 235 }
注:上面的完整代码为了减少代码量便于阅读,有些功能的实现写得不够完善,具体使用的时候需要自行改进。
操作ini配置文件设计一个最基本的可视化数据库系统
标签:region 存在 目的 sage generic default bug styles har
热心网友 时间:2022-05-02 07:49
INI是微软Windows操作系统中的文件扩展名。这些字母表示初始化。正如该术语所表示的,INI文件被用来对操作系统或特定程序初始化或进行参数设置。