1. 程式人生 > >C# 操作INI配置檔案

C# 操作INI配置檔案

重構YH是遇到這個一樣問題,就是YH前臺頁面的按鈕要求實現客戶自定義,客戶可以跟隨自己的喜好安排每個按鈕的功能,所以,在每一次客戶登陸是就需要初始化客戶自定義排版,這裡就需要用到初始化檔案——INI

 .ini 檔案是Initialization File的縮寫,即初始化檔案。是windows的系統配置檔案所採用的儲存格式,統管windows的各項配置,一般使用者就用windows提供的各項圖形化管理介面就可實現相同的配置了,但在某些情況,還是要直接編輯.ini才方便,一般只有很熟悉windows才能去直接編輯。開始時用於WIN3X下面,WIN95用登錄檔代替,以及後面的內容表示一個節,相當於登錄檔中的鍵。

除了windows現在很多其他作業系統下面的應用軟體也有.ini檔案,用來配置應用軟體以實現不同使用者的要求。一般不用直接編輯這些.ini檔案,應用程式的圖形介面即可操作以實現相同的功能。它可以用來存放軟體資訊,登錄檔資訊等。

INI檔案由節、鍵、值組成。

  [section]

引數(鍵=值)

  name=value

既然ini檔案在初始化中使用,那麼免不了的需要對它進行一些寫入讀取修改的操作,當然,這些東西window已經幫我們封裝好了,我們只需要呼叫就可以了。很easy

Ini使用到的一些API函式:

  1. #region 呼叫windowsAPI  
  2.        //用來讀取INI 檔案的內容  
  3.        [DllImport("Kernel32.dll")]  
  4.        private static extern int GetPrivateProfileString(string strAppName,string strKeyName,string strDefault,StringBuilder sbReturnString,int nSize,string strFileName);   
  5.        //返回所讀取的字串值的真實長度  
  6.        [DllImport("Kernel32.dll")]  
  7.        private extern static int GetPrivateProfileStringA(string strAppName, string strKeyName, string sDefault, byte[] buffer, int nSize, string strFileName);  
  8.        [DllImport("Kernel32.dll")]  
  9.        private static extern int GetPrivateProfileInt(string strAppName, string strKeyName, int nDefault, string strFileName);  
  10.        //獲取ini檔案所有的section  
  11.        [DllImport("Kernel32.dll")]  
  12.        private extern static int GetPrivateProfileSectionNamesA(byte[] buffer, int iLen, string fileName);  
  13.        //獲取指定Section的key和value          
  14.        [System.Runtime.InteropServices.DllImport("Kernel32.dll")]  
  15.        private static extern int GetPrivateProfileSection(string lpAppName,byte[] lpReturnedString, int nSize,string lpFileName);   
  16.        //根據傳入引數的不同進行寫入或修改或刪除操作(返回值 Long,非零表示成功,零表示失敗)  
  17.        [DllImport("Kernel32.dll")]  
  18.        public static extern long WritePrivateProfileString(string strAppName, string strKeyName, string strKeyValue, string strFileName);  
  19.        //新增一個section內容列表  
  20.        [DllImport("Kernel32.dll")]  
  21.        public static extern long WritePrivateProfileSection(string strAppName, string strkeyandvalue, string strFileName);  
  22.        #endregion  

