dotnet-读写ini配置文件-使用笔记2

这篇博客记录了C#中优化读写ini配置文件的方法,包括使用Dictionary<TKey, TValue>代替逐个读取参数,以及在保存时通过构造字符串对象提高效率。文章引用了相关文档并提供了修改后的iniFile类代码。" 113552264,10534959,深度学习学习率衰减策略详解,"['深度学习', '学习率调整', '优化策略']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

延续上一篇笔记:dotnet-读写ini配置文件-使用笔记 之后言:

1、读取参数信息的时候,下次尝试使用C#的键值对Dictionary<TKey,TValue>类试试。

2、关闭软件时,写信息是一个一个写的,下次尝试使用构造一个string对象,每个KeyValue键值对用字符'0'隔开,整体封装。

修改代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace IniFileDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List<string> portString = new List<string>()
        {
            "COM1",
            "COM2",
            "COM3",
            "COM4",
            "COM5",
            "COM6"
        };
        List<string> baudString = new List<string>()
        {
            "1200",
            "2400",
            "4800",
            "9600",
            "19200",
            "38400",
            "115200"
        };
        List<string> bitsString = new List<string>()
        {
            "5",
            "6",
            "7",
            "8",
        };
        List<string> parityString = new List<string>()
        {
            "NONE",
            "ODD",
            "EVEN",
        };
        List<string> stopString = new List<string>()
        {
            "1",
            "1.5",
            "2",
        };

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // 窗体加载时,给控件指定资源
            this.portComboBox.ItemsSource = portString;
            this.baudComboBox.ItemsSource = baudString;
            this.bitsComboBox.ItemsSource = bitsString;
            this.parityComboBox.ItemsSource = parityString;
            this.stopComboBox.ItemsSource = stopString;

            // 配置文件
            string strPath = AppDomain.CurrentDomain.BaseDirectory;
            IniFIle iniFIle = new IniFIle(strPath + @"Cfg.ini");

            // 获取CONFIG区域的信息,用来配置串口的通讯参数
            // 修改为Dictionary键值对使用
            Dictionary<string, string> keyValues = iniFIle.ReadIniFileKeyValues("CONFIG");
            foreach(KeyValuePair<string, string> keyValue in keyValues)
            {
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值