C#語言還是比較常見的東西,這裡我們主要介紹C#對INI檔案操作,包括介紹對INI檔案進行寫操作等方面。
C#對INI檔案操作
對INI檔案進行寫操作,是通過組件button2的"Click"事件來實現的。這裡有一點應該注意,當在調用 WritePrivateProfileString()對INI檔案進行寫操作的時候,如果此時在INI檔案中存在和要寫入的資訊相同的段落名稱和關鍵字,則將覆蓋此INI資訊。下面是button2組件的"Click"事件對應的代碼清單:
private void button2_Click ( object sender , System.EventArgs e )
{
string FileName = textBox1.Text ;
string section = textBox2.Text ;
string key = textBox3.Text ;
string keyValue = textBox4.Text ;
WritePrivateProfileString ( section , key , keyValue , FileName ) ;
MessageBox.Show( "成功寫入INI檔案!" , "資訊" ) ;
}
正確讀取INI的必須滿足三個前提:INI檔案的全路徑、段落名稱和關鍵字名稱。否則就無法正確讀取。你可以設定讀取不成功後的預設數值,在下面的程式中,為了直觀設定的是“無法讀取對應數值!”字串,讀取INI檔案是通過button3組件的“Click”事件來實現的,下面是其對應的代碼清單:
private void button3_Click ( object sender , System.EventArgs e )
{
StringBuilder temp = new StringBuilder ( 255 ) ;
string FileName = textBox1.Text ;
string section = textBox2.Text ;
string key = textBox3.Text ;
int i = GetPrivateProfileString ( section , key ,"無法讀取對應數值!",
temp , 255 , FileName ) ;
//顯示讀取的數值
textBox4.Text = temp.ToString( ) ;
}
以上介紹C#對INI檔案操作