This repository was archived by the owner on Nov 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerMainForm.FilesTab.cs
More file actions
484 lines (457 loc) · 18.9 KB
/
ServerMainForm.FilesTab.cs
File metadata and controls
484 lines (457 loc) · 18.9 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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Windows.Forms;
using Microsoft.VisualBasic;
namespace NostalgiaCoreGUI
{
public partial class ServerMainForm
{
private TabPage filesTabPage;
private Panel topPanel;
private Button btnUploadFiles;
private Button btnUploadFolder;
private Button btnDelete;
private Button btnCopy;
private Button btnCompress;
private Button btnBack;
private Button btnForward;
private Button btnReload;
private CheckBox chkSelectAll;
private ListView fileListView;
private ContextMenuStrip fileContextMenu;
private string currentPath;
private Stack<string> backStack = new Stack<string>();
private Stack<string> forwardStack = new Stack<string>();
internal void InitializeFilesTab()
{
filesTabPage = new TabPage("Files");
currentPath = serverPath;
topPanel = new Panel { Dock = DockStyle.Top, Height = 40 };
btnBack = new Button { Text = "←", Left = 5, Top = 5, Width = 40 };
btnForward = new Button { Text = "→", Left = 50, Top = 5, Width = 40 };
btnReload = new Button { Text = "⟳", Left = 95, Top = 5, Width = 40 };
btnUploadFiles = new Button { Text = "Upload Files", Left = 140, Top = 5, Width = 100 };
btnUploadFolder = new Button { Text = "Upload Folder", Left = 245, Top = 5, Width = 100 };
btnDelete = new Button { Text = "Delete", Left = 350, Top = 5, Width = 110, Visible = false };
btnCopy = new Button { Text = "Copy", Left = 465, Top = 5, Width = 80, Visible = false };
btnCompress = new Button { Text = "Compress", Left = 550, Top = 5, Width = 120, Visible = false };
chkSelectAll = new CheckBox { Text = "Select All", Left = 675, Top = 10, Width = 80 };
btnBack.Click += BtnBack_Click;
btnForward.Click += BtnForward_Click;
btnReload.Click += BtnReload_Click;
btnUploadFiles.Click += BtnUploadFiles_Click;
btnUploadFolder.Click += BtnUploadFolder_Click;
btnDelete.Click += BtnDeleteSelected_Click;
btnCopy.Click += BtnCopy_Click;
btnCompress.Click += BtnCreateArchive_Click;
chkSelectAll.CheckedChanged += ChkSelectAll_CheckedChanged;
topPanel.Controls.Add(btnBack);
topPanel.Controls.Add(btnForward);
topPanel.Controls.Add(btnReload);
topPanel.Controls.Add(btnUploadFiles);
topPanel.Controls.Add(btnUploadFolder);
topPanel.Controls.Add(btnDelete);
topPanel.Controls.Add(btnCopy);
topPanel.Controls.Add(btnCompress);
topPanel.Controls.Add(chkSelectAll);
fileListView = new ListView { Dock = DockStyle.Fill, View = View.Details, CheckBoxes = true };
fileListView.Columns.Add("Name", 250);
fileListView.Columns.Add("Date Modified", 150);
fileListView.Columns.Add("Size", 100);
fileListView.FullRowSelect = true;
fileListView.MultiSelect = true;
fileListView.HideSelection = false;
fileListView.DoubleClick += FileListView_DoubleClick;
fileListView.MouseClick += FileListView_MouseClick;
fileListView.ItemChecked += FileListView_ItemChecked;
fileContextMenu = new ContextMenuStrip();
fileContextMenu.Items.Add("Open").Click += FileOpen_Click;
fileContextMenu.Items.Add("Rename").Click += FileRename_Click;
fileContextMenu.Items.Add("Delete").Click += FileDelete_Click;
fileContextMenu.Items.Add("Compress").Click += FileCompress_Click;
fileContextMenu.Items.Add("Extract").Click += FileExtract_Click;
fileListView.ContextMenuStrip = fileContextMenu;
Panel mainPanel = new Panel { Dock = DockStyle.Fill };
mainPanel.Controls.Add(fileListView);
mainPanel.Controls.Add(topPanel);
filesTabPage.Controls.Add(mainPanel);
tabControl.TabPages.Add(filesTabPage);
LoadFileList();
}
private void LoadFileList()
{
fileListView.Items.Clear();
DirectoryInfo di = new DirectoryInfo(currentPath);
foreach (DirectoryInfo dir in di.GetDirectories())
{
ListViewItem lvi = new ListViewItem(dir.Name);
lvi.SubItems.Add(dir.LastWriteTime.ToString("G"));
lvi.SubItems.Add("Folder");
lvi.Tag = dir.FullName;
fileListView.Items.Add(lvi);
}
foreach (FileInfo file in di.GetFiles())
{
ListViewItem lvi = new ListViewItem(file.Name);
lvi.SubItems.Add(file.LastWriteTime.ToString("G"));
lvi.SubItems.Add(FormatSize(file.Length));
lvi.Tag = file.FullName;
fileListView.Items.Add(lvi);
}
UpdateActionButtons();
}
private string FormatSize(long bytes)
{
if (bytes < 1024)
return bytes + " B";
double kb = bytes / 1024.0;
if (kb < 1024)
return kb.ToString("F2") + " KB";
double mb = kb / 1024.0;
if (mb < 1024)
return mb.ToString("F2") + " MB";
double gb = mb / 1024.0;
return gb.ToString("F2") + " GB";
}
private void UpdateActionButtons()
{
bool hasSelection = fileListView.CheckedItems.Count > 0;
btnDelete.Visible = hasSelection;
btnCopy.Visible = hasSelection;
if (hasSelection)
{
bool allArchives = true;
foreach (ListViewItem item in fileListView.CheckedItems)
{
string path = item.Tag.ToString();
if (!File.Exists(path) || Path.GetExtension(path).ToLower() != ".zip")
{
allArchives = false;
break;
}
}
btnCompress.Text = allArchives ? "Extract" : "Compress";
btnCompress.Visible = true;
}
else
{
btnCompress.Visible = false;
}
}
private void FileListView_DoubleClick(object sender, EventArgs e)
{
if (fileListView.SelectedItems.Count == 0)
return;
ListViewItem item = fileListView.SelectedItems[0];
string path = item.Tag.ToString();
if (Directory.Exists(path))
{
backStack.Push(currentPath);
forwardStack.Clear();
currentPath = path;
LoadFileList();
}
else if (File.Exists(path))
{
OpenFile(path);
}
}
private void FileListView_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (fileListView.FocusedItem != null && fileListView.FocusedItem.Bounds.Contains(e.Location))
{
string path = fileListView.FocusedItem.Tag.ToString();
if (File.Exists(path) && Path.GetExtension(path).ToLower() == ".zip")
fileContextMenu.Items[4].Enabled = true;
else
fileContextMenu.Items[4].Enabled = false;
fileContextMenu.Show(Cursor.Position);
}
}
}
private void FileOpen_Click(object sender, EventArgs e)
{
if (fileListView.SelectedItems.Count == 0)
return;
ListViewItem item = fileListView.SelectedItems[0];
string path = item.Tag.ToString();
if (Directory.Exists(path))
{
backStack.Push(currentPath);
forwardStack.Clear();
currentPath = path;
LoadFileList();
}
else if (File.Exists(path))
{
OpenFile(path);
}
}
private void OpenFile(string path)
{
string ext = Path.GetExtension(path);
if (string.IsNullOrEmpty(ext))
System.Diagnostics.Process.Start("notepad.exe", path);
else
{
try
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo { FileName = path, UseShellExecute = true });
}
catch
{
System.Diagnostics.Process.Start("notepad.exe", path);
}
}
}
private void FileRename_Click(object sender, EventArgs e)
{
if (fileListView.SelectedItems.Count == 0)
return;
ListViewItem item = fileListView.SelectedItems[0];
string path = item.Tag.ToString();
string input = Interaction.InputBox("New name:", "Rename", Path.GetFileName(path));
if (!string.IsNullOrWhiteSpace(input))
{
string newPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), input);
if (File.Exists(path))
File.Move(path, newPath);
else if (Directory.Exists(path))
Directory.Move(path, newPath);
item.Text = input;
item.Tag = newPath;
LoadFileList();
}
}
private void FileDelete_Click(object sender, EventArgs e)
{
if (fileListView.SelectedItems.Count == 0)
return;
ListViewItem item = fileListView.SelectedItems[0];
string path = item.Tag.ToString();
if (File.Exists(path))
File.Delete(path);
else if (Directory.Exists(path))
Directory.Delete(path, true);
LoadFileList();
}
private void FileCompress_Click(object sender, EventArgs e)
{
if (fileListView.SelectedItems.Count == 0)
return;
ListViewItem item = fileListView.SelectedItems[0];
string path = item.Tag.ToString();
string zipPath = path + ".zip";
if (File.Exists(path))
{
using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Create))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
AddFileToZip(archive, path, System.IO.Path.GetFileName(path));
}
}
}
else if (Directory.Exists(path))
{
ZipFile.CreateFromDirectory(path, zipPath);
}
LoadFileList();
}
private void FileExtract_Click(object sender, EventArgs e)
{
if (fileListView.SelectedItems.Count == 0)
return;
foreach (ListViewItem item in fileListView.CheckedItems)
{
string path = item.Tag.ToString();
if (File.Exists(path) && Path.GetExtension(path).ToLower() == ".zip")
{
string extractDir = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), System.IO.Path.GetFileNameWithoutExtension(path));
if (!Directory.Exists(extractDir))
Directory.CreateDirectory(extractDir);
ZipFile.ExtractToDirectory(path, extractDir);
}
}
LoadFileList();
}
private void BtnUploadFiles_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog { Multiselect = true };
if (ofd.ShowDialog() == DialogResult.OK)
{
foreach (string file in ofd.FileNames)
{
string dest = System.IO.Path.Combine(currentPath, System.IO.Path.GetFileName(file));
File.Copy(file, dest, true);
}
LoadFileList();
}
}
private void BtnUploadFolder_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
string folderName = new DirectoryInfo(fbd.SelectedPath).Name;
string destDir = System.IO.Path.Combine(currentPath, folderName);
CopyDirectory(fbd.SelectedPath, destDir);
LoadFileList();
}
}
private void BtnBack_Click(object sender, EventArgs e)
{
if (backStack.Count > 0)
{
forwardStack.Push(currentPath);
currentPath = backStack.Pop();
LoadFileList();
}
}
private void BtnForward_Click(object sender, EventArgs e)
{
if (forwardStack.Count > 0)
{
backStack.Push(currentPath);
currentPath = forwardStack.Pop();
LoadFileList();
}
}
private void BtnReload_Click(object sender, EventArgs e)
{
LoadFileList();
}
private void BtnDeleteSelected_Click(object sender, EventArgs e)
{
if (fileListView.CheckedItems.Count == 0)
return;
foreach (ListViewItem item in fileListView.CheckedItems)
{
string source = item.Tag.ToString();
if (File.Exists(source))
File.Delete(source);
else if (Directory.Exists(source))
Directory.Delete(source, true);
}
LoadFileList();
}
private void BtnCopy_Click(object sender, EventArgs e)
{
if (fileListView.CheckedItems.Count == 0)
return;
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == DialogResult.OK)
{
foreach (ListViewItem item in fileListView.CheckedItems)
{
string source = item.Tag.ToString();
string dest = System.IO.Path.Combine(fbd.SelectedPath, System.IO.Path.GetFileName(source));
if (File.Exists(source))
File.Copy(source, dest, true);
else if (Directory.Exists(source))
CopyDirectory(source, dest);
}
}
}
private void BtnCreateArchive_Click(object sender, EventArgs e)
{
if (fileListView.CheckedItems.Count == 0)
return;
if (btnCompress.Text == "Extract")
{
BtnExtractSelected();
}
else
{
string defaultName = "Archive.zip";
if (fileListView.CheckedItems.Count == 1)
{
string source = fileListView.CheckedItems[0].Tag.ToString();
defaultName = Path.GetFileNameWithoutExtension(source) + ".zip";
}
SaveFileDialog sfd = new SaveFileDialog { Filter = "Zip files|*.zip", FileName = defaultName };
if (sfd.ShowDialog() == DialogResult.OK)
{
using (FileStream zipToCreate = new FileStream(sfd.FileName, FileMode.Create))
using (ZipArchive archive = new ZipArchive(zipToCreate, ZipArchiveMode.Create))
{
foreach (ListViewItem item in fileListView.CheckedItems)
{
string source = item.Tag.ToString();
if (File.Exists(source))
AddFileToZip(archive, source, Path.GetFileName(source));
else if (Directory.Exists(source))
AddDirectoryToZip(archive, source, Path.GetFileName(source));
}
}
}
}
}
private void BtnExtractSelected()
{
foreach (ListViewItem item in fileListView.CheckedItems)
{
string source = item.Tag.ToString();
if (File.Exists(source) && Path.GetExtension(source).ToLower() == ".zip")
{
string extractDir = Path.Combine(Path.GetDirectoryName(source), Path.GetFileNameWithoutExtension(source));
if (!Directory.Exists(extractDir))
Directory.CreateDirectory(extractDir);
ZipFile.ExtractToDirectory(source, extractDir);
}
}
LoadFileList();
}
private void ChkSelectAll_CheckedChanged(object sender, EventArgs e)
{
foreach (ListViewItem item in fileListView.Items)
item.Checked = chkSelectAll.Checked;
UpdateActionButtons();
}
private void FileListView_ItemChecked(object sender, ItemCheckedEventArgs e)
{
UpdateActionButtons();
}
private void CopyDirectory(string sourceDir, string targetDir)
{
Directory.CreateDirectory(targetDir);
foreach (string file in Directory.GetFiles(sourceDir))
{
string destFile = Path.Combine(targetDir, Path.GetFileName(file));
File.Copy(file, destFile, true);
}
foreach (string directory in Directory.GetDirectories(sourceDir))
{
string destDir = Path.Combine(targetDir, Path.GetFileName(directory));
CopyDirectory(directory, destDir);
}
}
private void AddFileToZip(ZipArchive archive, string source, string entryName)
{
using (FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
ZipArchiveEntry entry = archive.CreateEntry(entryName);
using (var entryStream = entry.Open())
fs.CopyTo(entryStream);
}
}
private void AddDirectoryToZip(ZipArchive archive, string sourceDir, string entryName)
{
DirectoryInfo di = new DirectoryInfo(sourceDir);
foreach (FileInfo file in di.GetFiles())
{
string relativePath = Path.Combine(entryName, file.Name);
AddFileToZip(archive, file.FullName, relativePath);
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
string relPath = Path.Combine(entryName, dir.Name);
AddDirectoryToZip(archive, dir.FullName, relPath);
}
}
}
}