FileUnlockForm.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Windows.Forms;
  8. namespace Optimizer {
  9. public sealed partial class FileUnlockForm : Form {
  10. List<Process> _lockingProcesses;
  11. public FileUnlockForm() {
  12. InitializeComponent();
  13. CheckForIllegalCrossThreadCalls = false;
  14. OptionsHelper.ApplyTheme(this);
  15. radioFile.Text = OptionsHelper.TranslationList["radioFile"].ToString();
  16. btnKill.Text = OptionsHelper.TranslationList["btnKill"].ToString();
  17. }
  18. private void FileUnlockForm_Load(object sender, EventArgs e) {
  19. }
  20. private void btnFind_Click(object sender, EventArgs e) {
  21. if (string.IsNullOrEmpty(txtFile.Text)) return;
  22. if (!File.Exists(txtFile.Text)) return;
  23. _lockingProcesses = FileHandleHelper.GetProcessesLockingFile(txtFile.Text);
  24. if (_lockingProcesses == null) return;
  25. listProcesses.Items.Clear();
  26. listProcesses.Items.AddRange(_lockingProcesses.Select(x => $"[{x.Id}] {x.ProcessName}").ToArray());
  27. }
  28. private void btnKill_Click(object sender, EventArgs e) {
  29. if (listProcesses.CheckedItems.Count <= 0) return;
  30. foreach (string x in listProcesses.CheckedItems) {
  31. IEnumerable<Process> prs = Process.GetProcesses().Where(pr => pr.ProcessName == x.Replace(x.Substring(0, x.IndexOf("]") + 1), string.Empty).Trim());
  32. foreach (Process z in prs) {
  33. try {
  34. z.Kill();
  35. }
  36. catch { continue; }
  37. }
  38. }
  39. btnFind.PerformClick();
  40. }
  41. }
  42. }