延续上一篇笔记: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)
{