-
Notifications
You must be signed in to change notification settings - Fork 5
/
Program.cs
94 lines (89 loc) · 1.98 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.Reflection;
using System.Text;
namespace WinService
{
class Program
{
private static readonly string _exePath = Assembly.GetExecutingAssembly().Location;
internal static Object globalLock = new Object();
static void Main(string[] args)
{
#if TRACE
Logger.WriteMessage(LogLevel.Trace, "Program.Main");
#endif
WinService service = new WinService();
Boolean consoleRun = Environment.UserInteractive;
if (args.Length > 0)
{
String cmd = args[0].Trim().Trim('-').Trim('/').Trim();
switch (cmd)
{
case "install":
case "i":
SelfInstall();
break;
case "uninstall":
case "u":
SelfUninstall();
break;
case "console":
case "c":
consoleRun = true;
break;
default:
break;
}
}
else
{
if (consoleRun)
{
Logger.WriteMessage(LogLevel.Info, "Starting Console Service");
service.Start(args);
Console.WriteLine("Press any key to exit");
Console.ReadKey();
service.Stop();
Logger.WriteMessage(LogLevel.Info, "Stopped Console Service");
}
else
{
System.ServiceProcess.ServiceBase.Run(service);
}
}
}
static void SelfInstall()
{
#if TRACE
Logger.WriteMessage(LogLevel.Trace, "Program.SelfInstall");
#endif
try
{
ManagedInstallerClass.InstallHelper(new string[] { _exePath });
Logger.WriteMessage(LogLevel.Info, "Service Installed");
}
catch (Exception e)
{
Logger.WriteException(e, "Could not self-install service");
}
}
static void SelfUninstall()
{
#if TRACE
Logger.WriteMessage(LogLevel.Trace, "Program.SelfUninstall");
#endif
try
{
ManagedInstallerClass.InstallHelper(new string[] { "/u", _exePath });
Logger.WriteMessage(LogLevel.Info, "Service Uninstalled");
}
catch (Exception e)
{
Logger.WriteException(e, "Could not self-uninstall service");
}
}
}
}