11using System ;
22using System . Configuration . Install ;
3+ using System . IO ;
34using System . Reflection ;
45using System . ServiceProcess ;
56using 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