winform c#寫ini檔案

iDotNetSpace發表於2009-12-03

INI檔案就是副檔名為"ini"的檔案。

其一般形式如下:
[section1]                             // 配置節
     //鍵名          //鍵值

   keyword1 = valuel
   keyword2 = value2
   ……
  [section2]
  keyword3 = value3
  keyword4 = value4

在Windows系統中,INI檔案是很多,最重要的就是"System.ini"、"System32.ini"和"Win.ini"。該檔案主要存放使用者所做的選擇以及系統的各種引數。使用者可以通過修改INI檔案,來改變應用程式和系統的很多配置。但自從Windows 95的退出,在Windows系統中引入了登錄檔的概念,INI檔案在Windows系統的地位就開始不斷下滑,這是因為登錄檔的獨特優點,使應用程式和系統都把許多引數和初始化資訊放進了登錄檔中。以及XML檔案的國際標準化給INI檔案又一次打擊。

但在某些場合,INI檔案還擁有其不可替代的地位。比如綠色軟體的規定就是不向登錄檔和系統中填入新東西。對於軟體需要儲存的資訊就需要存入到檔案中了。XML雖然相容性比較好,但對於僅僅儲存幾個自定義引數而言就顯得大材小用了。這是就可以選擇使用快速簡單的儲存方式:INI檔案。

本文就來探討一下C#是如何對INI進行讀寫操作。

主要思路是呼叫Win32 API。

1.引入名稱空間

using System.Runtime.InteropServices;
2.宣告(把一個Win32 API函式轉成C#函式)

//宣告INI檔案的寫操作函式 WritePrivateProfileString()
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

//宣告INI檔案的讀操作函式 GetPrivateProfileString()
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
3.函式


public void Writue(string section, string key, string value)
{
    // section=配置節,key=鍵名,value=鍵值,path=路徑
    WritePrivateProfileString(section, key, value, sPath);
}


public string ReadValue(string section, string key)
{
    // 每次從ini中讀取多少位元組
    System.Text.StringBuilder temp = new System.Text.StringBuilder(255);

    // section=配置節,key=鍵名,temp=上面,path=路徑
    GetPrivateProfileString(section, key, "", temp, 255, sPath);
    return temp.ToString();                  //注意型別的轉換
}
到此基本功能已經實現了。下面我們將所有的程式碼重新整合一下:

namespace Library.File
{
    public class Ini
    {
        // 宣告INI檔案的寫操作函式 WritePrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        // 宣告INI檔案的讀操作函式 GetPrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);

        private string sPath = null;

       
        public Ini(string path)
        {
            this.sPath = path;
        }

       
        public void Writue(string section, string key, string value)
        {
            // section=配置節,key=鍵名,value=鍵值,path=路徑
            WritePrivateProfileString(section, key, value, sPath);
        }

       
        public string ReadValue(string section, string key)
        {
            // 每次從ini中讀取多少位元組
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);

            // section=配置節,key=鍵名,temp=上面,path=路徑
            GetPrivateProfileString(section, key, "", temp, 255, sPath);
            return temp.ToString();
        }
    }
}
開始呼叫函式。

// 寫入ini
Ini ini = new Ini("C:/config.ini");
ini.Writue("Setting", "key1", "HELLO WORLD!");
ini.Writue("Setting", "key2", "HELLO CHINA!");

 

 

// 讀取ini
Ini ini = new Ini("C:/config.ini");
string str1 = ini.ReadValue("Setting", "key1");
MessageBox.Show(str1);


二,在一些小的應用中,有時候不需要使用資料困這樣大規模的資料管理工具,也很少進行資料的查詢、修改等操作,而僅用檔案來儲存資料。這時就需要使用。net中的檔案操作物件,如file、streamReader、streamWriter等。
1,使用File物件操作檔案
  System.IO.File類提供了一系類的靜態辦法,完成對晚間的常用操作,如新建、刪除、拷貝、移動等
2,使用StreamWriter寫入檔案

在System.IO空間中定義了一個檔案寫入器物件StreamWriter,使用它可以以一種特定的編碼向輸出流中(Stream)寫入字元。

3,使用SteamReader 讀取檔案

與streamWrite對應

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-621545/,如需轉載,請註明出處,否則將追究法律責任。

相關文章