1. 程式人生 > >WPF INI檔案的建立與使用

WPF INI檔案的建立與使用

      

INI檔案格式是某些平臺或軟體上的配置檔案的非正式標準,以節(section)和鍵(key)構成,常用於微軟Windows作業系統中。這種配置檔案的副檔名多為INI,故名。

INI是英文初始化initialization)的縮寫。正如該術語所表示的,INI檔案被用來對作業系統或特定程式初始化或進行引數設定。                                      ----摘自百度百科

        最近做專案的時候就遇到對ini檔案的使用。因為C#所使用的.NET框架下的公共類庫並沒有提供直接操作INI檔案的類,呼叫API函式是比較理想的方法。  

        首先,建立一個WPF專案,加上如圖的控制元件。


textBox用來顯示讀取INI檔案裡面的資訊。

建立一個INI.cs檔案,用來寫程式。

 public class INI
    {
        /// <summary>
        /// 寫操作
        /// </summary>
        /// <param name="section">節</param>
        /// <param name="key">鍵</param>
        /// <param name="value">值</param>
        /// <param name="filePath">檔案路徑</param>
        /// <returns></returns>
        [DllImport("kernel32")]
              private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);


       /// <summary>
       /// 讀操作
       /// </summary>
       /// <param name="section">節</param>
       /// <param name="key">鍵</param>
       /// <param name="def">未讀取到的預設值</param>
        /// <param name="retVal">讀取到的值</param>
        /// <param name="size">大小</param>
        /// <param name="filePath">路徑</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>
        /// 讀ini檔案
        /// </summary>
        /// <param name="section">節</param>
        /// <param name="key">鍵</param>
        /// <param name="defValue">未讀取到值時的預設值</param>
        /// <param name="filePath">檔案路徑</param>
        /// <returns></returns>
        public static string ReadIni(string section, string key)
        {
            string IniFilePath = @"F:\Setting.ini";
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(section, key, "", temp, 255, IniFilePath);
            return temp.ToString();
        }

        /// <summary>
        /// 寫入ini檔案
        /// </summary>
        /// <param name="section">節</param>
        /// <param name="key">鍵</param>
        /// <param name="value">值</param>
        /// <param name="filePath">檔案路徑</param>
        /// <returns></returns>
        public static void WriteIni(string section, string key, string value)
        {
            string IniFilePath = @"F:\Setting.ini";
            WritePrivateProfileString(section, key, value, IniFilePath);
        }
        /// <summary>
        /// 刪除節
        /// </summary>
        /// <param name="section">節</param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static long DeleteSection(string section)
        {
            string IniFilePath = @"F:\Setting.ini";
            return WritePrivateProfileString(section, null, null, IniFilePath);
        }

        /// <summary>
        /// 刪除鍵
        /// </summary>
        /// <param name="section">節</param>
        /// <param name="key">鍵</param>
        /// <param name="filePath">檔案路徑</param>
        /// <returns></returns>
        public static long DeleteKey(string section, string key)
        {
            string IniFilePath = @"F:\Setting.ini";
            return WritePrivateProfileString(section, key, null, IniFilePath);
        }

    }
        然後,新增Button的觸發事件
<span style="white-space:pre">	</span> //增加
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            INI.WriteIni("section1", "key1", "勇氣呀");
            INI.WriteIni("section1", "key2", "智慧呀");
            INI.WriteIni("section1", "key3", "堅持呀");
            INI.WriteIni("section1", "key4", "永遠呀");
            INI.WriteIni("section2", "key5", "智慧呀");
            INI.WriteIni("section2", "key6", "堅持呀");
            INI.WriteIni("section2", "key7", "果敢呀");
            INI.WriteIni("section2", "key8", "才能呀");
        }


        //顯示框
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            textBox1.Text = INI.ReadIni("section1", "key1");
            textBox2.Text = INI.ReadIni("section1", "key2");
            textBox3.Text = INI.ReadIni("section2", "key5");
            textBox4.Text = INI.ReadIni("section2", "key6");
        }
        //刪除值
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            INI.DeleteSection("section1");
        }
        //刪除鍵
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            INI.DeleteKey("section2", "key5");
        }

點選增加按鈕,會在設定目錄下生成Setting.ini檔案

檔案內容:

預先設定的內容。

 點選讀取,從INI檔案中讀取,顯示到textBox裡面:

點選刪除值後,再點選讀取,效果如圖:

點選刪除鍵,再點選增加按鈕,效果如圖:

讀取INI檔案成功了!!!