StartupItem.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Win32;
  7. using System.IO;
  8. using System.Text.RegularExpressions;
  9. using System.Windows.Forms;
  10. namespace Optimizer
  11. {
  12. internal class StartupItem
  13. {
  14. internal string Name { get; set; }
  15. internal string FileLocation { get; set; }
  16. internal StartupItemLocation RegistryLocation { get; set; }
  17. internal StartupItemType StartupType { get; set; }
  18. internal virtual void Remove() { }
  19. internal virtual void LocateFile() { }
  20. internal virtual void LocateKey() { }
  21. public override string ToString()
  22. {
  23. if (RegistryLocation == StartupItemLocation.Folder) return RegistryLocation.ToString();
  24. return string.Format("{0}:{1}", RegistryLocation, StartupType);
  25. }
  26. }
  27. internal class FolderStartupItem : StartupItem
  28. {
  29. internal string Shortcut { get; set; }
  30. internal override void Remove()
  31. {
  32. try
  33. {
  34. if (File.Exists(Shortcut))
  35. {
  36. File.Delete(Shortcut);
  37. }
  38. }
  39. catch { }
  40. }
  41. internal override void LocateFile()
  42. {
  43. try
  44. {
  45. Utilities.FindFile(FileLocation);
  46. }
  47. catch { }
  48. }
  49. }
  50. internal class RegistryStartupItem : StartupItem
  51. {
  52. internal RegistryKey Key { get; set; }
  53. internal override void LocateKey()
  54. {
  55. try
  56. {
  57. Utilities.FindKeyInRegistry(Key.ToString());
  58. }
  59. catch { }
  60. }
  61. internal override void Remove()
  62. {
  63. try
  64. {
  65. Key.DeleteValue(Name, false);
  66. }
  67. catch { }
  68. }
  69. internal override void LocateFile()
  70. {
  71. try
  72. {
  73. Utilities.FindFile(SanitizePath(FileLocation));
  74. }
  75. catch { }
  76. }
  77. internal string SanitizePath(string s)
  78. {
  79. s = s.Replace("\"", string.Empty);
  80. int i;
  81. while (s.Contains("/"))
  82. {
  83. i = s.LastIndexOf("/");
  84. s = s.Substring(0, i);
  85. }
  86. i = s.IndexOf(".exe");
  87. s = s.Substring(0, i + 4);
  88. return s.Trim();
  89. }
  90. }
  91. }