-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMainForm.cs
More file actions
690 lines (603 loc) · 27.5 KB
/
Copy pathMainForm.cs
File metadata and controls
690 lines (603 loc) · 27.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Windows.Forms;
using System.Diagnostics;
using System.Text.RegularExpressions;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace TiKey
{
public partial class MainForm : Form
{
private const int WM_DEVICECHANGE = 0x0219;
private const int DBT_DEVICEARRIVAL = 0x8000;
private const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
private const string USB_INTERFACE_TYPE = "USB";
private const string VERACRYPT_HIDDEN_FOLDER = ".DO_NOT_DELETE";
private const string AUTORUN_FOLDER = "AUTORUN.INF";
private const string DEFAULT_PASSWORD = "111";
public MainForm()
{
InitializeComponent();
LoadUSBDevices();
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_DEVICECHANGE)
{
int wParam = m.WParam.ToInt32();
if (wParam == DBT_DEVICEARRIVAL || wParam == DBT_DEVICEREMOVECOMPLETE)
{
// Sử dụng BeginInvoke để tránh xung đột luồng
BeginInvoke(new Action(LoadUSBDevices));
}
}
}
private void LoadUSBDevices()
{
LogMessage("Đang tải danh sách thiết bị USB...");
cbbUSB.Items.Clear();
var usbDevices = new HashSet<string>(); // Sử dụng HashSet để tránh trùng lặp
try
{
using (var searcher = new ManagementObjectSearcher("SELECT Model FROM Win32_DiskDrive WHERE InterfaceType='" + USB_INTERFACE_TYPE + "'"))
{
foreach (ManagementObject device in searcher.Get())
{
string name = device["Model"]?.ToString();
if (!string.IsNullOrWhiteSpace(name))
{
usbDevices.Add(name);
}
}
}
// Thêm các thiết bị vào ComboBox
foreach (var device in usbDevices)
{
cbbUSB.Items.Add(device);
}
// Cập nhật chọn lựa
cbbUSB.SelectedIndex = cbbUSB.Items.Count > 0 ? 0 : -1;
cbbUSB.Text = cbbUSB.SelectedIndex == -1 ? "" : cbbUSB.SelectedItem.ToString();
LogMessage($"Đã tìm thấy {usbDevices.Count} thiết bị USB.");
}
catch (Exception ex)
{
LogMessage($"Lỗi khi tải danh sách USB: {ex.Message}", true);
MessageBox.Show($"Lỗi khi tải danh sách USB: {ex.Message}", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnReload_Click(object sender, EventArgs e)
{
LoadUSBDevices();
}
private int ExtractDiskNumber(string deviceId)
{
if (string.IsNullOrEmpty(deviceId)) return -1;
var match = Regex.Match(deviceId, @"PHYSICALDRIVE(\d+)");
return match.Success ? int.Parse(match.Groups[1].Value) : -1;
}
private List<string> GetUSBDriveLetters()
{
var result = new List<string>();
try
{
using (var searcher = new ManagementObjectSearcher("SELECT DeviceID FROM Win32_LogicalDisk WHERE DriveType = 2"))
{
foreach (ManagementObject disk in searcher.Get())
{
string driveLetter = disk["DeviceID"]?.ToString();
if (!string.IsNullOrEmpty(driveLetter))
{
result.Add(driveLetter);
}
}
}
}
catch (Exception ex)
{
LogMessage($"Lỗi khi tìm ký tự ổ đĩa USB: {ex.Message}", true);
MessageBox.Show($"Lỗi khi tìm ký tự ổ đĩa USB: {ex.Message}", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return result;
}
private async Task<List<string>> WaitForUSBDriveLetters(int maxAttempts = 5)
{
LogMessage("Đang chờ hệ thống nhận diện USB...");
List<string> usbDrives = new List<string>();
int attempts = 0;
while (usbDrives.Count == 0 && attempts < maxAttempts)
{
usbDrives = GetUSBDriveLetters();
if (usbDrives.Count == 0)
{
// Chờ một khoảng thời gian trước khi thử lại
LogMessage($"Đang thử lại ({attempts + 1}/{maxAttempts})...");
await Task.Delay(1000);
attempts++;
}
}
if (usbDrives.Count > 0)
{
LogMessage($"Đã tìm thấy ổ USB: {string.Join(", ", usbDrives)}");
}
else
{
LogMessage("Không tìm thấy ổ USB sau nhiều lần thử.", true);
}
return usbDrives;
}
private async Task<bool> CreateVeraCryptContainer(string driveLetter)
{
try
{
LogMessage("Đang kiểm tra ổ đĩa...");
UpdateProgress(60);
// Kiểm tra ổ đĩa
DriveInfo drive = new DriveInfo(driveLetter);
if (!drive.IsReady)
{
LogMessage($"Ổ đĩa {driveLetter} chưa sẵn sàng.", true);
MessageBox.Show($"Ổ đĩa {driveLetter} chưa sẵn sàng.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
// Đường dẫn đến VeraCryptFormat.exe nằm trên USB
string hiddenFolder = Path.Combine(driveLetter, VERACRYPT_HIDDEN_FOLDER);
string veraCryptExe = Path.Combine(hiddenFolder, "VeraCryptFormat.exe");
string containerPath = Path.Combine(hiddenFolder, "vc-container.vc");
if (!File.Exists(veraCryptExe))
{
LogMessage("Không tìm thấy VeraCryptFormat.exe trên USB!", true);
MessageBox.Show("Không tìm thấy VeraCryptFormat.exe trên USB!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
// Chuẩn bị tham số
long availableSpace = drive.AvailableFreeSpace;
LogMessage($"Không gian khả dụng: {availableSpace / (1024 * 1024)} MB");
LogMessage("Đang tạo container...");
UpdateProgress(70);
string arguments = $"/create \"{containerPath}\" /size \"{availableSpace / (1024 * 1024)}\"M /password \"{txtPw1.Text}\" /encryption AES /filesystem FAT /quick /FastCreateFile /silent";
// Chuẩn bị thông tin tiến trình
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = veraCryptExe,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
};
// Thực thi tiến trình
using (Process process = Process.Start(psi))
{
LogMessage("Đang tạo container (quá trình này có thể mất vài phút)...");
await Task.Run(() => process.WaitForExit());
bool success = process.ExitCode == 0;
if (success)
{
LogMessage("Tạo container thành công.");
UpdateProgress(90);
}
else
{
LogMessage($"Lỗi khi tạo container(Exit code: {process.ExitCode}).", true);
}
return success;
}
}
catch (Exception ex)
{
LogMessage($"Lỗi khi tạo container: {ex.Message}", true);
MessageBox.Show($"Lỗi khi tạo container: {ex.Message}", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
private async Task CopyDirectoryAsync(string sourceDir, string destDir)
{
LogMessage($"Đang sao chép dữ liệu từ {sourceDir} đến {destDir}...");
UpdateProgress(40);
await Task.Run(() => {
CopyDirectoryInternal(sourceDir, destDir);
});
// Ẩn các thư mục quan trọng
if (Directory.Exists(destDir))
{
LogMessage("Đang cấu hình thuộc tính thư mục...");
UpdateProgress(50);
HideDirectory(destDir, VERACRYPT_HIDDEN_FOLDER);
HideDirectory(destDir, AUTORUN_FOLDER);
}
}
private void CopyDirectoryInternal(string sourceDir, string destDir)
{
try
{
// Kiểm tra thư mục nguồn
if (!Directory.Exists(sourceDir))
{
LogMessage($"Thư mục nguồn không tồn tại: {sourceDir}", true);
return;
}
// Tạo thư mục đích nếu cần
Directory.CreateDirectory(destDir);
// Sao chép tất cả các file trong thư mục hiện tại
foreach (string file in Directory.GetFiles(sourceDir))
{
string destFile = Path.Combine(destDir, Path.GetFileName(file));
File.Copy(file, destFile, true);
LogMessage($"Đã sao chép: {Path.GetFileName(file)}");
}
// Sao chép các thư mục con đệ quy
foreach (string subDir in Directory.GetDirectories(sourceDir))
{
string subDirName = Path.GetFileName(subDir);
LogMessage($"Đang sao chép thư mục: {subDirName}...");
string destSubDir = Path.Combine(destDir, subDirName);
CopyDirectoryInternal(subDir, destSubDir);
}
}
catch (Exception ex)
{
LogMessage($"Lỗi sao chép thư mục: {ex.Message}", true);
MessageBox.Show($"Lỗi sao chép thư mục: {ex.Message}", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void HideDirectory(string parentDir, string dirName)
{
try
{
string fullPath = Path.Combine(parentDir, dirName);
if (Directory.Exists(fullPath))
{
// Ẩn thư mục
DirectoryInfo dirInfo = new DirectoryInfo(fullPath);
if ((dirInfo.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
{
dirInfo.Attributes |= FileAttributes.Hidden;
LogMessage($"Đã ẩn thư mục: {dirName}");
}
}
else
{
// Ẩn file
string fullFilePath = Path.Combine(parentDir, dirName);
if (File.Exists(fullFilePath))
{
FileInfo fileInfo = new FileInfo(fullFilePath);
if ((fileInfo.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
{
fileInfo.Attributes |= FileAttributes.Hidden;
LogMessage($"Đã ẩn tệp tin: {dirName}");
}
}
}
}
catch (Exception ex)
{
// Log ra lỗi nhưng không hiển thị, vì đây không phải là lỗi nghiêm trọng
LogMessage($"Lỗi khi ẩn {dirName}: {ex.Message}", true);
Debug.WriteLine($"Lỗi khi ẩn {dirName}: {ex.Message}");
}
}
private void LogMessage(string message, bool isError = false)
{
if (txtLog.InvokeRequired)
{
txtLog.Invoke(new Action<string, bool>(LogMessage), message, isError);
return;
}
string timestamp = DateTime.Now.ToString("HH:mm:ss");
string formattedMessage = $"[{timestamp}] {message}";
txtLog.AppendText(formattedMessage + Environment.NewLine);
// Tự động cuộn xuống cuối
txtLog.SelectionStart = txtLog.Text.Length;
txtLog.ScrollToCaret();
// Cập nhật màu sắc nếu là lỗi
if (isError)
{
// Lưu vị trí hiện tại
int currentPosition = txtLog.SelectionStart;
// Chọn dòng vừa thêm vào
txtLog.Select(txtLog.Text.Length - formattedMessage.Length - Environment.NewLine.Length, formattedMessage.Length);
// Khôi phục vị trí
txtLog.SelectionStart = currentPosition;
txtLog.SelectionLength = 0;
}
Application.DoEvents(); // Cho phép UI cập nhật
}
private void UpdateProgress(int value)
{
if (progressBar1.InvokeRequired)
{
progressBar1.Invoke(new Action<int>(UpdateProgress), value);
return;
}
progressBar1.Value = value;
Application.DoEvents(); // Cho phép UI cập nhật
}
private async void btnRun_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtPw1.Text))
{
MessageBox.Show("Vui lòng nhập mật khẩu!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Kiểm tra mật khẩu khớp nhau
if (txtPw1.Text != txtPw2.Text)
{
MessageBox.Show("Mật khẩu không khớp. Vui lòng nhập lại!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (cbbUSB.SelectedItem == null)
{
MessageBox.Show("Vui lòng chọn thiết bị USB!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// Vô hiệu hóa nút để tránh click nhiều lần
btnRun.Enabled = false;
btnReload.Enabled = false;
btnReset.Enabled = false;
Cursor = Cursors.WaitCursor;
// Xóa log cũ và reset progress bar
txtLog.Clear();
progressBar1.Value = 0;
try
{
string selectedUSB = cbbUSB.SelectedItem.ToString();
LogMessage($"Đã chọn thiết bị: {selectedUSB}");
int diskNumber = -1;
// Tìm số Disk tương ứng
UpdateProgress(5);
LogMessage("Đang xác định số disk...");
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='" + USB_INTERFACE_TYPE + "'"))
{
foreach (ManagementObject device in searcher.Get())
{
string model = device["Model"]?.ToString() ?? "";
if (model == selectedUSB)
{
string deviceId = device["DeviceID"]?.ToString();
diskNumber = ExtractDiskNumber(deviceId);
LogMessage($"Đã xác định được số disk: {diskNumber}");
break;
}
}
}
if (diskNumber == -1)
{
LogMessage("Không xác định được số disk.", true);
MessageBox.Show("Không xác định được số disk.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// Xác nhận từ người dùng
DialogResult result = MessageBox.Show(
$"Bạn có chắc chắn muốn format và cài đặt lên {selectedUSB} không?\n\nLƯU Ý: Tất cả dữ liệu trên thiết bị sẽ bị xóa!",
"Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result != DialogResult.Yes)
{
LogMessage("Thao tác đã bị hủy bởi người dùng.");
return;
}
LogMessage("Bắt đầu tiến trình format USB...");
UpdateProgress(10);
// Thực thi diskpart với cơ chế retry
bool diskpartSuccess = false;
int retryCount = 0;
const int MAX_RETRIES = 3;
string diskpartErrorMessage = "";
while (!diskpartSuccess && retryCount < MAX_RETRIES)
{
try
{
// Tạo file script diskpart mới cho mỗi lần thử
string scriptPath = Path.GetTempFileName();
File.WriteAllText(scriptPath, $@"
select disk {diskNumber}
clean
create partition primary
format fs=ntfs quick
assign
exit
");
LogMessage($"Đang chạy diskpart lần thứ {retryCount + 1}...");
// Chuẩn bị process diskpart
ProcessStartInfo psi = new ProcessStartInfo("diskpart.exe", $"/s \"{scriptPath}\"")
{
CreateNoWindow = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden
};
using (Process process = Process.Start(psi))
{
await Task.Run(() => process.WaitForExit());
// Kiểm tra kết quả của diskpart
diskpartSuccess = process.ExitCode == 0;
if (diskpartSuccess)
{
LogMessage("Format USB thành công!");
UpdateProgress(20);
}
else
{
LogMessage($"Lỗi khi chạy diskpart (Exit code: {process.ExitCode}), đang thử lại...", true);
await Task.Delay(1000);
}
}
// Xóa file tạm
try { File.Delete(scriptPath); } catch { }
}
catch (Exception ex)
{
diskpartErrorMessage = ex.Message;
LogMessage($"Lỗi khi chạy diskpart: {ex.Message}", true);
}
retryCount++;
// Nếu đã thử đủ số lần mà vẫn thất bại
if (!diskpartSuccess && retryCount >= MAX_RETRIES)
{
string errorMsg = string.IsNullOrEmpty(diskpartErrorMessage)
? "Không thể format USB sau nhiều lần thử"
: $"Lỗi khi chạy diskpart: {diskpartErrorMessage}";
LogMessage(errorMsg, true);
MessageBox.Show(errorMsg, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
// Chờ hệ thống nhận diện USB mới - tăng thời gian chờ
LogMessage("Đang chờ hệ thống nhận diện USB mới...");
await Task.Delay(3000);
UpdateProgress(30);
// Lấy ký tự ổ đĩa của USB sau khi format
List<string> usbDrives = await WaitForUSBDriveLetters();
if (usbDrives.Count == 0)
{
LogMessage("Không tìm thấy phân vùng USB sau khi format.", true);
MessageBox.Show("Không tìm thấy phân vùng USB sau khi format. Vui lòng thử lại hoặc kiểm tra thiết bị.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string usbDrive = usbDrives[0]; // Lấy ổ đĩa đầu tiên
LogMessage($"Đang sử dụng ổ đĩa: {usbDrive}");
// Sao chép file từ thư mục bin vào USB
string binFolder = Path.Combine(Application.StartupPath, "bin");
if (!Directory.Exists(binFolder))
{
LogMessage("Không tìm thấy thư mục bin trong ứng dụng!", true);
MessageBox.Show("Không tìm thấy thư mục bin trong ứng dụng!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
await CopyDirectoryAsync(binFolder, usbDrive);
// Tạo container VeraCrypt
bool containerCreated = await CreateVeraCryptContainer(usbDrive);
if (containerCreated)
{
LogMessage("Đã hoàn tất tất cả các thao tác!");
UpdateProgress(100);
MessageBox.Show($"Đã hoàn tất cài đặt vào USB ({usbDrive})!", "Hoàn thành", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show($"Vui lòng rút USB ra và cắm lại!", "Hoàn thành", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
LogMessage("Cài đặt thành công nhưng không thể tạo container.", true);
UpdateProgress(95);
MessageBox.Show("Cài đặt thành công vui lòng rút/cắm USB ra và thử lại.", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
LogMessage($"Lỗi không xác định: {ex.Message}", true);
MessageBox.Show($"Lỗi không xác định: {ex.Message}", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
// Khôi phục giao diện
btnRun.Enabled = true;
btnReload.Enabled = true;
btnReset.Enabled = true;
Cursor = Cursors.Default;
// Làm mới danh sách USB
LoadUSBDevices();
}
}
private void MainForm_Load(object sender, EventArgs e)
{
this.ActiveControl = btnRun;
}
private async void btnReset_Click(object sender, EventArgs e)
{
if (cbbUSB.SelectedItem == null)
{
MessageBox.Show("Vui lòng chọn thiết bị USB!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
btnReset.Enabled = false;
Cursor = Cursors.WaitCursor;
txtLog.Clear();
progressBar1.Value = 0;
try
{
string selectedUSB = cbbUSB.SelectedItem.ToString();
LogMessage($"Đã chọn thiết bị: {selectedUSB}");
int diskNumber = -1;
// Tìm số Disk tương ứng
UpdateProgress(5);
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='" + USB_INTERFACE_TYPE + "'"))
{
foreach (ManagementObject device in searcher.Get())
{
string model = device["Model"]?.ToString() ?? "";
if (model == selectedUSB)
{
string deviceId = device["DeviceID"]?.ToString();
diskNumber = ExtractDiskNumber(deviceId);
LogMessage($"Đã xác định được số disk: {diskNumber}");
break;
}
}
}
if (diskNumber == -1)
{
LogMessage("Không xác định được số disk.", true);
MessageBox.Show("Không xác định được số disk.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
DialogResult confirm = MessageBox.Show(
$"Bạn có chắc chắn muốn **reset** (xóa và format lại) {selectedUSB} không?\n\nDỮ LIỆU TRÊN USB SẼ BỊ MẤT!",
"Xác nhận Reset", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (confirm != DialogResult.Yes)
{
LogMessage("Thao tác reset đã bị hủy bởi người dùng.");
return;
}
LogMessage("Bắt đầu reset USB...");
UpdateProgress(10);
// Format USB
string scriptPath = Path.GetTempFileName();
File.WriteAllText(scriptPath, $@"
select disk {diskNumber}
clean
create partition primary
format fs=ntfs quick
assign
exit");
ProcessStartInfo psi = new ProcessStartInfo("diskpart.exe", $"/s \"{scriptPath}\"")
{
CreateNoWindow = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden
};
using (Process proc = Process.Start(psi))
{
await Task.Run(() => proc.WaitForExit());
if (proc.ExitCode != 0)
{
LogMessage("Lỗi khi format USB bằng diskpart.", true);
MessageBox.Show("Không thể format USB.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
try { File.Delete(scriptPath); } catch { }
LogMessage("Reset USB thành công.");
UpdateProgress(100);
MessageBox.Show("USB đã được reset và format lại thành công!", "Hoàn tất", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
LogMessage($"Lỗi khi reset USB: {ex.Message}", true);
MessageBox.Show($"Lỗi khi reset USB: {ex.Message}", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
btnReset.Enabled = true;
Cursor = Cursors.Default;
LoadUSBDevices();
}
}
}
}