-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigDefault.cs
More file actions
145 lines (143 loc) · 5 KB
/
ConfigDefault.cs
File metadata and controls
145 lines (143 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Specialized;
namespace OpenINI
{
public partial class ConfigDefault : Form
{
public OpenINI opi;
public string section;
public string key;
public string skvalue;
public static string filePath = @".\ini";
public string fileName="configs.ini";
public ConfigDefault()
{
InitializeComponent();
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath); //创建目录
}
fileName = $"{filePath}\\{fileName}"; //创建对应目录的文件名称
if (!File.Exists(fileName)) File.Create(fileName);
opi = new OpenINI(fileName);
}
/// <summary>
/// 用于选择不是默认的配置文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSelectFile_Click(object sender, EventArgs e)
{
OpenFileDialog opn = new OpenFileDialog();
opn.Title = "选择配置文件";
opn.Filter = ".ini配置文件|*.ini";
if (opn.ShowDialog() == DialogResult.OK)
{
fileName = opn.FileName;
opi = new OpenINI(fileName);
}
}
/// <summary>
/// 配置写入按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnWriteConfig_Click(object sender, EventArgs e)
{
section = tbxSection.Text;
key= tbxKey.Text;
skvalue = tbxSKValues.Text;
if (section.Length == 0 || key.Length == 0 || skvalue.Length == 0) MessageBox.Show("你输入的其中一项为空,请确保节名,键名,值名不为空!", "写入提示!",MessageBoxButtons.OK,MessageBoxIcon.Warning);
opi.WriteINI(section,key,skvalue);
}
/// <summary>
/// 更新配置参数到key列表下拉菜单中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cbxSection_SelectedIndexChanged(object sender, EventArgs e)
{
section= cbxSection.SelectedItem.ToString();
StringCollection keys= opi.ReadAllKey(section);
cbxKey.Items.Clear();
foreach(var skvalues in keys)
{
cbxKey.Items.Add(skvalues);
}
}
/// <summary>
/// 更新配置参数到section列表下拉菜单中
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnReadConfig_Click(object sender, EventArgs e)
{
Select();
ClearText();
}
private void cbxKey_SelectedIndexChanged(object sender, EventArgs e)
{
section = cbxSection.Text;
key= cbxKey.Text;
if (opi.ItemExists(section,key)) MessageBox.Show("读取时,无该项!");
tbxSection.Text = section;
tbxKey.Text = key;
tbxSKValues.Text = opi.ReadIni(section, key);
}
/// <summary>
/// 删除配置,若只选择section项,只删除section项对应节名,若选择section和key项,删除对应section下的key的值(项)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDeleteConfig_Click(object sender, EventArgs e)
{
section = cbxSection.Text;
key = tbxKey.Text;
if (cbxSection.SelectedIndex > -1 && cbxKey.SelectedIndex ==-1) {
opi.DeleteSection(section);
}
else if ( cbxKey.SelectedIndex > -1)
{
opi.DeleteKey(section, key);
}
else
{
MessageBox.Show("你还未选择任何参数,请先选择!", "删除操作提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
}
/// <summary>
/// 窗体载入函数,初始读取默认配置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ConfigForm_Load(object sender, EventArgs e)
{
Select();
ClearText();
}
/// <summary>
/// 更新Section列表内容
/// </summary>
private void Select()
{
cbxSection.Items.Clear();
cbxKey.Items.Clear();
StringCollection sections = opi.ReadAllSections();
foreach (var sct in sections)
{
cbxSection.Items.Add(sct);
}
}
/// <summary>
/// 清空数据参数显示框
/// </summary>
private void ClearText()
{
tbxSection.Clear();
tbxKey.Clear();
tbxSKValues.Clear();
}
}
}