1313using System . Collections . ObjectModel ;
1414using System . Diagnostics ;
1515using System . IO ;
16+ using System . IO . Compression ;
1617using System . Linq ;
1718using System . Reflection ;
1819using System . Text ;
@@ -374,26 +375,7 @@ public void SettingsExecuted(object sender)
374375 {
375376 case OptionsDialog . OptionsDialogResult . apply :
376377 {
377- StringBuilder args = new StringBuilder ( ) ;
378- if ( Settings . CurrentFilterFiles . Count > 0 )
379- {
380- args . Append ( string . Format ( "/filter: \" {0}\" " , string . Join ( "\" ;\" " , Settings . CurrentFilterFiles ) ) ) ;
381- }
382-
383- if ( Settings . CurrentLogFiles . Count > 0 )
384- {
385- if ( args . Length > 0 )
386- {
387- args . Append ( " " ) ;
388- }
389-
390- args . Append ( string . Format ( "/log: \" {0}\" " , string . Join ( "\" ;\" " , Settings . CurrentLogFiles ) ) ) ;
391- }
392-
393- Settings . Save ( ) ;
394- CreateProcess ( Process . GetCurrentProcess ( ) . MainModule . FileName , args . ToString ( ) ) ;
395- Debug . Print ( args . ToString ( ) ) ;
396- Application . Current . Shutdown ( ) ;
378+ Restart ( ) ;
397379 break ;
398380 }
399381 //case OptionsDialog.OptionsDialogResult.cancel:
@@ -431,6 +413,30 @@ public void SettingsExecuted(object sender)
431413 }
432414 }
433415
416+ private void Restart ( )
417+ {
418+ StringBuilder args = new StringBuilder ( ) ;
419+ if ( Settings . CurrentFilterFiles . Count > 0 )
420+ {
421+ args . Append ( string . Format ( "/filter: \" {0}\" " , string . Join ( "\" ;\" " , Settings . CurrentFilterFiles ) ) ) ;
422+ }
423+
424+ if ( Settings . CurrentLogFiles . Count > 0 )
425+ {
426+ if ( args . Length > 0 )
427+ {
428+ args . Append ( " " ) ;
429+ }
430+
431+ args . Append ( string . Format ( "/log: \" {0}\" " , string . Join ( "\" ;\" " , Settings . CurrentLogFiles ) ) ) ;
432+ }
433+
434+ Settings . Save ( ) ;
435+ CreateProcess ( Process . GetCurrentProcess ( ) . MainModule . FileName , args . ToString ( ) ) ;
436+ Debug . Print ( args . ToString ( ) ) ;
437+ Application . Current . Shutdown ( ) ;
438+ }
439+
434440 public void VersionCheckExecuted ( object sender )
435441 {
436442 VersionCheck ( false ) ;
@@ -484,26 +490,29 @@ private void HandleNewCurrentStatus(object sender, string status)
484490
485491 private void VersionCheck ( bool silent )
486492 {
493+ string destFile = string . Empty ;
494+ string downloadLocation = string . Empty ;
495+ string message = string . Empty ;
496+ string version = string . Empty ;
497+
487498 try
488499 {
489500 if ( string . IsNullOrEmpty ( Settings . VersionCheckFile ) )
490501 {
502+ message = "version check url not specified" ;
503+
491504 if ( silent )
492505 {
493- SetStatus ( "VersionCheckExecuted:version check url not specified" ) ;
506+ SetStatus ( message ) ;
494507 }
495508 else
496509 {
497- MessageBox . Show ( "version check url not specified" ) ;
510+ MessageBox . Show ( message ) ;
498511 }
499-
500512 return ;
501513 }
502-
503- string version = string . Empty ;
514+
504515 string workingVersion = Process . GetCurrentProcess ( ) . MainModule . FileVersionInfo . FileVersion ;
505- string downloadLocation = string . Empty ;
506- string destFile = string . Empty ;
507516 string workingDir = Path . GetDirectoryName ( Process . GetCurrentProcess ( ) . MainModule . FileName ) ;
508517
509518 XmlDocument doc = new XmlDocument ( ) ;
@@ -518,15 +527,7 @@ private void VersionCheck(bool silent)
518527 }
519528 if ( string . IsNullOrEmpty ( downloadLocation ) )
520529 {
521- string message = "unable to check version: " + Settings . VersionCheckFile ;
522- if ( silent )
523- {
524- SetStatus ( "VersionCheck:" + message ) ;
525- }
526- else
527- {
528- MessageBox . Show ( message ) ;
529- }
530+ message = "unable to check version: " + Settings . VersionCheckFile ;
530531 }
531532 else if ( string . Compare ( version , workingVersion , true ) > 0 )
532533 {
@@ -545,19 +546,49 @@ private void VersionCheck(bool silent)
545546 {
546547 string downloadZip = string . Format ( "{0}\\ {1}" , workingDir . TrimEnd ( '\\ ' ) , Path . GetFileName ( downloadLocation ) ) ;
547548 ( new System . Net . WebClient ( ) ) . DownloadFile ( downloadLocation , downloadZip ) ;
548- MessageBox . Show ( string . Format ( "New version has been downloaded from: {0} \n to: {1}.\n Extract zip and restart." , downloadLocation , downloadZip ) ) ;
549- CreateProcess ( "explorer.exe" , workingDir ) ;
549+
550+ string downloadZipDir = string . Format ( "{0}-{1}" , Path . GetFileNameWithoutExtension ( downloadZip ) , version ) ;
551+
552+ if ( Directory . Exists ( downloadZipDir ) )
553+ {
554+ Directory . Delete ( downloadZipDir ) ;
555+ }
556+
557+ // extract zip
558+ ZipFile . ExtractToDirectory ( downloadZip , downloadZipDir ) ;
559+ File . Delete ( downloadZip ) ;
560+
561+ string currentExe = Process . GetCurrentProcess ( ) . MainModule . FileName ;
562+ //string currentConfig = currentExe + ".config";
563+ string currentProcessName = Process . GetCurrentProcess ( ) . MainModule . ModuleName ;
564+ string newExe = string . Format ( "{0}\\ {1}" , downloadZipDir , currentProcessName ) ;
565+ //string newConfig = newExe + ".config";
566+
567+ // overwrite exe
568+ File . Move ( currentExe , currentExe + ".old" ) ;
569+ File . Copy ( newExe , currentExe , true ) ;
570+
571+ // todo: merge configs?
572+ Restart ( ) ;
550573 }
551574 }
552575 }
576+ else if ( string . Compare ( version , workingVersion , true ) < 0 )
577+ {
578+ message = string . Format ( "running version is newer. running ver: {0} update ver: {1}" , workingVersion , version ) ;
579+ }
553580 else
554581 {
555- string message = "version is same: " + workingVersion ;
556- if ( silent )
557- {
558- SetStatus ( "VersionCheck:" + message ) ;
559- }
560- else
582+ message = "version is same: " + workingVersion ;
583+ }
584+
585+ if ( silent )
586+ {
587+ SetStatus ( "VersionCheck:" + message ) ;
588+ }
589+ else
590+ {
591+ if ( ! string . IsNullOrEmpty ( message ) )
561592 {
562593 MessageBox . Show ( message ) ;
563594 }
0 commit comments