呼叫這些函式的所用到的方法:

  1. #region 供UI呼叫的方法   
  2.         /// <summary>
  3.         /// 判斷該ini檔案是否存在如果不存在新建一個該檔案  
  4.         /// </summary>
  5.         public void FileExists()  
  6.         {  
  7.             try  
  8.             {  
  9.                 if (!File.Exists(this.filePath))  
  10.                 {  
  11.                     using (FileStream fs = File.Create(this.filePath))  
  12.                     {  
  13.                         fs.Close();  
  14.                     }  
  15.                 }  
  16.              }  
  17.             catch(Exception e)  
  18.             {  
  19.             }  
  20.         }  
  21.         /// <summary>
  22.         /// 返回該配置檔案中所有Section名稱的集合  
  23.         /// </summary>
  24.         /// <returns></returns>
  25.         public ArrayList ReadSections()  
  26.         {  
  27.             byte[] buffer = new byte[65535];  
  28.             int rel = GetPrivateProfileSectionNamesA(buffer, buffer.GetUpperBound(0), this.filePath);  
  29.             int iCnt, iPos;  
  30.             ArrayList arrayList = new ArrayList();  
  31.             string tmp;  
  32.             if (rel > 0)  
  33.             {  
  34.                 iCnt = 0iPos = 0;  
  35.                 for (iCnt = 0; iCnt <rel; iCnt++)  
  36.                 {  
  37.                     if (buffer[iCnt] == 0x00)  
  38.                     {  
  39.                         tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();  
  40.                         iPos = iCnt + 1;  
  41.                         if (tmp != "")  
  42.                             arrayList.Add(tmp);  
  43.                     }  
  44.                 }  
  45.             }  
  46.             return arrayList;  
  47.         }  
  48.         /// <summary>
  49.         ///  獲取指定節點的所有KEY的名稱  
  50.         /// </summary>
  51.         /// <paramname="sectionName"></param>
  52.         /// <returns></returns>
  53.         public ArrayList ReadKeys(string sectionName)  
  54.         {  
  55.             byte[] buffer = new byte[5120];  
  56.             int rel = GetPrivateProfileStringA(sectionName, null, "", buffer, buffer.GetUpperBound(0), this.filePath);  
  57.             int iCnt, iPos;  
  58.             ArrayList arrayList = new ArrayList();  
  59.             string tmp;  
  60.             if (rel > 0)  
  61.             {  
  62.                 iCnt = 0iPos = 0;  
  63.                 for (iCnt = 0; iCnt <rel; iCnt++)  
  64.                 {  
  65.                     if (buffer[iCnt] == 0x00)  
  66.                     {  
  67.                         tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();  
  68.                         iPos = iCnt + 1;  
  69.                         if (tmp != "")  
  70.                             arrayList.Add(tmp);  
  71.                     }  
  72.                 }  
  73.             }  
  74.             return arrayList;  
  75.         }  
  76.         /// <summary>
  77.         /// 讀取指定節點下的指定key的value返回string  
  78.         /// </summary>
  79.         /// <paramname="section"></param>
  80.         /// <paramname="key">param>
  81.         /// <returns></returns>
  82.         public string GetIniKeyValueForStr(string section, string key)  
  83.         {  
  84.             if (section.Trim().Length <= 0 || key.Trim().Length <= 0) return string.Empty;  
  85.             StringBuilder strTemp = new StringBuilder(256);  
  86.             GetPrivateProfileString(section, key, string.Empty, strTemp, 256, this.filePath);  
  87.             return strTemp.ToString().Trim();  
  88.         }  
  89.         /// <summary>
  90.         /// 從指定的節點中獲取一個整數值( Long,找到的key的值;如指定的key未找到,就返回預設值。如找到的數字不是一個合法的整數,函式會返回其中合法的一部分。如,對於“xyz=55zz”這個條目,函式返回55。)  
  91.         /// </summary>
  92.         /// <paramname="section"></param>
  93.         /// <paramname="key"></param>
  94.         /// <returns></returns>
  95.         public int GetIniKeyValueForInt(string section, string key)  
  96.         {  
  97.             if (section.Trim().Length <= 0 || key.Trim().Length <= 0) return 0;  
  98.             return GetPrivateProfileInt(section, key, 0, this.filePath);  
  99.         }  
  100.         /// <summary>
  101.         /// 讀取指定節點下的所有key 和value  
  102.         /// </summary>
  103.         /// <paramname="section"></param>
  104.         /// <returns></returns>
  105.         public ArrayList GetIniSectionValue(string section)  
  106.         {  
  107.             byte[] buffer = new byte[5120];  
  108.             int rel = GetPrivateProfileSection(section, buffer, buffer.GetUpperBound(0), this.filePath);  
  109.             int iCnt, iPos;  
  110.             ArrayList arrayList = new ArrayList();  
  111.             string tmp;  
  112.             if (rel > 0)  
  113.             {  
  114.                 iCnt = 0iPos = 0;  
  115.                 for (iCnt = 0; iCnt <rel; iCnt++)  
  116.                 {  
  117.                     if (buffer[iCnt] == 0x00)  
  118.                     {  
  119.                         tmp = System.Text.ASCIIEncoding.Default.GetString(buffer, iPos, iCnt - iPos).Trim();  
  120.                         iPos = iCnt + 1;  
  121.                         if (tmp != "")  
  122.                             arrayList.Add(tmp);  
  123.                     }  
  124.                 }  
  125.             }  
  126.             return arrayList;  
  127.         }  
  128.         /// <summary>
  129.         /// 往指定section的key中寫入value  
  130.         /// </summary>
  131.         /// <paramname="section"></param>
  132.         /// <paramname="key"></param>
  133.         /// <paramname="value"></param>
  134.         /// <returns></returns>
  135.         public bool WriteIniKey(string section, string key, string value)  
  136.         {  
  137.             try  
  138.             {  
  139.                 if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0)  
  140.                 {  
  141.                     flag = false;  
  142.                 }  
  143.                 else  
  144.                 {  
  145.                     if (WritePrivateProfileString(section, key, value, this.filePath) == 0)  
  146.                     {  
  147.                         flag = false;  
  148.                     }  
  149.                     else  
  150.                     {  
  151.                         flag = true;  
  152.                     }  
  153.                 }  
  154.             }  
  155.             catch  
  156.             {  
  157.                 flag = false;  
  158.             }  
  159.             return flag;  
  160.         }  
  161.         /// <summary>
  162.         /// 修改指定section的key的值  
  163.         /// </summary>
  164.         /// <paramname="section"></param>
  165.         /// <paramname="key"></param>
  166.         /// <paramname="value"></param>
  167.         /// <returns></returns>
  168.         public bool EditIniKey(string section, string key, string value)  
  169.         {  
  170.             try  
  171.             {  
  172.                 if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0)  
  173.                 {  
  174.                     flag = false;  
  175.                 }  
  176.                 else  
  177.                 {  
  178.                     if (WritePrivateProfileString(section, key, value, this.filePath) == 0)  
  179.                     {  
  180.                         flag = false;  
  181.                     }  
  182.                     else  
  183.                     {  
  184.                         flag = true;  
  185.                     }  
  186.                 }  
  187.             }  
  188.             catch  
  189.             {  
  190.                 flag = false;  
  191.             }  
  192.             return flag;  
  193.         }  
  194.         /// <summary>
  195.         /// 刪除指定section的指定key  
  196.         /// </summary>
  197.         /// <paramname="section"></param>
  198.         /// <paramname="key"></param>
  199.         /// <returns></returns>
  200.         public bool DeleteIniKey(string section, string key)  
  201.         {  
  202.             try  
  203.             {  
  204.                 if (section.Trim().Length <= 0 || key.Trim().Length <= 0)  
  205.                 {  
  206.                     flag = false;  
  207.                 }  
  208.                 else  
  209.                 {  
  210.                     if (WritePrivateProfileString(section, key, null, this.filePath) == 0)  
  211.                     {  
  212.                         flag = false;  
  213.                     }  
  214.                     else  
  215.                     {  
  216.                         flag = true;  
  217.                     }  
  218.                 }  
  219.             }  
  220.             catch  
  221.             {  
  222.                 flag = false;  
  223.             }  
  224.             return flag;  
  225.         }  
  226.         /// <summary>
  227.         /// 刪除指定section  
  228.         /// </summary>
  229.         /// <paramname="section"></param>
  230.         /// <returns></returns>
  231.         public bool DeleteIniSection(string section)  
  232.         {  
  233.             try  
  234.             {  
  235.                 if (section.Trim().Length <= 0)  
  236.                 {  
  237.                     flag = false;  
  238.                 }  
  239.                 else  
  240.                 {  
  241.                     if (WritePrivateProfileString(section, null, null, this.filePath) == 0)  
  242.                     {  
  243.                         flag = false;  
  244.                     }  
  245.                     else  
  246.                     {  
  247.                         flag = true;  
  248.                     }  
  249.                 }  
  250.             }  
  251.             catch  
  252.             {  
  253.                 flag = false;  
  254.             }  
  255.             return flag;  
  256.         }  
  257.         /// <summary>
  258.         /// 給一個節點寫入key和value列表  
  259.         /// </summary>
  260.         /// <paramname="section"></param>
  261.         /// <paramname="ht"></param>
  262.         /// <returns></returns>
  263.         public bool WriteIniSectionAndValue(string section, Hashtable ht)  
  264.         {  
  265.             string lpString = "";  
  266.             try  
  267.             {  
  268.                 if (section.Trim().Length <= 0 || ht.Count == 0)  
  269.                 {  
  270.                     flag = false;  
  271.                 }  
  272.                 else  
  273.                 {  
  274.                     foreach (DictionaryEntry de in ht)  
  275.                     {  
  276.                         lpString += de.Key+"="+de.Value;                        
  277.                         lpString += "\r\n";  
  278.                     }  
  279.                     if (WritePrivateProfileSection(section, lpString, this.filePath) == 0)  
  280.                     {  
  281.                         flag = false;  
  282.                     }  
  283.                     else  
  284.                     {  
  285.                         flag = true;  
  286.                     }  
  287.                 }  
  288.             }  
  289.             catch  
  290.             {  
  291.                 flag = false;  
  292.             }  
  293.             return flag;  
  294.         }  
  295.         /// <summary>
  296.         /// 給一個節點寫入key 列表  
  297.         /// </summary>
  298.         /// <paramname="section"></param>
  299.         /// <paramname="lstKeyValue"></param>
  300.         /// <returns></returns>
  301.         public bool WriteIniSectionName(string section, List<string> lstKeyValue)  
  302.         {  
  303.             string lpString = "";  
  304.             try  
  305.             {  
  306.                 if (section.Trim().Length <= 0 || lstKeyValue.Count == 0)  
  307.                 {  
  308.                     flag = false;  
  309.                 }  
  310.                 else  
  311.                 {  
  312.                     for (int i = 0; i <lstKeyValue.Count; ++i)  
  313.                     {  
  314.                         lpString += lstKeyValue[i];  
  315.                         lpString += "\r\n";  
  316.                     }  
  317.                     if (WritePrivateProfileSection(section, lpString, this.filePath) == 0)  
  318.                     {  
  319.                         flag = false;  
  320.                     }  
  321.                     else  
  322.                     {  
  323.                         flag = true;  
  324.                     }  
  325.                 }  
  326.             }  
  327.             catch  
  328.             {  
  329.                 flag = false;  
  330.             }  
  331.             return flag;  
  332.         }  
  333.         #endregion  

