FileUnlockForm.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. {
  10. public sealed partial class FileUnlockForm : Form
  11. {
  12. List<Process> _lockingProcesses;
  13. public FileUnlockForm()
  14. {
  15. InitializeComponent();
  16. CheckForIllegalCrossThreadCalls = false;
  17. OptionsHelper.ApplyTheme(this);
  18. radioFile.Text = OptionsHelper.TranslationList["radioFile"].ToString();
  19. btnKill.Text = OptionsHelper.TranslationList["btnKill"].ToString();
  20. }
  21. private void FileUnlockForm_Load(object sender, EventArgs e)
  22. {
  23. }
  24. private void btnFind_Click(object sender, EventArgs e)
  25. {
  26. if (string.IsNullOrEmpty(txtFile.Text)) return;
  27. if (!File.Exists(txtFile.Text)) return;
  28. _lockingProcesses = FileHandleHelper.GetProcessesLockingFile(txtFile.Text);
  29. if (_lockingProcesses == null) return;
  30. listProcesses.Items.Clear();
  31. listProcesses.Items.AddRange(_lockingProcesses.Select(x => $"[{x.Id}] {x.ProcessName}").ToArray());
  32. }
  33. private void btnKill_Click(object sender, EventArgs e)
  34. {
  35. if (listProcesses.CheckedItems.Count <= 0) return;
  36. foreach (string x in listProcesses.CheckedItems)
  37. {
  38. IEnumerable<Process> prs = Process.GetProcesses().Where(pr => pr.ProcessName == x.Replace(x.Substring(0, x.IndexOf("]") + 1), string.Empty).Trim());
  39. foreach (Process z in prs)
  40. {
  41. try
  42. {
  43. z.Kill();
  44. }
  45. catch { continue; }
  46. }
  47. }
  48. btnFind.PerformClick();
  49. }
  50. }
  51. }