FileUnlockForm.cs 1.9 KB

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