Skip to content

Commit cd2f3a0

Browse files
committed
Installing service always in System32
1 parent 93857c9 commit cd2f3a0

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

Tulpep.NetworkAutoSwitch.Service/Program.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Configuration.Install;
3+
using System.IO;
34
using System.Reflection;
45
using System.ServiceProcess;
56
using System.Threading;
@@ -22,17 +23,21 @@ static int Main(string[] args)
2223
switch (string.Concat(args))
2324
{
2425
case "--install":
25-
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
26-
Console.WriteLine("Service Installed");
26+
27+
string pathOfService = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "NetworkAutoSwitch.exe");
28+
Logging.WriteMessage("Copying file to " + pathOfService);
29+
File.Copy(Assembly.GetExecutingAssembly().Location, pathOfService, true);
30+
ManagedInstallerClass.InstallHelper(new string[] { pathOfService });
31+
Logging.WriteMessage("Service Installed");
2732
const string serviceName = "NetworkAutoSwitch";
2833
int timeout = 5000;
29-
Console.WriteLine(String.Format("Starting Windows Service {0} with timeout of {1} ms", serviceName, timeout));
30-
Console.WriteLine("Service running");
34+
Logging.WriteMessage(String.Format("Starting Windows Service {0} with timeout of {1} ms", serviceName, timeout));
35+
Logging.WriteMessage("Service running");
3136
StartService(serviceName, timeout);
3237
return 0;
3338
case "--uninstall":
3439
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
35-
Console.WriteLine("Service Uninstalled");
40+
Logging.WriteMessage("Service Uninstalled");
3641
return 0;
3742
default:
3843
Console.WriteLine("Use parameters --install or --uninstall to use as Windows Service");
@@ -73,7 +78,6 @@ private static void StartService(string serviceName, int timeoutMilliseconds)
7378
{
7479
ServiceController service = new ServiceController(serviceName);
7580
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
76-
7781
service.Start();
7882
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
7983
}

0 commit comments

Comments
 (0)