U層,我們需要

  1. //建立一個INIFile物件,引數為檔案路徑  
  2.         ManagerConfigIni iniConfig = new ManagerConfigIni(AppDomain.CurrentDomain.BaseDirectory + @"\配置.ini");  

然後通過相應的事件,來呼叫對應的方法即可。

如果 ini 中沒有指定的 SectionAPI會新建 Section,如果沒有指定的 Key則新建一個 Key 並寫入資料,如果已經存在,則用字串代替原來的值。當指定的 ini也不存在的時候,API 會自動建立一個新的檔案,所以使用 ini的好處是我們不必為了儲存少量的資料涉及到檔案操作,就連查詢檔案是否存在的操作都不必要。

使用要點:在我們實際使用的時候,用的最多的是 GetPrivateProfileString WritePrivateProfileString,但在對自定義 ini檔案操作的時候要注意的是,如果 lpFileName 指定的檔案沒有路徑的話,Api會去 Windows 的安裝目錄去找而不會在當前目錄找,但是每次用到 ini函式要獲取當前路徑顯然太麻煩了,這裡有一個變通的辦法,你只要在 ini 檔名前面加上 .\ 就可以了,比如說要對本目錄下的 user.ini操作,那麼檔名就是 '.\user.ini' 這樣顯然比較方便。另外,當你要把一個 Key清除的時候,可以使用把 lpString 指向一個空的字串然後使用 WritePrivateProfileString。當你要把一個 section的全部內容清空的時候,也不必把 key 一個個的清除,可以使用把 lpString指向一個空的字串然後使用 WritePrivateProfileSection INI檔案就是副檔名為“ini”的檔案。在Windows系統中,INI檔案是很多,最重要的就是“System.ini”、“System32.ini”和“Win.ini”。該檔案主要存放使用者所做的選擇以及系統的各種引數。使用者可以通過修改INI檔案,來改變應用程式和系統的很多配置。但自從Windows 95的退出,在Windows系統中引入了登錄檔的概念,INI檔案在Windows系統的地位就開始不斷下滑,這是因為登錄檔的獨特優點,使應用程式和系統都把許多引數和初始化資訊放進了登錄檔中。

http://blog.csdn.net/laner0515/article/details/8439933