StartupItem.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. public class StartupItem
  13. {
  14. public string Name { get; set; }
  15. public string Location { get; set; }
  16. public StartupItemLocation RegistryLocation { get; set; }
  17. public StartupItemType RegistryType { get; set; }
  18. public virtual void Remove() { }
  19. public virtual void LocateFile() { }
  20. public virtual void LocateKey() { }
  21. public override string ToString()
  22. {
  23. if (RegistryLocation == StartupItemLocation.Folder) return RegistryLocation.ToString();
  24. return string.Format("{0}:{1}", RegistryLocation, RegistryType);
  25. }
  26. }
  27. public class FolderStartupItem : StartupItem
  28. {
  29. public string Shortcut { get; set; }
  30. public override void Remove()
  31. {
  32. try
  33. {
  34. if (File.Exists(Shortcut))
  35. {
  36. File.Delete(Shortcut);
  37. }
  38. }
  39. catch { }
  40. }
  41. public override void LocateFile()
  42. {
  43. try
  44. {
  45. CleanHelper.FindFile(Location);
  46. }
  47. catch { }
  48. }
  49. }
  50. public class RegistryStartupItem : StartupItem
  51. {
  52. public RegistryKey Key { get; set; }
  53. public override void LocateKey()
  54. {
  55. try
  56. {
  57. CleanHelper.FindKeyInRegistry(Key.ToString());
  58. }
  59. catch { }
  60. //finally { Key.Close(); }
  61. }
  62. public override void Remove()
  63. {
  64. try
  65. {
  66. Key.DeleteValue(Name, false);
  67. }
  68. catch { }
  69. finally { Key.Close(); }
  70. }
  71. public override void LocateFile()
  72. {
  73. try
  74. {
  75. CleanHelper.FindFile(SanitizePath(Location));
  76. }
  77. catch { }
  78. }
  79. private string SanitizePath(string s)
  80. {
  81. s = s.Replace("\"", string.Empty);
  82. int i;
  83. while (s.Contains("/"))
  84. {
  85. i = s.LastIndexOf("/");
  86. s = s.Substring(0, i);
  87. }
  88. i = s.IndexOf(".exe");
  89. s = s.Substring(0, i + 4);
  90. return s.Trim();
  91. }
  92. //private int CountOccurences(string data, string occurence)
  93. //{
  94. // return (data.Length - data.Replace(occurence, string.Empty).Length) / occurence.Length;
  95. //}
  96. //private string SanitizePath(string s)
  97. //{
  98. // string n = "";
  99. // Regex r = new Regex("\".*?\"");
  100. // var matches = r.Matches(s);
  101. // if (matches.Count > 0)
  102. // {
  103. // s = matches[0].ToString();
  104. // }
  105. // foreach (char c in s)
  106. // {
  107. // if (c != '"')
  108. // {
  109. // n += c;
  110. // }
  111. // }
  112. // int i = n.LastIndexOf(".exe");
  113. // n = n.Substring(0, i + 4);
  114. // return n;
  115. //}
  116. }
  117. }