-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingForm.cs
More file actions
224 lines (196 loc) · 9.08 KB
/
SettingForm.cs
File metadata and controls
224 lines (196 loc) · 9.08 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using NetworkAdapterRouteControl.WinApi;
using System;
using System.Collections;
using System.Configuration;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Microsoft.Win32;
namespace NetworkAdapterRouteControl
{
public partial class SettingForm : Form
{
private string preRouteDestinationValue = String.Empty;
private bool ignoreRouteDestinationChange = false;
private readonly Regex routDestinationRegex = new Regex(@"^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
public SettingForm()
{
InitializeComponent();
}
private static String ReadSetting(string key)
{
try
{
var appSettings = ConfigurationManager.AppSettings;
return appSettings[key];
}
catch (ConfigurationErrorsException)
{
return String.Empty;
}
}
private bool IsAutorun()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (rk.GetValue(Application.ProductName) == null)
return false;
else
return true;
}
private void SettingForm_Load(object sender, EventArgs e)
{
routeDestinationListBox.Items.AddRange(ReadSetting("RouteDestinationList").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) as object[]);
if (routeDestinationListBox.Items.Count > 0) {
routeDestinationListBox.SelectedIndex = 0;
}
var adaptersInfo = IpHelper.GetAdaptersInfo();
adapterComboBox.DataSource = adaptersInfo;
adapterComboBox.SelectedIndex = adaptersInfo.FindIndex(i => (i.Description == ReadSetting("AdapterDescription")));
int value = int.MinValue;
if (!int.TryParse(ReadSetting("RouteMetric"), NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) { value = 1000; };
routeMetricNumericUpDown.Value = Convert.ToDecimal(value);
if (!int.TryParse(ReadSetting("InterfaceMetric"), NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) { value = 1000; };
interfaceMetricNumericUpDown.Value = Convert.ToDecimal(value);
if (!int.TryParse(ReadSetting("SyncPeriod"), NumberStyles.Integer, CultureInfo.InvariantCulture, out value)) { value = 100; };
syncPeriodNumericUpDown.Value = Convert.ToDecimal(value);
autorunCheckBox.Checked = IsAutorun();
}
private void CancelButton_Click(object sender, EventArgs e)
{
Close();
}
private void SaveButton_Click(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["RouteDestinationList"].Value =
String.Join(",", ((IEnumerable)routeDestinationListBox.Items).Cast<object>().Select(x => x.ToString()).ToArray());
config.AppSettings.Settings["AdapterDescription"].Value = (adapterComboBox.SelectedValue as AdapterInfo)?.Description;
config.AppSettings.Settings["RouteMetric"].Value = routeMetricNumericUpDown.Value.ToString(CultureInfo.InvariantCulture.NumberFormat);
config.AppSettings.Settings["InterfaceMetric"].Value = interfaceMetricNumericUpDown.Value.ToString(CultureInfo.InvariantCulture.NumberFormat);
config.AppSettings.Settings["SyncPeriod"].Value = syncPeriodNumericUpDown.Value.ToString(CultureInfo.InvariantCulture.NumberFormat);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
(Owner as MainForm)?.SyncSettings();
var isAutorun = IsAutorun();
var autorunPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
if (autorunCheckBox.Checked && !isAutorun)
{
var regKey = Registry.CurrentUser.OpenSubKey(autorunPath, true);
regKey.SetValue(Application.ProductName, "\"" + Application.ExecutablePath.ToString() + "\"", RegistryValueKind.String);
} else if (!autorunCheckBox.Checked && isAutorun) {
var regKey = Registry.CurrentUser.OpenSubKey(autorunPath, true);
regKey.DeleteValue(Application.ProductName, false);
}
Close();
}
private void AddRouteDestinationButton_Click(object sender, EventArgs e)
{
if (!routDestinationRegex.IsMatch(routeDestinationTextBox.Text) || !IPAddress.TryParse(routeDestinationTextBox.Text, out var routeDestination))
{
routeDestinationToolTip.Show("Неверный формат IP-адреса", routeDestinationTextBox, new Point(10, -25), 1000);
return;
}
routeDestinationListBox.Items.Add(routeDestination.ToString());
routeDestinationTextBox.Text = "";
}
private void RouteDestinationListBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
if (routeDestinationListBox.SelectedIndex >= 0)
{
var index = routeDestinationListBox.SelectedIndex;
routeDestinationListBox.Items.RemoveAt(index);
if (routeDestinationListBox.Items.Count > index)
{
routeDestinationListBox.SelectedIndex = index;
}
else if (routeDestinationListBox.Items.Count > 0)
{
routeDestinationListBox.SelectedIndex = routeDestinationListBox.Items.Count - 1;
}
}
}
}
private bool IsEditKey(KeyEventArgs e)
{
if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
{
return true;
}
else if (e.Modifiers == Keys.Control && (e.KeyCode == Keys.C ||
e.KeyCode == Keys.V ||
e.KeyCode == Keys.X))
{
return true;
}
else if (e.Modifiers == Keys.Shift && (e.KeyCode == Keys.Insert))
{
return true;
}
return false;
}
private bool IsEnterKey(KeyEventArgs e)
{
return e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return;
}
private bool IsForwardKey(KeyEventArgs e)
{
return e.KeyCode == Keys.Right || e.KeyCode == Keys.Left;
}
private bool IsNumericKey(KeyEventArgs e)
{
if (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) return true;
return e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9;
}
private bool IsDotKey(KeyEventArgs e)
{
return e.KeyCode == Keys.Space || e.KeyCode == Keys.OemPeriod || e.KeyCode == Keys.Decimal;
}
private bool IsReverseKey(KeyEventArgs e)
{
if (e.KeyCode == Keys.Left ||
e.KeyCode == Keys.Up)
{
return true;
}
return false;
}
private void RouteDestinationTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (!IsNumericKey(e) && !IsEditKey(e) && !IsDotKey(e) && !IsForwardKey(e)) e.SuppressKeyPress = true;
if (IsDotKey(e))
{
routeDestinationTextBox.AppendText(".");
e.SuppressKeyPress = true;
}
preRouteDestinationValue = routeDestinationTextBox.Text;
}
private void RouteDestinationTextBox_TextChanged(object sender, EventArgs e)
{
if (ignoreRouteDestinationChange)
{
ignoreRouteDestinationChange = false;
return;
}
var value = routeDestinationTextBox.Text;
if (value.Length == 0) return;
if (value.Length < 15)
{
var dotCount = value.Length - value.Replace(".", "").Length;
if (value.EndsWith(".") && dotCount < 4) value += "0";
for (var i = dotCount; i < 3; i++) value += ".0";
}
if (routDestinationRegex.IsMatch(value)) return;
var selectionStart = routeDestinationTextBox.SelectionStart;
ignoreRouteDestinationChange = true;
routeDestinationTextBox.Text = preRouteDestinationValue;
routeDestinationTextBox.SelectionStart = Math.Min(selectionStart, routeDestinationTextBox.Text.Length);
routeDestinationTextBox.SelectionLength = 0;
routeDestinationToolTip.Show("Неверный формат IP-адреса", routeDestinationTextBox, new Point(10, -25), 1000);
}
}
}