簡單分析C#操作INI檔案

來源:互聯網
上載者:User

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檔案操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.