StartupItem.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Microsoft.Win32;
  2. using System;
  3. using System.IO;
  4. namespace Optimizer
  5. {
  6. internal class StartupItem
  7. {
  8. internal string Name { get; set; }
  9. internal string FileLocation { get; set; }
  10. internal StartupItemLocation RegistryLocation { get; set; }
  11. internal StartupItemType StartupType { get; set; }
  12. internal virtual void Remove() { }
  13. internal virtual void LocateFile() { }
  14. internal virtual void LocateKey() { }
  15. public override string ToString()
  16. {
  17. if (RegistryLocation == StartupItemLocation.Folder) return RegistryLocation.ToString();
  18. return string.Format("{0}:{1}", RegistryLocation, StartupType);
  19. }
  20. }
  21. internal sealed class FolderStartupItem : StartupItem
  22. {
  23. internal string Shortcut { get; set; }
  24. internal override void Remove()
  25. {
  26. try
  27. {
  28. if (File.Exists(Shortcut))
  29. {
  30. File.Delete(Shortcut);
  31. }
  32. }
  33. catch (Exception ex)
  34. {
  35. Logger.LogError("FolderStartupItem.Remove", ex.Message, ex.StackTrace);
  36. }
  37. }
  38. internal override void LocateFile()
  39. {
  40. try
  41. {
  42. Utilities.FindFile(FileLocation);
  43. }
  44. catch (Exception ex)
  45. {
  46. Logger.LogError("FolderStartupItem.LocateFile", ex.Message, ex.StackTrace);
  47. }
  48. }
  49. }
  50. internal sealed 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 (Exception ex)
  60. {
  61. Logger.LogError("RegistryStartupItem.LocateKey", ex.Message, ex.StackTrace);
  62. }
  63. }
  64. internal override void Remove()
  65. {
  66. try
  67. {
  68. Key.DeleteValue(Name, false);
  69. }
  70. catch (Exception ex)
  71. {
  72. Logger.LogError("RegistryStartupItem.Remove", ex.Message, ex.StackTrace);
  73. }
  74. }
  75. internal override void LocateFile()
  76. {
  77. try
  78. {
  79. Utilities.FindFile(SanitizePath(FileLocation));
  80. }
  81. catch (Exception ex)
  82. {
  83. Logger.LogError("RegistryStartupItem.LocateFile", ex.Message, ex.StackTrace);
  84. }
  85. }
  86. internal string SanitizePath(string s)
  87. {
  88. s = s.Replace("\"", string.Empty);
  89. int i;
  90. while (s.Contains("/"))
  91. {
  92. i = s.LastIndexOf("/");
  93. s = s.Substring(0, i);
  94. }
  95. i = s.IndexOf(".exe");
  96. s = s.Substring(0, i + 4);
  97. return s.Trim();
  98. }
  99. }
  100. }