• 操作ini配置文件设计一个最基本的可视化数据库系统


    对于很多小项目来说,不需要搭建专门的数据库系统(例如用SQLite搭建本地数据库),这时可以用ini配置文件实现一个最基本的数据库,实现数据库最基本的增删改查功能。

    ini配置文件的用法参考我以前写的文章:http://www.cnblogs.com/xh6300/p/5895759.html

    这种配置文件的结构如下:

    [section1]

    key1=value1

    key2=value2

    [section2]

    key3=value3

    key4=value4

    ……

    具体配置文件如下:

    如果要想让这个数据库可视化,在winform中用若干个ComboBox控件是合适的选择。简略界面如下:

    要达到这一目的需要实现的功能如下:

    ① 需要将广东省、江苏省、北京市这3个section添加到cmb_address控件的Items中。

    ② 当选择不同的section时,需将该section名下所有条目的key值添加到cmb_name控件的Items中;当切换cmb_address的选项时,cmb_name的内容也会随之切换。

    ③ 当点击“删除”时,将删除当前section名下选定的那条“key=value”记录。

    ④ 当点击“修改”时,将使用tbx_value的值修改当前选中的section和key所对应的value值。

    至于数据库的“增”和“查”功能,那是ini配置文件的最基本的操作,用对应的SetValue()和GetValue()方法就能轻易实现,在此不再赘述。

    1、将所有section添加到一个List列表中的代码实现:

     1 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 }

     注:上面的完整代码为了减少代码量便于阅读,有些功能的实现写得不够完善,具体使用的时候需要自行改进。

  • 相关阅读:
    自动化测试项目实战训练【广州8月】
    RFT基础使用手册
    TestComplete自动化测试实战训练【6月11、12号】
    Jubula Eclipse开源功能测试工具
    网络管理自动化测试应用
    IBM RFT自动化测试实战课程
    GUI自动化测试原理剖析—JAVA测试篇
    简易自动化测试设计之(一) 基于RFT的自动化测试层次
    录制,到底给我们带来了什么?
    IBM Rational Functional Tester(RFT) 自动化测试框架ITCL
  • 原文地址:https://www.cnblogs.com/xh6300/p/7482473.html
Copyright © 2020-2023  润新知