C#操作INI文件的案例分享

2022-11-06 20:11:39 浏览数 (1)

kernel32

kernel32.dll是Windows 9x/Me中非常重要的32位动态链接库文件,属于内核级文件。它控制着系统的内存管理、数据的输入输出操作和中断处理,当Windows启动时,kernel32.dll就驻留在内存中特定的写保护区域,使别的程序无法占用这个内存区域。 以上来自百度百科

1

[DllImport("kernel32")] //读取INI文件public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);[DllImport("kernel32")]//向INI文件中写入数据public static extern long WritePrivateProfileString(string mpAppName,string mpKeyName,string mpDefault,string mpFileName);

Demo案例

实现功能

调用Kernel32函数实现ini文件的读取和写入。

注意ini的编码格式为ANSI。在实际软件编写时,发现无法保存为ANSI,网上了解后发现是ASCI包含中文字符,所有ini需要有中文字符。如果全是英文的话,则无法保存为ASCI,即使保存为ASCI,再次打开后也会变成其他编码格式。

程序代码

2

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Runtime.InteropServices;using System.Text;using System.Threading.Tasks;using System.Windows.Forms; namespace INIOpera{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        #region 定义公共变量        public static string strNode = "INFO";        public static string strPath = Application.StartupPath "\INFO.ini";        #endregion         //引入kernel32函数         [DllImport("kernel32")] //读取INI文件        public static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);        [DllImport("kernel32")]//向INI文件中写入数据        public static extern long WritePrivateProfileString(string mpAppName, string mpKeyName, string mpDefault, string mpFileName);        /// <summary>        /// 读取INI文件        /// </summary>        /// <param name="section">欲在其中查找条目的小节名称。这个字串不区分大小写。</param>        /// <param name="key">欲获取的项名或条目名。这个字串不区分大小写。</param>        /// <param name="def">指定的条目没有找到时返回的默认值。可设为空("")</param>        /// <param name="filePath">完整的INI文件名</param>        /// <returns></returns>        public static string GetINIFileString(string section,string key, string def, string filePath)        {            StringBuilder strTemp = new StringBuilder(1024);            GetPrivateProfileString(section, key, def, strTemp, 1024, filePath);            return strTemp.ToString();        }                 private void button1_Click(object sender, EventArgs e)        {            textBox1.Text = GetINIFileString(strNode, "Name","", strPath);        }         private void button2_Click(object sender, EventArgs e)        {            textBox2.Text = GetINIFileString(strNode, "Location", "", strPath);        }         private void button3_Click(object sender, EventArgs e)        {            textBox3.Text = GetINIFileString(strNode, "IP", "", strPath);        }         private void button4_Click(object sender, EventArgs e)        {            textBox4.Text = GetINIFileString(strNode, "ComputerName", "", strPath);        }         private void button5_Click(object sender, EventArgs e)        {            textBox5.Text = GetINIFileString(strNode, "WorkAblity", "", strPath);        }         private void textBox1_TextChanged(object sender, EventArgs e)        {         }         private void textBox2_TextChanged(object sender, EventArgs e)        {         }         private void button6_Click(object sender, EventArgs e)        {            WritePrivateProfileString(strNode, "Name", textBox10.Text,strPath);        }         private void button7_Click(object sender, EventArgs e)        {            WritePrivateProfileString(strNode, "Location", textBox9.Text, strPath);        }         private void button8_Click(object sender, EventArgs e)        {            WritePrivateProfileString(strNode, "IP", textBox8.Text, strPath);        }         private void button9_Click(object sender, EventArgs e)        {            WritePrivateProfileString(strNode, "ComputerName", textBox7.Text, strPath);        }         private void button10_Click(object sender, EventArgs e)        {            WritePrivateProfileString(strNode, "WorkAblity", textBox6.Text, strPath);        }    }}

0 人点赞