ITsiAdministratorItem.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using BluePointLilac.Methods;
  2. using ContextMenuManager.Methods;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. namespace ContextMenuManager.Controls.Interfaces
  6. {
  7. interface ITsiAdministratorItem
  8. {
  9. ContextMenuStrip ContextMenuStrip { get; set; }
  10. RunAsAdministratorItem TsiAdministrator { get; set; }
  11. ShellLink ShellLink { get; }
  12. }
  13. sealed class RunAsAdministratorItem : ToolStripMenuItem
  14. {
  15. public RunAsAdministratorItem(ITsiAdministratorItem item) : base(AppString.Menu.RunAsAdministrator)
  16. {
  17. item.ContextMenuStrip.Opening += (sender, e) =>
  18. {
  19. if(item.ShellLink == null)
  20. {
  21. this.Enabled = false;
  22. return;
  23. }
  24. string filePath = item.ShellLink.TargetPath;
  25. string extension = Path.GetExtension(filePath)?.ToLower();
  26. switch(extension)
  27. {
  28. case ".exe":
  29. case ".bat":
  30. case ".cmd":
  31. this.Enabled = true;
  32. break;
  33. default:
  34. this.Enabled = false;
  35. break;
  36. }
  37. this.Checked = item.ShellLink.RunAsAdministrator;
  38. };
  39. this.Click += (sender, e) =>
  40. {
  41. item.ShellLink.RunAsAdministrator = !this.Checked;
  42. item.ShellLink.Save();
  43. if(item is WinXItem) ExplorerRestarter.Show();
  44. };
  45. }
  46. }
  47. }