詳解C#如何實現讀寫ini文件

1.創建一個C#項目

2.創建一個ini文件

在Debug目錄下創建一個ini文件,寫入下面內容,註意編碼格式用ANSI。

[Information]

Name=周星星

Gender=男

Age=55

Region=香港

3.創建一個winform界面

如下圖

4.添加一個ini管理類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
 
namespace Test1
{
   public static class IniFunc
    {
        /// <summary>
        /// 獲取值
        /// </summary>
        /// <param name="section">段落名</param>
        /// <param name="key">鍵名</param>
        /// <param name="defval">讀取異常是的缺省值</param>
        /// <param name="retval">鍵名所對應的的值,沒有找到返回空值</param>
        /// <param name="size">返回值允許的大小</param>
        /// <param name="filepath">ini文件的完整路徑</param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(
            string section,
            string key,
            string defval,
            StringBuilder retval,
            int size,
            string filepath);
 
        /// <summary>
        /// 寫入
        /// </summary>
        /// <param name="section">需要寫入的段落名</param>
        /// <param name="key">需要寫入的鍵名</param>
        /// <param name="val">寫入值</param>
        /// <param name="filepath">ini文件的完整路徑</param>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        private static extern int WritePrivateProfileString(
            string section,
            string key,
            string val,
            string filepath);
 
 
        /// <summary>
        /// 獲取數據
        /// </summary>
        /// <param name="section">段落名</param>
        /// <param name="key">鍵名</param>
        /// <param name="def">沒有找到時返回的默認值</param>
        /// <param name="filename">ini文件完整路徑</param>
        /// <returns></returns>
        public static string getString(string section, string key, string def, string filename)
        {
            StringBuilder sb = new StringBuilder(1024);
            GetPrivateProfileString(section, key, def, sb, 1024, filename);
            return sb.ToString();
        }
 
        /// <summary>
        /// 寫入數據
        /// </summary>
        /// <param name="section">段落名</param>
        /// <param name="key">鍵名</param>
        /// <param name="val">寫入值</param>
        /// <param name="filename">ini文件完整路徑</param>
        public static void writeString(string section, string key, string val, string filename)
        {
            WritePrivateProfileString(section, key, val, filename);
        }
    }
}

5.添加winform代碼

雙擊winform界面,加入下面代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Test1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private string filename = null;
 
        private void Form1_Load(object sender, EventArgs e)
        {
            filename = Application.StartupPath + "\\Config.ini";
        }
 
        /// <summary>
        /// 讀取
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Read_Click(object sender, EventArgs e)
        {
            string names = IniFunc.getString("Information", "Name", null, filename);
            string gender = IniFunc.getString("Information", "Gender", null, filename);
            string age = IniFunc.getString("Information", "Age", null, filename);
            string region = IniFunc.getString("Information", "Region", null, filename);
 
            TextBox_Name.Text = names;
            TextBox_Gender.Text = gender;
            TextBox_Age.Text = age;
            TextBox_Region.Text = region;
        }
 
        /// <summary>
        /// 寫入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Write_Click(object sender, EventArgs e)
        {
            string names = TextBox_Name.Text;
            string gender = TextBox_Gender.Text;
            string age = TextBox_Age.Text;
            string region = TextBox_Region.Text;
 
            IniFunc.writeString("Information", "Name", names, filename);
            IniFunc.writeString("Information", "Gender", gender, filename);
            IniFunc.writeString("Information", "Age", age, filename);
            IniFunc.writeString("Information", "Region", region, filename);
        }
 
        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Clear_Click(object sender, EventArgs e)
        {
            TextBox_Name.Text = string.Empty;
            TextBox_Gender.Text = string.Empty;
            TextBox_Age.Text = string.Empty;
            TextBox_Region.Text = string.Empty;
        }
    }
}

到此這篇關於詳解C#如何實現讀寫ini文件的文章就介紹到這瞭,更多相關C#讀寫ini文件內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: