-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
761 lines (711 loc) · 32.8 KB
/
Form1.cs
File metadata and controls
761 lines (711 loc) · 32.8 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
using static System.Net.WebRequestMethods;
using WinSCP;
using static System.Collections.Specialized.BitVector32;
using System.Diagnostics;
using Renci.SshNet.Security;
using System.Windows.Forms;
using System.Xml.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Reflection.Metadata.BlobBuilder;
using System;
using Renci.SshNet;
using System.Security.Policy;
using System.Net.NetworkInformation;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Microsoft.VisualBasic.Logging;
namespace FTPManagement
{
public partial class FTPManagement : Form
{
IniFile config = new IniFile("configuration.ini");
Dictionary<string, Dictionary<string, string>> configData = new Dictionary<string, Dictionary<string, string>>();
public FTPManagement()
{
InitializeComponent();
InitialSystem();
}
private void InitialSystem()
{
LoadConfig();
main.BringToFront();
}
private void btnAddProject_Click(object sender, EventArgs e)
{
txtbHostName.Text = "";
txtbPort.Text = "22";
txtbUsername.Text = "";
txtbPassword.Text = "";
ckIsPrivateKey.Checked = false;
txtbPPKFile.Text = "";
txtbTargetDir.Text = "";
txtbLocalDir.Text = "";
txtbConfigName.Text = "";
txtbIgnore.Text = "";
txtbScriptDir.Text = "";
enableFTPFORM();
ftpConfig.BringToFront();
}
private void btnOpenFileFTP_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
InitialDirectory = "c:\\",
Filter = "PPK files (*.ppk)|*.ppk",
FilterIndex = 2,
RestoreDirectory = true
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
txtbPPKFile.Text = filePath;
}
}
private void btnOpenDirLocal_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
{
folderBrowserDialog.Description = "Select a folder";
folderBrowserDialog.RootFolder = Environment.SpecialFolder.MyComputer;
if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string folderPath = folderBrowserDialog.SelectedPath;
txtbLocalDir.Text = folderPath;
}
}
}
private void btnCancelFTPConfig_Click(object sender, EventArgs e)
{
main.BringToFront();
}
private void btnTestConnectFTP_Click(object sender, EventArgs e)
{
tsStatusOther.Text = "Test config FTP...";
Invoke(new Action(() => tsProgressBar.Value = 50));
string privateKeyPath = ckIsPrivateKey.Checked ? txtbPPKFile.Text : "";
try
{
Console.WriteLine(txtbHostName.Text);
TSFTP sftp = new TSFTP(txtbHostName.Text, txtbPort.Text, txtbUsername.Text, txtbPassword.Text, privateKeyPath);
WinSCP.Session session = sftp.Connect();
Invoke(new Action(() => tsProgressBar.Value = 100));
if (sftp.IsConnected())
{
sftp.Disconnect();
MessageBox.Show("Connection success!!", "SFTP Connection Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
sftp.Disconnect();
tsStatusOther.Text = "Connection fail!!";
tsStatusOther.ForeColor = Color.Red;
MessageBox.Show("Connection fail!!", "SFTP Connection Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.ForeColor = Color.Black;
tsStatusOther.Text = "";
}
catch (Exception ex)
{
tsStatusOther.Text = "Connection fail!!";
tsStatusOther.ForeColor = Color.Red;
DialogResult error = MessageBox.Show("Connection fail!!", "SFTP Connection Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (error == DialogResult.OK)
{
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.ForeColor = Color.Black;
tsStatusOther.Text = "";
}
}
}
private void btnSaveFTPConfig_Click(object sender, EventArgs e)
{
tsStatusOther.Text = "Update config FTP...";
string section = "SFTP";
Invoke(new Action(() => tsProgressBar.Value = 30));
if (txtbConfigName.Text.Equals(string.Empty))
{
MessageBox.Show("Invalid Name!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.Text = "";
return;
}
if (txtbHostName.Text.Equals(string.Empty))
{
MessageBox.Show("Invalid Host!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.Text = "";
return;
}
if (txtbUsername.Text.Equals(string.Empty))
{
MessageBox.Show("Invalid Username!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.Text = "";
return;
}
if (txtbLocalDir.Text.Equals(string.Empty))
{
MessageBox.Show("Invalid local directory!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.Text = "";
return;
}
if (txtbTargetDir.Text.Equals(string.Empty))
{
MessageBox.Show("Invalid target directory!!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.Text = "";
return;
}
Invoke(new Action(() => tsProgressBar.Value = 50));
string prot = string.IsNullOrEmpty(txtbPort.Text) ? "22" : txtbPort.Text;
string configName = $"SFTP_{txtbConfigName.Text.ToUpper()}";
config.Write("host", txtbHostName.Text, configName);
config.Write("port", prot, configName);
config.Write("username", txtbUsername.Text, configName);
config.Write("password", txtbPassword.Text, configName);
config.Write("localDirectory", txtbLocalDir.Text, configName);
config.Write("targetDirectory", txtbTargetDir.Text, configName);
config.Write("usePrivateKey", ckIsPrivateKey.Checked.ToString(), configName);
config.Write("privateKeyPath", txtbPPKFile.Text, configName);
config.Write("configName", txtbConfigName.Text, configName);
config.Write("scriptPath", txtbScriptDir.Text, configName);
config.Write("ignore", txtbIgnore.Text, configName);
Invoke(new Action(() => tsProgressBar.Value = 80));
LoadConfig(section, txtbConfigName.Text);
LoadConfig();
Invoke(new Action(() => tsProgressBar.Value = 100));
MessageBox.Show("Success to Update!!", "Success", MessageBoxButtons.OK);
main.BringToFront();
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.Text = "";
}
private void LoadSFTPConfig(string session)
{
configData[session] = new Dictionary<string, string>();
configData[session]["host"] = config.Read("host", session);
configData[session]["port"] = config.Read("port", session);
configData[session]["username"] = config.Read("username", session);
configData[session]["password"] = config.Read("password", session);
configData[session]["usePrivateKey"] = config.Read("usePrivateKey", session);
configData[session]["privateKeyPath"] = config.Read("privateKeyPath", session);
configData[session]["targetDirectory"] = config.Read("targetDirectory", session);
configData[session]["localDirectory"] = config.Read("localDirectory", session);
configData[session]["configName"] = config.Read("configName", session);
configData[session]["scriptPath"] = config.Read("scriptPath", session);
configData[session]["ignore"] = config.Read("ignore", session);
if (!configData[session]["usePrivateKey"].ToLower().Equals("true"))
{
configData[session]["privateKeyPath"] = "";
}
}
private void LoadConfig(string _section = "", string option = null)
{
string section;
section = "SFTP";
if (_section.ToLower().Equals(section.ToLower()) || _section.Equals(string.Empty))
{
if (option != null)
{
string configName = $"{section}_{option}";
LoadSFTPConfig(configName);
txtbHostName.Text = configData[configName]["host"];
txtbPort.Text = configData[configName]["port"];
txtbUsername.Text = configData[configName]["username"];
txtbPassword.Text = configData[configName]["password"];
ckIsPrivateKey.Checked = configData[configName]["usePrivateKey"] == "True" ? true : false;
txtbPPKFile.Text = configData[configName]["privateKeyPath"];
txtbTargetDir.Text = configData[configName]["targetDirectory"];
txtbLocalDir.Text = configData[configName]["localDirectory"];
txtbConfigName.Text = configData[configName]["configName"];
txtbScriptDir.Text = configData[configName]["scriptPath"];
txtbIgnore.Text = configData[configName]["ignore"];
}
else
{
List<string> sections = config.GetAllSections();
var sftpKeys = sections.Where(key => key.StartsWith("SFTP_"));
foreach (var key in sftpKeys)
{
LoadSFTPConfig(key);
}
}
}
section = "LIST_SFTP";
if (_section.ToLower().Equals(section.ToLower()) || _section.Equals(string.Empty))
{
Dictionary<string, Dictionary<string, string>> projectData = new Dictionary<string, Dictionary<string, string>>();
List<string> sections = config.GetAllSections();
var sftpKeys = sections.Where(key => key.StartsWith("SFTP_"));
lsbProjectList.Items.Clear();
foreach (var key in sftpKeys)
{
if (configData.ContainsKey(key))
{
string name = configData[key]["configName"];
string host = configData[key]["host"];
string username = configData[key]["username"];
string port = configData[key]["port"];
string localDir = configData[key]["localDirectory"];
string tagetDir = configData[key]["targetDirectory"];
string scripDir = configData[key]["scriptPath"];
string ignoreFile = configData[key]["ignore"];
projectData[name] = new Dictionary<string, string>
{
{ "host", host },
{ "port", port },
{ "username", username },
{ "localDir", localDir },
{ "targetDir", tagetDir },
{ "ignore", ignoreFile },
{ "scriptDir", scripDir },
};
lsbProjectList.Items.Add(name);
}
}
if (lsbProjectList.Items.Count > 0)
{
lsbProjectList.SelectedIndex = 0;
string selectedName = lsbProjectList.Text;
if (projectData.ContainsKey(selectedName))
{
var data = projectData[selectedName];
int maxLength = 15;
lblHostCurrentName.Text = $"Host Name: {selectedName}";
lblHostCurrent.Text = $"Host: {data.GetValueOrDefault("host", "-")}:{data.GetValueOrDefault("port", "-")}";
lblUserNameCurrent.Text = $"Username: {data.GetValueOrDefault("username", "-")}";
lblLocalDirCurrent.Text = $"Local Dir: {TruncateText(data.GetValueOrDefault("localDir", "-"), maxLength)}";
lblTragetDirCurrent.Text = $"Target Dir: {TruncateText(data.GetValueOrDefault("targetDir", "-"), maxLength)}";
lblScriptDitCurrent.Text = $"Script Dir: {TruncateText(data.GetValueOrDefault("scriptDir", "-"), maxLength)}";
lblIgnoreFileCureent.Text = $"Ignore File: {TruncateText(data.GetValueOrDefault("ignore", "-"), maxLength)}";
}
}
}
section = "WEB_CONFIG";
if (_section.ToLower().Equals(section.ToLower()) || _section.Equals(string.Empty))
{
if (option != null)
{
string configName = $"{section}_{option}";
configData[configName] = new Dictionary<string, string>();
configData[configName]["domain"] = config.Read("domain", configName);
txtbDomain.Text = configData[configName]["domain"];
}
else
{
List<string> sections = config.GetAllSections();
var sftpKeys = sections.Where(key => key.StartsWith("WEB_CONFIG_"));
foreach (var key in sftpKeys)
{
configData[key] = new Dictionary<string, string>();
configData[key]["domain"] = config.Read("domain", key);
}
}
}
}
private string TruncateText(string text, int maxLength)
{
if (string.IsNullOrEmpty(text)) return "-";
return text.Length > maxLength ? text.Substring(0, maxLength) + "..." : text;
}
private void btnEditProject_Click(object sender, EventArgs e)
{
if (lsbProjectList.SelectedItem != null)
{
enableFTPFORM();
string selectedItem = lsbProjectList.SelectedItem.ToString();
if (selectedItem != null)
{
LoadConfig("SFTP", selectedItem);
ftpConfig.BringToFront();
}
}
}
private void btnDeleteFTP_Click(object sender, EventArgs e)
{
tsStatusOther.Text = "Deleted FTP...";
DialogResult result = MessageBox.Show("Success to Deleted!!", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
Invoke(new Action(() => tsProgressBar.Value = 50));
string selectedItem = lsbProjectList.SelectedItem.ToString();
string configName = $"SFTP_{selectedItem.ToUpper()}";
config.DeleteKey("host", configName);
config.DeleteKey("port", configName);
config.DeleteKey("username", configName);
config.DeleteKey("password", configName);
config.DeleteKey("localDirectory", configName);
config.DeleteKey("targetDirectory", configName);
config.DeleteKey("usePrivateKey", configName);
config.DeleteKey("privateKeyPath", configName);
config.DeleteKey("configName", configName);
config.DeleteKey("scriptPath", configName);
config.DeleteKey("txtbIgnore", configName);
config.DeleteSection(configName);
lsbProjectList.Items.Remove(selectedItem);
Invoke(new Action(() => tsProgressBar.Value = 100));
}
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.Text = "";
}
private void enableFTPFORM()
{
txtbHostName.ReadOnly = false;
txtbPort.ReadOnly = false;
txtbUsername.ReadOnly = false;
txtbPassword.ReadOnly = false;
ckIsPrivateKey.Enabled = true;
txtbPPKFile.ReadOnly = false;
txtbTargetDir.ReadOnly = false;
txtbLocalDir.ReadOnly = false;
txtbConfigName.ReadOnly = false;
btnSaveFTPConfig.Visible = true;
btnOpenFileFTP.Visible = true;
btnOpenDirLocal.Visible = true;
btnUploadFTP.Visible = false;
btnCmd.Visible = false;
btnDropFTP.Visible = false;
txtbScriptDir.ReadOnly = false;
txtbIgnore.ReadOnly = false;
}
private void disableFTPFORM()
{
txtbHostName.ReadOnly = true;
txtbPort.ReadOnly = true;
txtbUsername.ReadOnly = true;
txtbPassword.ReadOnly = true;
ckIsPrivateKey.Enabled = false;
txtbPPKFile.ReadOnly = true;
txtbTargetDir.ReadOnly = true;
txtbLocalDir.ReadOnly = true;
txtbConfigName.ReadOnly = true;
btnSaveFTPConfig.Visible = false;
btnOpenFileFTP.Visible = false;
btnOpenDirLocal.Visible = false;
btnUploadFTP.Visible = true;
btnCmd.Visible = true;
btnDropFTP.Visible = true;
txtbScriptDir.ReadOnly = true;
txtbIgnore.ReadOnly = true;
}
private void btnDetailFTP_Click(object sender, EventArgs e)
{
if (lsbProjectList.SelectedItem != null)
{
string selectedItem = lsbProjectList.SelectedItem.ToString();
if (selectedItem != null)
{
LoadConfig("SFTP", selectedItem);
ftpConfig.BringToFront();
disableFTPFORM();
}
}
}
private void uploadFTP()
{
try
{
string selectedItem = lsbProjectList.SelectedItem.ToString();
if (selectedItem.Equals(string.Empty))
{
return;
}
logs.BringToFront();
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.Text = "Starting connect...";
string configName = $"SFTP_{selectedItem.ToUpper()}";
LoadConfig("SFTP", selectedItem);
TSFTP sftp = new TSFTP(configData[configName]["host"], configData[configName]["port"], configData[configName]["username"], configData[configName]["password"], configData[configName]["privateKeyPath"]);
WinSCP.Session session = sftp.Connect();
Invoke(new Action(() => tsProgressBar.Value = 100));
tsStatusOther.Text = "Starting upload...";
sftp.OnProgressReport += UpdateProgress;
string[] ignore = null;
if (!configData[configName]["ignore"].Equals(string.Empty))
{
ignore = configData[configName]["ignore"].Split(',').Select(s => s.Trim()).ToArray();
}
sftp.UploadFTP(configData[configName]["localDirectory"], configData[configName]["targetDirectory"], ignore);
if (tsProgressBar.Value >= 100)
{
MessageBox.Show("Upload success!!", "Upload FTP", MessageBoxButtons.OK);
}
else
{
MessageBox.Show("Upload fail!!", "Upload FTP", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show("Upload fail!!", "Upload FTP", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.ForeColor = Color.Black;
tsStatusOther.Text = "";
rtbLogs.Clear();
}
private void btnUploadFTP_Click(object sender, EventArgs e)
{
uploadFTP();
ftpConfig.BringToFront();
}
private void UpdateProgress(string message, int percentage, bool status = true)
{
if (InvokeRequired)
{
Invoke(new Action(() => UpdateProgress(message, percentage, status)));
return;
}
tsStatusOther.ForeColor = status ? Color.Green : Color.Red;
tsProgressBar.Value = percentage;
rtbLogs.AppendText(message + Environment.NewLine);
rtbLogs.ForeColor = status ? Color.Green : Color.Red;
rtbLogs.ScrollToCaret();
}
private void runCmdFTP()
{
try
{
string selectedItem = lsbProjectList.SelectedItem.ToString();
if (selectedItem.Equals(string.Empty))
{
return;
}
logs.BringToFront();
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.Text = "Starting connect...";
string configName = $"SFTP_{selectedItem.ToUpper()}";
LoadConfig("SFTP", selectedItem);
TSFTP sftp = new TSFTP(configData[configName]["host"], configData[configName]["port"], configData[configName]["username"], configData[configName]["password"], configData[configName]["privateKeyPath"]);
WinSCP.Session session = sftp.Connect();
Invoke(new Action(() => tsProgressBar.Value = 100));
tsStatusOther.Text = "Starting cmd script...";
sftp.OnProgressReport += UpdateProgress;
sftp.CmdFTP(configData[configName]["scriptPath"]);
MessageBox.Show("Run Cmd success!!", "CMD FTP", MessageBoxButtons.OK);
}
catch
{
MessageBox.Show("Run Cmd fail!!", "CMD FTP", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
tsStatusOther.ForeColor = Color.Black;
tsStatusOther.Text = "";
rtbLogs.Clear();
Invoke(new Action(() => tsProgressBar.Value = 0));
}
private void btnCmd_Click(object sender, EventArgs e)
{
runCmdFTP();
ftpConfig.BringToFront();
}
private void dropFTP()
{
try
{
string selectedItem = lsbProjectList.SelectedItem.ToString();
if (selectedItem.Equals(string.Empty))
{
return;
}
logs.BringToFront();
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.Text = "Starting connect...";
string configName = $"SFTP_{selectedItem.ToUpper()}";
LoadConfig("SFTP", selectedItem);
TSFTP sftp = new TSFTP(configData[configName]["host"], configData[configName]["port"], configData[configName]["username"], configData[configName]["password"], configData[configName]["privateKeyPath"]);
WinSCP.Session session = sftp.Connect();
Invoke(new Action(() => tsProgressBar.Value = 100));
tsStatusOther.Text = "Starting delete folder...";
sftp.OnProgressReport += UpdateProgress;
sftp.RemoveFTP(configData[configName]["targetDirectory"]);
MessageBox.Show("Upload success!!", "Upload FTP", MessageBoxButtons.OK);
}
catch (Exception ex)
{
MessageBox.Show("Upload fail!!", "Upload FTP", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
tsStatusOther.ForeColor = Color.Black;
tsStatusOther.Text = "";
rtbLogs.Clear();
Invoke(new Action(() => tsProgressBar.Value = 0));
}
private void btnDropFTP_Click(object sender, EventArgs e)
{
dropFTP();
ftpConfig.BringToFront();
}
private void btnQtUpload_Click(object sender, EventArgs e)
{
DialogResult confirm = MessageBox.Show("Upload!!", "Upload", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (confirm == DialogResult.Yes)
{
uploadFTP();
main.BringToFront();
}
}
private void btnQtDrop_Click(object sender, EventArgs e)
{
DialogResult confirm = MessageBox.Show("Drop!!", "Drop", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (confirm == DialogResult.Yes)
{
dropFTP();
main.BringToFront();
}
}
private void btnQtCmd_Click(object sender, EventArgs e)
{
DialogResult confirm = MessageBox.Show("Run Cmd!!", "CMD", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (confirm == DialogResult.Yes)
{
runCmdFTP();
main.BringToFront();
}
}
private void btnWebConfig_Click(object sender, EventArgs e)
{
string section = "WEB_CONFIG";
string selectedItem = lsbProjectList.SelectedItem.ToString();
if (selectedItem.Equals(string.Empty))
{
return;
}
LoadConfig("WEB_CONFIG", selectedItem);
webConfig.BringToFront();
}
private void brnSaveWebConfig_Click(object sender, EventArgs e)
{
tsStatusOther.Text = "Start save web config ...";
string selectedItem = lsbProjectList.SelectedItem.ToString();
if (selectedItem.Equals(string.Empty))
{
return;
}
string section = "WEB_CONFIG";
Invoke(new Action(() => tsProgressBar.Value = 0));
string configName = $"WEB_CONFIG_{selectedItem.ToUpper()}";
config.Write("domain", txtbDomain.Text, configName);
config.Write("configName", selectedItem, configName);
Invoke(new Action(() => tsProgressBar.Value = 80));
LoadConfig(section, selectedItem);
tsStatusOther.ForeColor = Color.Green;
Invoke(new Action(() => tsProgressBar.Value = 100));
MessageBox.Show("Success to Update!!", "Success", MessageBoxButtons.OK);
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.ForeColor = Color.Black;
tsStatusOther.Text = "";
}
private void btnWebCancel_Click(object sender, EventArgs e)
{
main.BringToFront();
}
private void btnOpenWeb_Click(object sender, EventArgs e)
{
string selectedItem = lsbProjectList.SelectedItem.ToString();
if (selectedItem.Equals(string.Empty))
{
return;
}
string configName = $"WEB_CONFIG_{selectedItem.ToUpper()}";
Process.Start(new ProcessStartInfo
{
FileName = configData[configName]["domain"],
UseShellExecute = true
});
}
private void btnPingWeb_Click(object sender, EventArgs e)
{
try
{
string selectedItem = lsbProjectList.SelectedItem.ToString();
if (selectedItem.Equals(string.Empty))
{
return;
}
logs.BringToFront();
rtbLogs.Clear();
tsStatusOther.Text = "Start ping ...";
UpdateProgress("Start ping...", 0);
string configName = $"WEB_CONFIG_{selectedItem.ToUpper()}";
Ping pingSender = new Ping();
string server = configData[configName]["domain"];
PingReply reply = pingSender.Send(server);
UpdateProgress($"Check config ping to {server}", 50);
// Check the status of the ping reply
if (reply.Status == IPStatus.Success)
{
UpdateProgress($"Ping to {server} successful", 100);
UpdateProgress($"Roundtrip time: {reply.RoundtripTime} ms", 100);
UpdateProgress($"Time to live: {reply.Options.Ttl}", 100);
UpdateProgress($"Don't fragment: {reply.Options.DontFragment}", 100);
UpdateProgress($"Buffer size: {reply.Buffer.Length}", 100);
UpdateProgress($"Ping success...", 100);
tsStatusOther.Text = "Ping success...";
MessageBox.Show("Ping success!!", "Ping", MessageBoxButtons.OK);
}
else
{
tsStatusOther.Text = "Ping fail...";
UpdateProgress($"Ping to {configData[configName]["domain"]} failed. Status: {reply.Status}", 0, false);
MessageBox.Show("Ping fail!!", "Ping", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
tsStatusOther.Text = "Ping fail...";
UpdateProgress("An error occurred: " + ex.Message, 0, false);
MessageBox.Show("Ping fail!!", "Ping", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Invoke(new Action(() => tsProgressBar.Value = 0));
tsStatusOther.ForeColor = Color.Black;
tsStatusOther.Text = "";
rtbLogs.Clear();
webConfig.BringToFront();
}
private void btnWebCancel_Click_1(object sender, EventArgs e)
{
main.BringToFront();
}
private void btnOpenCmd_Click(object sender, EventArgs e)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
UseShellExecute = true
};
Process process = new Process
{
StartInfo = processStartInfo
};
process.Start();
}
private void lsbProjectList_SelectedIndexChanged(object sender, EventArgs e)
{
if (lsbProjectList.SelectedItem == null) return;
string selectedItem = lsbProjectList.SelectedItem.ToString();
LoadConfig("SFTP", selectedItem);
string configName = $"SFTP_{selectedItem}";
if (configData.ContainsKey(configName))
{
var configSection = configData[configName];
var data = new Dictionary<string, string>
{
["host"] = configSection["host"],
["port"] = configSection["port"],
["username"] = configSection["username"],
["localDir"] = configSection["localDirectory"],
["targetDir"] = configSection["targetDirectory"],
["scriptDir"] = configSection["scriptPath"],
["ignore"] = configSection["ignore"]
};
const int MAX_DISPLAY_LENGTH = 15;
lblHostCurrentName.Text = $"Host Name: {selectedItem}";
lblHostCurrent.Text = $"Host: {data.GetValueOrDefault("host", "-")}:{data.GetValueOrDefault("port", "-")}";
lblUserNameCurrent.Text = $"Username: {data.GetValueOrDefault("username", "-")}";
lblLocalDirCurrent.Text = $"Local Dir: {TruncateText(data.GetValueOrDefault("localDir", "-"), MAX_DISPLAY_LENGTH)}";
lblTragetDirCurrent.Text = $"Target Dir: {TruncateText(data.GetValueOrDefault("targetDir", "-"), MAX_DISPLAY_LENGTH)}";
lblScriptDitCurrent.Text = $"Script Dir: {TruncateText(data.GetValueOrDefault("scriptDir", "-"), MAX_DISPLAY_LENGTH)}";
lblIgnoreFileCureent.Text = $"Ignore File: {TruncateText(data.GetValueOrDefault("ignore", "-"), MAX_DISPLAY_LENGTH)}";
}
}
}
}