<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      XuGang

      記錄一個(gè)程序員的成長(zhǎng)

       

      我的INI 配置文件讀寫動(dòng)態(tài)庫

       

      工作需要,今天上午花時(shí)間看了一下INI 配置文件的相關(guān)文章,并添加到項(xiàng)目中。

      后來想想,干脆封裝成DLL 動(dòng)態(tài)庫,并提供給大家使用,順便更新一下博客。^_^

       

      INI 配置文件的格式   

      在早期的Windows 桌面系統(tǒng)中,主要是用INI 文件作為系統(tǒng)的配置文件,從Win95 以后開始轉(zhuǎn)向使用注冊(cè)表,但是還有很多系統(tǒng)配置是使用INI 文件的。其實(shí),INI 文件就是簡(jiǎn)單的text 文件,只不過這種txt 文件要遵循一定的INI 文件格式。

      “.ini” 就是英文 “initialization” 的頭三個(gè)字母的縮寫;當(dāng)然INI file 的后綴名也不一定是".ini"也可以是".cfg",".conf ”或者是".txt"。

      經(jīng)典格式:

      INI文件的格式很簡(jiǎn)單,最基本的三個(gè)要素是:parameters,sections 和 comments。

      什么是 parameters?

      INI所包含的最基本的“元素”就是parameter;每一個(gè)parameter都有一個(gè)name和一個(gè)value,name和value是由等號(hào)“=”隔開。name在等號(hào)的左邊。

      如:  name = value

      什么是sections ?

      所有的parameters都是以sections為單位結(jié)合在一起的。所有的section名稱都是獨(dú)占一行,并且sections名字都被方括號(hào)包圍著 ([ and ])。在section聲明后的所有parameters都是屬于該section。對(duì)于一個(gè)section沒有明顯的結(jié)束標(biāo)志符,一個(gè)section的 開始就是上一個(gè)section的結(jié)束,或者是end of the file。Sections一般情況下不能被nested,當(dāng)然特殊情況下也可以實(shí)現(xiàn)sections的嵌套。

      section 如:   [section]

       什么是 comments ?

      在INI 文件中注釋語句是以分號(hào)“;”開始的。所有的注釋語句不管多長(zhǎng)都是獨(dú)占一行直到結(jié)束的。在分號(hào)和行結(jié)束符之間的所有內(nèi)容都是被忽略的。

      注釋如:   ;comments text

       

      當(dāng)然,上面講的都是最經(jīng)典的INI文件格式,隨著使用的需求INI文件的格式也出現(xiàn)了很多變種;

      變種格式:請(qǐng)參考:http://en.wikipedia.org/wiki/INI_file

       

      我的 INI 配置文件讀寫動(dòng)態(tài)庫

      其實(shí)就是調(diào)用了kernel32.dll 中的 WritePrivateProfileString 和 GetPrivateProfileString 函數(shù)。

      kernel32.dll是Windows 9x/Me 中非常重要的32位動(dòng)態(tài)鏈接庫文件,屬于內(nèi)核級(jí)文件。它控制著系統(tǒng)的內(nèi)存管理、數(shù)據(jù)的輸入輸出操作和中斷處理。
       
      INI 配置文件讀寫動(dòng)態(tài)庫 INIHelper.dll 的源碼很簡(jiǎn)單,代碼如下:
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Runtime.InteropServices;
      using System.IO;

      namespace INIHelper
      {
      public class INIFileHelper
      {
      private string strFileName = ""; //INI文件名
      private string strFilePath = "";//獲取INI文件路徑

      public INIFileHelper()
      {
      strFileName = "Config.ini"; //INI文件名
      //方法1獲取INI文件路徑
      strFilePath = Directory.GetCurrentDirectory() + "\\" + strFileName;
      //方法2:獲取INI文件路徑
      //strFilePath = Path.GetFullPath(".\\") + strFileName;
      }

      public INIFileHelper(string FileName)
      {
      strFileName = FileName; //INI文件名
      //
      獲取INI文件路徑
      strFilePath = Directory.GetCurrentDirectory() + "\\" + strFileName;
      }

      public INIFileHelper(string FullPath, string FileName)
      {
      strFileName = FileName; //INI文件名
      strFilePath = FullPath + "\\" + strFileName;//獲取INI文件路徑
      }

      /// <summary>
      /// 寫入INI文件
      /// </summary>
      /// <param name="section">節(jié)點(diǎn)名稱[如[TypeName]]</param>
      /// <param name="key"></param>
      /// <param name="val"></param>
      /// <param name="filepath">文件路徑</param>
      /// <returns></returns>
      [DllImport("kernel32")]
      public static extern long WritePrivateProfileString(string section, string key, string val, string filepath);

      /// <summary>
      /// 寫入
      /// </summary>
      /// <param name="sectionName">section 節(jié)點(diǎn)名稱</param>
      /// <param name="key">key 值</param>
      /// <param name="value">value 值</param>
      public void Write(string sectionName, string key, string value)
      {
      try
      {
      //根據(jù)INI文件名設(shè)置要寫入INI文件的節(jié)點(diǎn)名稱
      //此處的節(jié)點(diǎn)名稱完全可以根據(jù)實(shí)際需要進(jìn)行配置
      strFileName = Path.GetFileNameWithoutExtension(strFilePath);
      INIFileHelper.WritePrivateProfileString(sectionName, key, value, strFilePath);
      }
      catch
      {
      throw new Exception("配置文件不存在或權(quán)限不足導(dǎo)致無法寫入");
      }
      }

      /// <summary>
      /// 寫入默認(rèn)節(jié)點(diǎn)"FileConfig"下的相關(guān)數(shù)據(jù)
      /// </summary>
      /// <param name="key">key 值</param>
      /// <param name="value">value 值</param>
      public void Write(string key, string value)
      {
      // section 節(jié)點(diǎn)名稱使用默認(rèn)值:"FileConfig"
      Write("FileConfig", key, value);
      }

      /// <summary>
      /// 讀取INI文件
      /// </summary>
      /// <param name="section">節(jié)點(diǎn)名稱</param>
      /// <param name="key"></param>
      /// <param name="def"></param>
      /// <param name="retval">stringbulider對(duì)象</param>
      /// <param name="size">字節(jié)大小</param>
      /// <param name="filePath">文件路徑</param>
      /// <returns></returns>
      [DllImport("kernel32")]
      public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);

      /// <summary>
      /// 讀取
      /// </summary>
      /// <param name="sectionName">section 節(jié)點(diǎn)名稱</param>
      /// <param name="key">key 值</param>
      /// <returns>value 值</returns>
      public string Read(string sectionName, string key)
      {
      if (File.Exists(strFilePath)) //讀取時(shí)先要判讀INI文件是否存在
      {
      strFileName = Path.GetFileNameWithoutExtension(strFilePath);
      //return ContentValue(strFileName, key);
      StringBuilder outValue = new StringBuilder(1024);
      INIFileHelper.GetPrivateProfileString(sectionName, key, "", outValue, 1024, strFilePath);
      return outValue.ToString();
      }
      else
      {
      throw new Exception("配置文件不存在");
      }
      }

      /// <summary>
      /// 讀取默認(rèn)節(jié)點(diǎn)"FileConfig"下的相關(guān)數(shù)據(jù)
      /// </summary>
      /// <param name="key">key 值</param>
      /// <returns>value 值</returns>
      public string Read(string key)
      {
      // section 節(jié)點(diǎn)名稱使用默認(rèn)值:"FileConfig"
      return Read("FileConfig", key);
      }

      }
      }
       

      對(duì) INIHelper.dll 動(dòng)態(tài)庫的使用和測(cè)試,代碼如下:

      // 獲取INI文件路徑
      // private string strFilePath = Application.StartupPath + "\\FileConfig.ini";

      //寫入
      private void button1_Click(object sender, EventArgs e)
      {
          //test1(WinForm 測(cè)試)
          string strFilePath = "Config.ini";//獲取INI文件路徑
          INIFileHelper file1 = new INIFileHelper(strFilePath);
          file1.Write(label1.Text, textBox1.Text);
          file1.Write(label2.Text, textBox2.Text);
          MessageBox.Show("test1 寫入完畢");

          //test2
          INIFileHelper file2 = new INIFileHelper();
          file2.Write("xugang""http://xugang.cnblogs.com");
          file2.Write("hobby""@#$%^&*()");
          MessageBox.Show("test2 寫入完畢");

          //test3
          INIFileHelper file3 = new INIFileHelper("newConfig.ini");
          file3.Write("NewSection""xugang""http://xugang.cnblogs.com");
          file3.Write("NewSection""hobby""@#$%^&*()");
          MessageBox.Show("test3 寫入完畢");

          //test4
          string strPath = Application.StartupPath; //文件路徑
          string strName = "xxx.ini";//INI文件名稱

          INIFileHelper file4 = new INIFileHelper(strPath, strName);
          file4.Write("NewSection""xugang""http://xugang.cnblogs.com");
          file4.Write("NewSection""hobby""@#$%^&*()");
          MessageBox.Show("test4 寫入完畢");
      }

      //讀取
      private void button2_Click(object sender, EventArgs e)
      {
          //test1(WinForm 測(cè)試)
          string strFilePath = "Config.ini";//獲取INI文件路徑
          INIFileHelper file1 = new INIFileHelper(strFilePath);
          StringBuilder str = new StringBuilder();
          str.AppendLine(file1.Read(label1.Text));
          str.AppendLine(file1.Read(label2.Text));
          MessageBox.Show(str.ToString());

          //test2
          INIFileHelper file2 = new INIFileHelper();
          MessageBox.Show(file2.Read("xugang") +"   "+file2.Read("hobby"));

          //test3
          INIFileHelper file3 = new INIFileHelper("newConfig.ini");
          MessageBox.Show( file3.Read("NewSection""xugang")
                 + "   " + file3.Read("NewSection""hobby"));

          //test4
          string strPath = Application.StartupPath; //文件路徑
          string strName = "xxx.ini";//INI文件名稱

          INIFileHelper file4 = new INIFileHelper(strPath, strName);
          MessageBox.Show(file4.Read("NewSection""xugang")
                + "   " + file4.Read("NewSection""hobby"));
      }
       

      參考來源:

      INI 配置文件的格式

      INI 格式文件操作

       

      源碼下載

      posted on 2012-03-21 13:44  鋼鋼  閱讀(6041)  評(píng)論(4)    收藏  舉報(bào)

      導(dǎo)航

      主站蜘蛛池模板: 亚洲国产成人久久综合区| 伊人精品无码av一区二区三区| 国产精品一区在线蜜臀| 色悠悠国产在线视频一线| 国产成人免费一区二区三区| 高清自拍亚洲精品二区| 激情综合色区网激情五月| 永久免费无码av在线网站| 亚洲中文字幕av天堂| 国产一区二区黄色在线观看| 亚洲经典在线中文字幕| 国产免费无遮挡吃奶视频| 国产精品亚洲一区二区在| 亚洲国产午夜精品福利| 新田县| 激情动态图亚洲区域激情| 日韩人妻无码一区二区三区99| 欧美日韩一线| 亚洲一本大道在线| 激情国产一区二区三区四| 国产亚洲av手机在线观看| 天堂av资源在线免费| 久久涩综合一区二区三区| 国产麻豆精品一区二区三区v视界| 精品久久精品午夜精品久久 | 亚洲综合国产激情另类一区| 国产熟睡乱子伦午夜视频| 鲁丝片一区二区三区免费| 国产一区二区三区不卡视频| 久久综合狠狠综合久久| 石家庄市| 99久久精品久久久久久婷婷| 国产熟睡乱子伦视频在线播放| 板桥市| 国产精品毛片av999999| 国产综合视频一区二区三区| 韩国免费a级毛片久久| 九九视频热最新在线视频| 国产精品自在拍首页视频8| 大姚县| 成人午夜免费无码视频在线观看|