-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.Designer.cs
More file actions
242 lines (214 loc) · 7.79 KB
/
Main.Designer.cs
File metadata and controls
242 lines (214 loc) · 7.79 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using System.ComponentModel;
using System.Diagnostics;
using System.Net.NetworkInformation;
namespace NetworkMonitor
{
partial class Main
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
private BackgroundWorker backgroundWorker;
private static string logPath = Path.Combine(Directory.GetCurrentDirectory(), "logs.txt");
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new Container();
ComponentResourceManager resources = new ComponentResourceManager(typeof(Main));
reportBox = new TextBox();
backgroundWorker = new BackgroundWorker();
notifyIcon1 = new NotifyIcon(components);
SuspendLayout();
//
// reportBox
//
reportBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
reportBox.Location = new Point(12, 12);
reportBox.Multiline = true;
reportBox.Name = "reportBox";
reportBox.ScrollBars = ScrollBars.Vertical;
reportBox.Size = new Size(776, 426);
reportBox.TabIndex = 0;
reportBox.WordWrap = false;
//
// notifyIcon1
//
notifyIcon1.Icon = (Icon)resources.GetObject("notifyIcon1.Icon");
notifyIcon1.Text = "notifyIcon1";
notifyIcon1.Visible = true;
notifyIcon1.MouseDoubleClick += notifyIcon1_MouseDoubleClick;
notifyIcon1.MouseUp += notifyIcon1_MouseUp;
//
// Main
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(reportBox);
Icon = (Icon)resources.GetObject("$this.Icon");
Name = "Main";
Text = "Network status";
TopMost = true;
FormClosing += Main_FormClosing;
ResumeLayout(false);
PerformLayout();
//
// backgroundWorker
//
backgroundWorker = new BackgroundWorker
{
WorkerReportsProgress = true
};
backgroundWorker.DoWork += CheackBeat;
backgroundWorker.RunWorkerAsync();
}
private void CheackBeat(object sender, DoWorkEventArgs e)
{
int tested = 0;
while (true)
{
if (!CheckConnectivity())
{
try
{
if (tested > 4)
{
RestartNetworkAdapter();
tested = 0;
}
else
{
tested += 1;
}
}
catch (Exception ex)
{
var netowrkAdapterErr = ex.Message;
try {
SaveMessage(netowrkAdapterErr);
} catch (Exception ex2)
{
Console.WriteLine(ex2);
}
}
}
System.Threading.Thread.Sleep(5000);
}
}
private bool CheckConnectivity()
{
Ping ping = new Ping();
try
{
UpdateTextBox($"{DateTime.Now} - Checking connectivity...");
PingReply reply = ping.Send("8.8.8.8", 1000);
return (reply.Status == IPStatus.Success);
}
catch
{
UpdateTextBox($"{DateTime.Now} - Connection not succed...");
return false;
}
}
private void RestartNetworkAdapter()
{
UpdateTextBox("Restarting network adapter...");
var adapters = NetworkInterface.GetAllNetworkInterfaces();
foreach(var adapter in adapters) {
Process.Start("netsh", $"interface set interface \"{adapter.Name}\" admin=disable");
System.Threading.Thread.Sleep(5000);
Process.Start("netsh", $"interface set interface \"{adapter.Name}\" admin=enable");
}
RunCommand("ipconfig /renew");
string logMessage = $"{DateTime.Now}: Network adapter restarted.";
File.AppendAllText(logPath, logMessage + Environment.NewLine);
UpdateTextBox("Network adapter restarted.");
}
private static string RunCommand(string command)
{
Process process = new Process();
// Set the start info for the process
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "powershell.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
UseShellExecute = false
};
process.StartInfo = startInfo;
// Start the process
process.Start();
process.StandardInput.WriteLine(command);
// End the input stream
process.StandardInput.Close();
// Read the output of the PowerShell commands
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
private void UpdateTextBox(string text)
{
if (reportBox.InvokeRequired)
{
reportBox.Invoke(new Action<string>(UpdateTextBox), text);
}
else
{
reportBox.AppendText(text + Environment.NewLine);
}
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}
private void notifyIcon1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
ToolStripMenuItem exitToolStripMenuItem = new ToolStripMenuItem("Exit");
exitToolStripMenuItem.Click += new EventHandler(Exit_Click);
contextMenuStrip.Items.Add(exitToolStripMenuItem);
notifyIcon1.ContextMenuStrip = contextMenuStrip;
contextMenuStrip.Show(Cursor.Position);
}
}
private void Exit_Click(object Sender, EventArgs e)
{
Environment.Exit(0);
}
private void SaveMessage(string message)
{
if (!File.Exists(logPath))
{
using (FileStream fs = File.Create(logPath))
{
fs.Close();
}
}
File.AppendAllText(logPath, message);
}
#endregion
private TextBox reportBox;
private NotifyIcon notifyIcon1;
}
}