創作內容

13 GP

C# INI檔讀取與寫入

作者:貓貓風 ฅ●ω●ฅ│2022-04-02 18:52:31│巴幣:26│人氣:1360
.












INI檔案是一種無固定標準格式的設定檔。 它以簡單的文字與簡單的結構組成,常常使用在Windows作業系統上,許多程式也會採用INI檔案做為設定檔之用。

通常INI檔會用來儲存軟體在載入時所需要的參數,此參數可以在軟體開啟前進行編輯,在軟體開啟後載入設定值,比起用文字檔[.txt] 又更具有識別性與操作便利性

文字檔需要自定義結構進行內容處理;INI則只要照屬性去取得需要的數值內容即可

以下為INI檔讀取與寫入功能程式碼

此範例調用 WIN32 API進行實作  當然也可以用檔案讀取 Stream Reader的方式來撰寫

Class  IniManager


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. namespace ini_read_write
  7. {
  8.     class IniManager
  9.     {
  10.         private string filePath;
  11.         private StringBuilder lpReturnedString;
  12.         private int bufferSize;
  13.         [DllImport("kernel32")]
  14.         private static extern long WritePrivateProfileString(string section, string key,
  15.         string lpString, string lpFileName);
  16.         [DllImport("kernel32")]
  17.         private static extern int GetPrivateProfileString(string section, string key, string
  18.         lpDefault, StringBuilder lpReturnedString, int nSize, string lpFileName);
  19.         public IniManager(string iniPath)
  20.         {
  21.             filePath = iniPath;
  22.             bufferSize = 512;
  23.             lpReturnedString = new StringBuilder(bufferSize);
  24.         }
  25.         // read ini date depend on section and key
  26.         public string ReadIniFile(string section, string key, string defaultValue)
  27.         {
  28.             lpReturnedString.Clear();
  29.             GetPrivateProfileString(section, key, defaultValue, lpReturnedString, bufferSize,
  30.             filePath);
  31.             return lpReturnedString.ToString();
  32.         }
  33.         // write ini data depend on section and key
  34.         public void WriteIniFile(string section, string key, Object value)
  35.         {
  36.             WritePrivateProfileString(section, key, value.ToString(), filePath);
  37.         }
  38.     }
  39. }

Class Main
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace ini_read_write
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         String _ini_path = "";
  18.         IniManager _iniManager;
  19.         private void Form1_Load(object sender, EventArgs e)
  20.         {
  21.             _ini_path = System.Windows.Forms.Application.StartupPath;
  22.             _iniManager = new IniManager(_ini_path + "\\parameter.ini");
  23.         }
  24.         private void btn_sec1_w1_Click(object sender, EventArgs e)
  25.         {
  26.             _iniManager.WriteIniFile("Customer", "1", txt_sec1_f1.Text);
  27.         }
  28.         private void btn_sec1_w2_Click(object sender, EventArgs e)
  29.         {
  30.             _iniManager.WriteIniFile("Customer", "2", txt_sec1_f2.Text);
  31.         }
  32.         private void btn_sec1_w3_Click(object sender, EventArgs e)
  33.         {
  34.             _iniManager.WriteIniFile("Customer", "3", txt_sec1_f3.Text);
  35.         }
  36.         private void btn_sec2_w1_Click(object sender, EventArgs e)
  37.         {
  38.             _iniManager.WriteIniFile("Mode", "1", txt_sec2_f1.Text);
  39.         }
  40.         private void btn_sec2_w2_Click(object sender, EventArgs e)
  41.         {
  42.             _iniManager.WriteIniFile("Mode", "2", txt_sec2_f2.Text);
  43.         }
  44.         private void btn_sec1_r1_Click(object sender, EventArgs e)
  45.         {
  46.             txt_sec1_f1.Text = _iniManager.ReadIniFile("Customer", "1", "no-match-data");
  47.         }
  48.         private void btn_sec1_r2_Click(object sender, EventArgs e)
  49.         {
  50.             txt_sec1_f2.Text = _iniManager.ReadIniFile("Customer", "2", "no-match-data");
  51.         }
  52.         private void btn_sec1_r3_Click(object sender, EventArgs e)
  53.         {
  54.             txt_sec1_f3.Text = _iniManager.ReadIniFile("Customer", "3", "no-match-data");
  55.         }
  56.         private void btn_sec2_r1_Click(object sender, EventArgs e)
  57.         {
  58.             txt_sec2_f1.Text = _iniManager.ReadIniFile("Mode", "1", "no-match-data");
  59.         }
  60.         private void btn_sec2_r2_Click(object sender, EventArgs e)
  61.         {
  62.             txt_sec2_f2.Text = _iniManager.ReadIniFile("Mode", "2", "no-match-data");
  63.         }
  64.     }
  65. }

執行結果


引用網址:https://home.gamer.com.tw/TrackBack.php?sn=5424957
All rights reserved. 版權所有,保留一切權利

相關創作

同標籤作品搜尋:C#|檔案讀取|INI|StreamReader|StreamWriter|涼涼風|程式設計

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

13喜歡★s1234567 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:靈魂潮汐 七個願望與神的... 後一篇:小米空氣清淨機濾芯更換與...

追蹤私訊切換新版閱覽

作品資料夾

flys8028大家
有點色色取向的部落格 https://www.rocksugarcat.com/看更多我要大聲說昨天15:38


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】