StartupItem.cs 2.2 KB

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