forked from hellzerg/optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StartupItem.cs
114 lines (100 loc) · 2.94 KB
/
StartupItem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Microsoft.Win32;
using System;
using System.IO;
namespace Optimizer
{
internal class StartupItem
{
internal string Name { get; set; }
internal string FileLocation { get; set; }
internal StartupItemLocation RegistryLocation { get; set; }
internal StartupItemType StartupType { get; set; }
internal virtual void Remove() { }
internal virtual void LocateFile() { }
internal virtual void LocateKey() { }
public override string ToString()
{
if (RegistryLocation == StartupItemLocation.Folder) return RegistryLocation.ToString();
return string.Format("{0}:{1}", RegistryLocation, StartupType);
}
}
internal sealed class FolderStartupItem : StartupItem
{
internal string Shortcut { get; set; }
internal override void Remove()
{
try
{
if (File.Exists(Shortcut))
{
File.Delete(Shortcut);
}
}
catch (Exception ex)
{
ErrorLogger.LogError("FolderStartupItem.Remove", ex.Message, ex.StackTrace);
}
}
internal override void LocateFile()
{
try
{
Utilities.FindFile(FileLocation);
}
catch (Exception ex)
{
ErrorLogger.LogError("FolderStartupItem.LocateFile", ex.Message, ex.StackTrace);
}
}
}
internal sealed class RegistryStartupItem : StartupItem
{
internal RegistryKey Key { get; set; }
internal override void LocateKey()
{
try
{
Utilities.FindKeyInRegistry(Key.ToString());
}
catch (Exception ex)
{
ErrorLogger.LogError("RegistryStartupItem.LocateKey", ex.Message, ex.StackTrace);
}
}
internal override void Remove()
{
try
{
Key.DeleteValue(Name, false);
}
catch (Exception ex)
{
ErrorLogger.LogError("RegistryStartupItem.Remove", ex.Message, ex.StackTrace);
}
}
internal override void LocateFile()
{
try
{
Utilities.FindFile(SanitizePath(FileLocation));
}
catch (Exception ex)
{
ErrorLogger.LogError("RegistryStartupItem.LocateFile", ex.Message, ex.StackTrace);
}
}
internal string SanitizePath(string s)
{
s = s.Replace("\"", string.Empty);
int i;
while (s.Contains("/"))
{
i = s.LastIndexOf("/");
s = s.Substring(0, i);
}
i = s.IndexOf(".exe");
s = s.Substring(0, i + 4);
return s.Trim();
}
}
}