C# ini文件全自动界面配置:打开界面时读ini配置到界面各控件,界面上的控件根据ini文件内容自动生成,点保存时把界面各控件的值写到ini里。

发布于:2025-03-29 ⋅ 阅读:(29) ⋅ 点赞:(0)

用C#窗体 写一个人性化的.ini文件 的配置工具。 打开界面时读ini配置到界面各控件,界面上的控件根据ini文件内容自动生成,点保存时把界面各控件的值写到ini里。
在这里插入图片描述

通用

using FT_Tools;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;


public class ConfigForm : Form
{
    private Dictionary<Control, Tuple<string, string>> controlMap = new Dictionary<Control, Tuple<string, string>>();
    private Button btnSave = new Button { Text = "保存", Dock = DockStyle.Bottom };
    private Panel panel = new Panel { Dock = DockStyle.Fill, AutoScroll = true };

    public ConfigForm()
    {
        Text = "设置";
        WindowState = FormWindowState.Maximized;
        LoadConfig();
        btnSave.Click += (s, e) => SaveConfig();
        Controls.Add(panel);
        Controls.Add(btnSave);
    }

    private void LoadConfig()
    {
        var data = Program.myIni.ReadAll();
        int y = 10;
        foreach (var section in data)
        {
            foreach (var kv in section.Value)
            {
                Label lbl = new Label { Text = kv.Key, Left = 10, Top = y, AutoSize = true };
                Control inputControl;

                if (bool.TryParse(kv.Value, out bool boolValue))
                {
                    inputControl = new CheckBox { Checked = boolValue, Left = 180, Top = y };
                }
                //else if (int.TryParse(kv.Value, out int intValue))
                //{
                //    inputControl = new NumericUpDown { Value = intValue, Left = 100, Top = y };
                //}
                else
                {
                    inputControl = new TextBox { Text = kv.Value, Left = 180, Top = y, Width = 700 };
                }
                controlMap[inputControl] = Tuple.Create(section.Key, kv.Key);
                panel.Controls.Add(lbl);
                panel.Controls.Add(inputControl);
                y += 25;
            }
        }
    }

    private void SaveConfig()
    {
        foreach (var kv in controlMap)
        {
            string value;
            if (kv.Key is TextBox tb)
            {
                value = tb.Text;
            }
            else if (kv.Key is NumericUpDown num)
            {
                value = num.Value.ToString();
            }
            else if (kv.Key is CheckBox cb)
            {
                value = cb.Checked.ToString();
            }
            else
            {
                value = "";
            }
            Program.myIni.Write(kv.Value.Item1, kv.Value.Item2, value);
        }
        MessageBox.Show("配置已保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //重启自己
        Application.Restart();
    }
}

并能定制控制

using FT_Tools;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;


public class ConfigFormF200CKD : Form
{
    private Dictionary<Control, Tuple<string, string>> controlMap = new Dictionary<Control, Tuple<string, string>>();
    private Button btnSave = new Button { Text = "保存", Dock = DockStyle.Bottom };
    private Panel panel = new Panel { Dock = DockStyle.Fill, AutoScroll = true };

    public ConfigFormF200CKD()
    {
        Text = "设置";
        WindowState = FormWindowState.Maximized;
        LoadConfig();
        btnSave.Click += (s, e) => SaveConfig();
        Controls.Add(panel);
        Controls.Add(btnSave);
    }

    private void LoadConfig()
    {
        var data = Program.myIni.ReadAll();
        int y = 10;
        foreach (var section in data)
        {
            foreach (var kv in section.Value)
            {
                Label lbl = new Label { Text = kv.Key, Left = 10, Top = y, AutoSize = true };
                Control inputControl;

                if (bool.TryParse(kv.Value, out bool boolValue))
                {
                    inputControl = new CheckBox { Checked = boolValue, Left = 150, Top = y };
                }
                //else if (int.TryParse(kv.Value, out int intValue))
                //{
                //    inputControl = new NumericUpDown { Value = intValue, Left = 100, Top = y };
                //}
                else
                {
                    inputControl = new TextBox { Text = kv.Value, Left = 150, Top = y, Width = 400 };
                }
                controlMap[inputControl] = Tuple.Create(section.Key, kv.Key);
                panel.Controls.Add(lbl);
                panel.Controls.Add(inputControl);
                { //定制配置
                    if (kv.Key == "mysql数据库IP")
                    {
                        Button btn1 = new Button { Text = "测试", Left = inputControl.Right + 10, Top = y, Width = 100 };
                        btn1.Click += (s, e) => { inputControl.Text = "192.168.8.240"; };
                        panel.Controls.Add(btn1);

                        Button btn2 = new Button { Text = "工厂", Left = inputControl.Right + 10 + 100 + 10, Top = y };
                        btn2.Click += (s, e) => { inputControl.Text = "rm-8vbikz8103zit0xgs3o.mysql.zhangbei.rds.aliyuncs.com"; };
                        panel.Controls.Add(btn2);
                    }
                    else if (kv.Key == "型号")
                    {
                        Button btn1 = new Button { Text = "F100CKD", Left = inputControl.Right + 10, Top = y, Width = 100 };
                        btn1.Click += (s, e) => { inputControl.Text = btn1.Text; };
                        panel.Controls.Add(btn1);

                        Button btn2 = new Button { Text = "F200CKD", Left = inputControl.Right + 10 + 100 + 10, Top = y };
                        btn2.Click += (s, e) => { inputControl.Text = btn2.Text; };
                        panel.Controls.Add(btn2);
                    }
                    else if (kv.Key == "物料标签模板DateCode" || kv.Key.Contains("模板")) //选择一个文件
                    {
                        Button btn1 = new Button { Text = "浏览", Left = inputControl.Right + 10, Top = y, Width = 100 };
                        btn1.Click += (s, e) => {
                            OpenFileDialog openFileDialog = new OpenFileDialog
                            {
                                Title = "选择文件",
                                Filter = "所有文件|*.*|文本文件|*.txt|图片文件|*.jpg;*.png;*.gif",
                                InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
                            };

                            if (openFileDialog.ShowDialog() == DialogResult.OK)
                            {
                                string selectedFilePath = openFileDialog.FileName;
                                //MessageBox.Show("你选择的文件路径是: " + selectedFilePath);
                                inputControl.Text = selectedFilePath;
                            }
                        };
                        panel.Controls.Add(btn1);
                    }
                }
                y += 25;
            }
        }
    }

    private void SaveConfig()
    {
        foreach (var kv in controlMap)
        {
            string value;
            if (kv.Key is TextBox tb)
            {
                value = tb.Text;
            }
            else if (kv.Key is NumericUpDown num)
            {
                value = num.Value.ToString();
            }
            else if (kv.Key is CheckBox cb)
            {
                value = cb.Checked.ToString();
            }
            else
            {
                value = "";
            }
            Program.myIni.Write(kv.Value.Item1, kv.Value.Item2, value);
        }
        MessageBox.Show("配置已保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //重启自己
        Application.Restart();
    }
}