static void Main(string[] args) { //Retrive cmdline to pass to new process CommandLineString = ""; for (int i = 0; i < args.Length; i++) { CommandLineString = string.Format("{0} {1}", CommandLineString, args[i]); } CommandLineString += " /appstartversion " + Assembly.GetExecutingAssembly().GetName().Version.ToString(); RestartCommandLineString = CommandLineString + " /restart "; CommandLineArgs = new String[args.Length + 2]; CommandLineArgs[CommandLineArgs.Length - 2] = "/appstartversion"; CommandLineArgs[CommandLineArgs.Length - 1] = Assembly.GetExecutingAssembly().GetName().Version.ToString(); RestartCommandLineArgs = new String[CommandLineArgs.Length + 1]; RestartCommandLineArgs[RestartCommandLineArgs.Length - 1] = "/restart"; AppStartConfig Config = LoadConfig(); if (Config.AppLaunchMode == AppStartConfig.LaunchModes.Process) { StartAndWait_Process(); } else { StartAndWait_Domain(); } }
/********************************************************************* * LoadConfig() **********************************************************************/ private static AppStartConfig LoadConfig() { AppStartConfig Config; //Load the config file which knows where the app lives try { //Try app specific config file name Config = AppStartConfig.Load(CalcConfigFileLocation()); return(Config); } catch (Exception e) { try { //Try default config file name Debug.WriteLine("APPLICATION STARTER: Falling back to try to read appstart.config."); Config = AppStartConfig.Load(AppDomain.CurrentDomain.BaseDirectory + @"AppStart.Config"); return(Config); } catch { HandleTerminalError(e); } } return(null); }
//************************************************************** // Load() //************************************************************** /// <summary> /// Gets an AppStart object deserialized from the file. /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static AppStartConfig Load(string filePath) { AppStartConfig temp = null; try { XmlSerializer xs = new XmlSerializer(typeof(AppStartConfig)); FileStream fs = new FileStream(filePath, FileMode.Open); temp = (AppStartConfig)xs.Deserialize(fs); fs.Close(); } catch (Exception e) { Debug.WriteLine("Error deserializing Appstart"); throw new ConfigFileMissingException("Error loading config file :" + filePath, e); } return(temp); }
//************************************************************** // RunThread() //************************************************************** public void RunThread() { Debug.WriteLine("APPMANAGER: Starting Update"); //Load the AppStart config File string ConfigFilePath =Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); ConfigFilePath = Path.Combine(Directory.GetParent(ConfigFilePath).FullName,"AppStart.config"); Config = AppStartConfig.Load(ConfigFilePath); try { //Mark in the manifest that a download is in progress. Persisted in //manifest in case the app is stop & restarted during the download. if (AppMan.Manifest.State.Phase == UpdatePhases.Complete) { AppMan.Manifest.State.Phase = UpdatePhases.Scavenging; AppMan.Manifest.State.UpdateFailureCount=0; AppMan.Manifest.State.UpdateFailureEncoutered = false; AppMan.Manifest.State.DownloadDestination = CreateTempDirectory(); if (AppMan.ChangeDetectionMode == ChangeDetectionModes.ServerManifestCheck) { ServerManifest SM = new ServerManifest(); SM.Load(AppMan.UpdateUrl); //download from the web and desserialise it into the object AppMan.Manifest.State.DownloadSource = SM.ApplicationUrl; //new: krishna added this to enable specifying files explicitly instead of deep copying //a directory using HTTP-DAV. AppMan.Manifest.State.FilesToDownload = SM.FilesToDownload; } else { AppMan.Manifest.State.DownloadSource = AppMan.UpdateUrl; } AppMan.Manifest.Update(); } //Scavenge Existing Directories if (AppMan.Manifest.State.Phase == UpdatePhases.Scavenging) { Debug.WriteLine("APPMANAGER: Scavaging older versions"); Scavenge(); AppMan.Manifest.State.Phase=UpdatePhases.Downloading; AppMan.Manifest.Update(); } //Download the New Version if (AppMan.Manifest.State.Phase == UpdatePhases.Downloading) { Debug.WriteLine("APPMANAGER: Downloading new version"); Download(); AppMan.Manifest.State.Phase = UpdatePhases.Validating; AppMan.Manifest.Update(); } //Validate the downloaded bits if (AppMan.Manifest.State.Phase == UpdatePhases.Validating) { Debug.WriteLine("APPMANAGER: Downloading new version"); Validate(); AppMan.Manifest.State.Phase = UpdatePhases.Merging; AppMan.Manifest.Update(); } //Merge the existing version into the new version if (AppMan.Manifest.State.Phase == UpdatePhases.Merging) { Debug.WriteLine("APPMANAGER: Merging current & new versions"); MergeDirectory(AppDomain.CurrentDomain.BaseDirectory,AppMan.Manifest.State.DownloadDestination); AppMan.Manifest.State.Phase = UpdatePhases.Finalizing; AppMan.Manifest.Update(); } //Finalize the update. Rename the new version directory, etc... if (AppMan.Manifest.State.Phase == UpdatePhases.Finalizing) { Debug.WriteLine("APPMANAGER: Finalizing update"); FinalizeUpdate(); } //Reset Update State AppMan.Manifest.State.Phase = UpdatePhases.Complete; AppMan.Manifest.State.UpdateFailureCount=0; AppMan.Manifest.State.UpdateFailureEncoutered = false; AppMan.Manifest.State.DownloadSource = ""; AppMan.Manifest.State.DownloadDestination = ""; AppMan.Manifest.State.NewVersionDirectory = ""; AppMan.Manifest.Update(); } catch (ThreadAbortException) { Thread.ResetAbort(); Debug.WriteLine("APPMANAGER: ThreadAborted: Updater Thread stopped, stopping download."); return; } catch (Exception e) { UpdateEventArgs.FailureException = e; } if (AppMan.Manifest.State.Phase != UpdatePhases.Complete) HandleUpdateFailure(); else HandleUpdateSuccess(); }