@@ -20,21 +20,26 @@ static int Main(string[] args)
2020
2121 if ( Environment . UserInteractive )
2222 {
23+ const string serviceName = "NetworkAutoSwitch" ;
24+ const string exeFileName = "NetworkAutoSwitch.exe" ;
25+ const string installStateFileName = "NetworkAutoSwitch.InstallState" ;
26+
2327 string currentPath = Assembly . GetExecutingAssembly ( ) . Location ;
24- string pathInSystem32 = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . System ) , "NetworkAutoSwitch.exe" ) ;
25- bool runningFromSystem32 = string . Equals ( currentPath , pathInSystem32 , StringComparison . OrdinalIgnoreCase ) ;
28+ string system32Path = Environment . GetFolderPath ( Environment . SpecialFolder . System ) ;
29+ string serviceInSystem32Path = Path . Combine ( system32Path , exeFileName ) ;
30+ bool runningFromSystem32 = string . Equals ( currentPath , serviceInSystem32Path , StringComparison . OrdinalIgnoreCase ) ;
31+ Logging . WriteMessage ( "Starting from " + currentPath ) ;
2632 switch ( string . Concat ( args ) )
2733 {
2834 case "--install" :
2935 if ( runningFromSystem32 ) Logging . WriteMessage ( "The file is located in {0}, you can not delete it from there after the service installation" , currentPath ) ;
3036 else
3137 {
32- Logging . WriteMessage ( "Copying file to " + pathInSystem32 ) ;
33- File . Copy ( currentPath , pathInSystem32 , true ) ;
38+ Logging . WriteMessage ( "Copying file to " + serviceInSystem32Path ) ;
39+ File . Copy ( currentPath , serviceInSystem32Path , true ) ;
3440 }
35- ManagedInstallerClass . InstallHelper ( new string [ ] { "/LogFile=" , "/LogToConsole=true" , pathInSystem32 } ) ;
41+ ManagedInstallerClass . InstallHelper ( new string [ ] { "/LogFile=" , "/LogToConsole=true" , serviceInSystem32Path } ) ;
3642 Logging . WriteMessage ( "Service Installed" ) ;
37- const string serviceName = "NetworkAutoSwitch" ;
3843 int timeout = 5000 ;
3944 Logging . WriteMessage ( "Starting Windows Service {0} with timeout of {1} ms" , serviceName , timeout ) ;
4045 StartService ( serviceName , timeout ) ;
@@ -45,8 +50,18 @@ static int Main(string[] args)
4550 Logging . WriteMessage ( "Service Uninstalled" ) ;
4651 if ( ! runningFromSystem32 )
4752 {
48- Logging . WriteMessage ( "Removing file from " + pathInSystem32 ) ;
49- File . Delete ( pathInSystem32 ) ;
53+ if ( File . Exists ( serviceInSystem32Path ) )
54+ {
55+ Logging . WriteMessage ( "Removing file from " + serviceInSystem32Path ) ;
56+ File . Delete ( serviceInSystem32Path ) ;
57+ }
58+
59+ string installStatePath = Path . Combine ( system32Path , installStateFileName ) ;
60+ if ( File . Exists ( installStatePath ) )
61+ {
62+ Logging . WriteMessage ( "Removing file from " + installStatePath ) ;
63+ File . Delete ( installStatePath ) ;
64+ }
5065 }
5166 return 0 ;
5267 default :
@@ -56,8 +71,6 @@ static int Main(string[] args)
5671 eventArgs . Cancel = true ;
5772 exitEvent . Set ( ) ;
5873 } ;
59-
60- Logging . WriteMessage ( "Starting from " + currentPath ) ;
6174 DetectNetworkChanges detectNetworkChanges = new DetectNetworkChanges ( ) ;
6275 Console . WriteLine ( "Press CTRL + C to exit..." ) ;
6376 exitEvent . WaitOne ( ) ;
0 commit comments