MyToolkit函式庫加入Ini檔案讀寫功能

image

 

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5:  
   6: using System.Runtime.InteropServices;
   7: namespace MyToolkit
   8: {
   9:    
  10:     public class IniFileTool
  11:     {
  12:         public string path;
  13:         [DllImport("kernel32")]
  14:         private static extern long WritePrivateProfileString(string section,
  15:             string key, string val, string filePath);
  16:         [DllImport("kernel32")]
  17:         private static extern int GetPrivateProfileString(string section,
  18:                  string key, string def, StringBuilder retVal,
  19:             int size, string filePath);
  20:         /// <summary>
  21:         /// INIFile Constructor.
  22:         /// </summary>
  23:         /// <PARAM name="INIPath"></PARAM>
  24:         public IniFileTool(string INIPath)
  25:         {
  26:             path = INIPath;
  27:         }
  28:         /// <summary>
  29:         /// Write Data to the INI File
  30:         /// </summary>
  31:         /// <PARAM name="Section"></PARAM>
  32:         /// Section name
  33:         /// <PARAM name="Key"></PARAM>
  34:         /// Key Name
  35:         /// <PARAM name="Value"></PARAM>
  36:         /// Value Name
  37:         public void IniWriteValue(string Section, string Key, string Value)
  38:         {
  39:             WritePrivateProfileString(Section, Key, Value, this.path);
  40:         }
  41:  
  42:         /// <summary>
  43:         /// Read Data Value From the Ini File
  44:         /// </summary>
  45:         /// <PARAM name="Section"></PARAM>
  46:         /// <PARAM name="Key"></PARAM>
  47:         /// <PARAM name="Path"></PARAM>
  48:         /// <returns></returns>
  49:         public string IniReadValue(string Section, string Key)
  50:         {
  51:             StringBuilder temp = new StringBuilder(255);
  52:             int i = GetPrivateProfileString(Section, Key, "", temp,
  53:                                             255, this.path);
  54:             return temp.ToString();
  55:  
  56:         }
  57:     }
  58: }

-------------------------------------------------------------------------------------------------------------------------------------

撰寫一個Windows Form測試剛才的Ini函式庫

image

加入MyToolkit.dll

image

image

寫入

   1: private void button1_Click(object sender, EventArgs e)
   2: {
   3:     DateTimeTool dt = new DateTimeTool();    
   4:     dt.tic();// 計時開始   
   5:     string sAppDir = Application.StartupPath;
   6:     string sFilename = textBox1.Text;
   7:     string sFilepath = sAppDir + "\\" + sFilename;
   8:     IniFileTool ini = new IniFileTool(sFilepath);
   9:     string section = textBox2.Text;
  10:     string key = textBox3.Text;
  11:     string value = textBox4.Text;
  12:     ini.IniWriteValue(section, key, value);
  13:     int day, hour, min, sec, msec;
  14:     dt.toc(out day, out hour, out min, out sec, out msec);  // 計時結束 
  15:     this.Text = string.Format("{0}時{1}時{2}分{3}秒{4}毫秒", day, hour, min, sec, msec);
  16: }

image

讀取

   1: private void button2_Click(object sender, EventArgs e)
   2: {
   3:     DateTimeTool dt = new DateTimeTool();
   4: dt.tic();        // 計時開始   
   5: string sAppDir = Application.StartupPath;
   6: string sFilename = textBox1.Text;
   7: string sFilepath = sAppDir + "\\" + sFilename;
   8: IniFileTool ini = new IniFileTool(sFilepath);
   9: string section = textBox2.Text;
  10: string key = textBox3.Text;
  11: string value;
  12: value = ini.IniReadValue(section, key);
  13: this.Text = value;
  14: int day, hour, min, sec, msec;
  15: dt.toc(out day, out hour, out min, out sec, out msec);  // 計時結束 
  16: MessageBox.Show( string.Format("{0}時{1}時{2}分{3}秒{4}毫秒", day, hour, min, sec, msec) );}

image

參考資料:

  1. An INI file handling class using C#
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()