-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
116 lines (102 loc) · 3.98 KB
/
Program.cs
File metadata and controls
116 lines (102 loc) · 3.98 KB
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
115
116
using System;
using System.ServiceProcess;
using System.Threading;
using System.Diagnostics;
using System.Reflection;
namespace service_core
{
static class ServiceInfo
{
static public string SVC_APP_NAME = "MyService_core";
static public string SVC_EXECUTABLE_PATH = Assembly.GetExecutingAssembly().Location.Remove(Assembly.GetExecutingAssembly().Location.Length - 4) + ".exe";
}
class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
var cmd = args[0];
if(cmd.Equals("-u"))
{
var processStop = new Process();
var startInfoStop = new ProcessStartInfo();
startInfoStop.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfoStop.FileName = "sc.exe";
Console.WriteLine("Stopping Service");
startInfoStop.Arguments = "stop " + ServiceInfo.SVC_APP_NAME;
processStop.StartInfo = startInfoStop;
processStop.Start();
processStop.WaitForExit();
Console.WriteLine("Service uninstall");
var process = new Process();
var startInfo = new ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "sc.exe";
startInfo.Arguments = "delete " + ServiceInfo.SVC_APP_NAME;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
else if (cmd.Equals("-i"))
{
var process = new Process();
var startInfo = new ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "sc.exe";
startInfo.Arguments = string.Format("create {0} displayname=\"{1}\" binpath=\"{2}\"",ServiceInfo.SVC_APP_NAME, ServiceInfo.SVC_APP_NAME, ServiceInfo.SVC_EXECUTABLE_PATH);
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
}
else if (cmd.Equals("-h"))
{
Console.WriteLine(string.Format("{0}",ServiceInfo.SVC_APP_NAME));
Console.WriteLine("Options:");
Console.WriteLine(" -i\tInstall Service");
Console.WriteLine(" -u\tUninstall Service");
Console.WriteLine(" -h\tShow command line switch help");
}
else
{
Console.WriteLine(string.Format("{0}",ServiceInfo.SVC_APP_NAME));
Console.WriteLine("Options:");
Console.WriteLine(" -i\tInstall Service");
Console.WriteLine(" -u\tUninstall Service");
Console.WriteLine(" -h\tShow command line switch help");
}
}
else
{
ServiceBase.Run(new MyService());
}
}
}
class MyService : ServiceBase
{
public MyService ()
{
this.AutoLog = false;
this.CanHandlePowerEvent = true;
this.CanPauseAndContinue = false;
this.CanShutdown = true;
this.ServiceName = "MyService";
}
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
return true;
}
protected override void OnStart(string[] args)
{
// add your code
}
protected override void OnStop()
{
// add your code
}
protected override void OnShutdown()
{
// add your code
}
}
}