C# 保存参数为ini文件

先创建个类,把对ini文件的读写方法写在一起。你们可以直接把下面这个类复制到你想要的命名空间下就能用了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.IO;

class OperateIniFile
{
	#region API函数声明
	[DllImport("kernel32")]
	private static extern long WritePrivateProfileString(string section, string key,
            string val, string filePath);
    
    [DllImport("kernel32")]
    private static extern long GetPrivateProfileString(string section, string key, string def,
            StringBuilder retVal, int size, string filePath);
    #endregion

	#region 读ini文件
	public string ReadIniData(string Section, string Key, string iniFilePath)
	{
		if(File.Exists(iniFilePath))
		{
			StringBuilder temp = new StringBuilder(1024);
            GetPrivateProfileString(Section, Key, "", temp, 1024, iniFilePath);
            return temp.ToString();
		}
		else
		{
			return String.Empty;
		}
	}
	#endregion
	
	#region 写ini文件
	public bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
	{
		if (File.Exists(iniFilePath))
        {
        	long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
            if (OpStation == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        else
        {
            return false;
        }
	}
	#endregion
}

写完后,我们在winform上试试。
在这里插入图片描述
ini文件的结构,section就想是一个大结点,然后在其内又包括许多属性
C# 保存参数为ini文件_第1张图片

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;
using System.IO;
namespace SaveParameter
{
	public partial class Form1 : Form
	{
		//保存路径自己随意,Aplication.StartupPath是可执行文件.exe的路径
		private string iniPath = Application.StartupPath + "\\settings\\test1.ini";
		public Form1()
        {
            InitializeComponent();
        }
        private void save_button_Click(object sender, EventArgs e)
        {
            OperateIniFile F_operateIni = new OperateIniFile();
            bool result = true;
            result = F_operateIni.WriteIniData("PLC配置", "PlcIpAddress", ipText.Text, iniPath);
            result = F_operateIni.WriteIniData("PLC配置", "PlcPort", comText.Text, iniPath);

            if(result == true)
            {
                MessageBox.Show("保存成功!");
            }
            else
            {
                MessageBox.Show("保存失败!");
            }
        }
        private void read_button_Click(object sender, EventArgs e)
        {
            OperateIniFile F_operateIni = new OperateIniFile();

            string ip = F_operateIni.ReadIniData("PLC配置", "PlcIpAddress", iniPath);
            string port = F_operateIni.ReadIniData("PLC配置", "PlcPort", iniPath);

            ipText.Text = ip;
            comText.Text = port;
        }
	}
}

参考:
参考链接

你可能感兴趣的:(笔记,c#,开发语言)