-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
91 lines (78 loc) · 3.33 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
using System;
using System.ServiceProcess;
namespace ASTAWebClient
{
static class Program
{
/// <summary>
/// The main entry point for the application
/// </summary>
/// <param name="args"> Parameters for install: AstaWebClient.exe -i, uninstall: AstaWebClient.exe -u </param>
static void Main(string[] args)
{
//load libraries from this assembly
AssemblyLoader.RegisterAssemblyLoader();
IServiceManageable serviceManagable = new ServiceManager();
WindowsServiceClass uninstallService = new WindowsServiceClass();
uninstallService.EvntInfoMessage += (x, e) => serviceManagable.AddInfo(e.Message);
//class Name matched installing services (public partial class from Program.cs)
AstaWebClient service = new AstaWebClient(serviceManagable);
if (args?.Length > 0)
{
foreach (var str in args)
serviceManagable.AddInfo($"Got environment argument '{str}'");
}
ServiceBase[] ServicesToRun = new ServiceBase[]
{
service
};
string serviceName = ServiceInstallerUtility.serviceName;
if (Environment.UserInteractive)
{
// Разбор пути для саморегистрации
if (args?.Length > 0 && args[0].Length > 1
&& (args[0].StartsWith("-") || args[0].StartsWith("/")))
{
switch (args[0].Substring(1).ToLower())
{
case "install":
case "i":
if (!ServiceInstallerUtility.Install())
{
serviceManagable.AddInfo("Failed to install service");
}
else
{
serviceManagable.OnStart();
serviceManagable.AddInfo("Running service");
}
break;
case "uninstall":
case "u":
ServiceInstallerUtility.StopService();
uninstallService.Uninstall(serviceName);
if (!ServiceInstallerUtility.Uninstall())
{
serviceManagable.AddInfo("Failed to uninstall service");
}
else
{
serviceManagable.AddInfo("Service stopped. Goodbye.");
}
string processName = System.IO.Path.GetFileName(ServiceInstallerUtility.serviceExePath);
System.Diagnostics.Process.Start("taskkill", $"/F /IM {processName}");
break;
default:
serviceManagable.OnStart();
// ServiceInstallerUtility.Install();
break;
}
}
}
else
{
ServiceBase.Run(ServicesToRun);
}
}
}
}