C# read, write and edit INI files - complete and detailed version

INI file concept
INI is a file with the extension "INI". In fact, it is a text file that can be opened with Notepad. It mainly stores the choices made by the user or various parameters of the system.

The C# read and write ini file is actually not an ordinary text file. It has its own structure. It consists of several paragraphs (SECTION). Under each bracketed title, there are several keywords (KEYWORD) starting with a single word and An equal sign, the right side of the equal sign is the value of the keyword (VALUE), for example,

[Section1]

KeyWord1 = Value1 

KeyWord2 = Value2 

... 

[Section2]

KeyWord3 = Value3 

KeyWord4 = Value4

Insert image description here
INI file reading operation
There is no class to directly read and write INI in the C# namespace. Although there is no class in C#, there are Win32 API functions in the file "kernel32.dll" -WritePrivateProfileString() and GetPrivateProfileString()

class file

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
 
 
namespace Files
{
    
    
    class FilesINI
    {
    
    
        // 声明INI文件的写操作函数 WritePrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
 
        // 声明INI文件的读操作函数 GetPrivateProfileString()
        [System.Runtime.InteropServices.DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath);
 
 
        /// 写入INI的方法
        public void INIWrite(string section, string key, string value,string path)
        {
    
    
            // section=配置节点名称,key=键名,value=返回键值,path=路径
            WritePrivateProfileString(section, key, value, path);
        }
 
        //读取INI的方法
        public string INIRead(string section, string key,string path)
        {
    
    
            // 每次从ini中读取多少字节
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);
 
            // section=配置节点名称,key=键名,temp=上面,path=路径
            GetPrivateProfileString(section, key, "", temp, 255, path);
            return temp.ToString();
 
        }
 
        //删除一个INI文件
        public void INIDelete(string FilePath)
        {
    
    
            File.Delete(FilePath);
        }
 
    }
}

main function

using Files;
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 INI文件读写操作
{
    
    
    public partial class Form1 : Form
    {
    
    
        public Form1()
        {
    
    
            InitializeComponent();
        }
 
        string IP;
        int Port;
        int Speed;
        //声明默认配置文件路径
        public string INIPath = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "Config.ini";
        private void Form1_Load(object sender, EventArgs e)
        {
    
    
            FilesINI ConfigINI = new FilesINI();
 
            IP = ConfigINI.INIRead("Fanuc机器人控制参数", "IP", INIPath);
            Port = Convert.ToInt32(ConfigINI.INIRead("Fanuc机器人控制参数", "Port", INIPath));
            Speed = Convert.ToInt32(ConfigINI.INIRead("Fanuc机器人控制参数", "Speed", INIPath));
            textBox1.Text = IP;
            textBox2.Text = Port.ToString();
            textBox3.Text = Speed.ToString();
        }
    }
}

Insert image description here
Insert image description here
INI file modification operation
Modify IP

 private void button1_Click(object sender, EventArgs e)
        {
    
    
            FilesINI ConfigINI = new FilesINI();
            ConfigINI.INIWrite("Fanuc机器人控制参数", "IP", textBox1.Text, INIPath);
            
        }

Insert image description here

INI file writing operation
Add new configuration node [KUKA robot control parameters]

 private void button1_Click(object sender, EventArgs e)
        {
    
    
            FilesINI ConfigINI = new FilesINI();
            ConfigINI.INIWrite("KUKA机器人控制参数", "IP", textBox1.Text, INIPath);
            
        }

Insert image description here
Under a certain configuration node, add the new keyword Angle

private void button1_Click(object sender, EventArgs e)
        {
    
    
            FilesINI ConfigINI = new FilesINI();
            ConfigINI.INIWrite("KUKA机器人控制参数", "Angle", textBox1.Text, INIPath);
            
        }

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_45499836/article/details/118353614