⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 appupdater.cs

📁 饭店类的C#程序
💻 CS
📖 第 1 页 / 共 2 页
字号:
				vm.Load(UpdateUrl);
				retValue = vm.IsServerVersionNewer(GetLatestInstalledVersion());
			} 

				//If versioning is not enabled, check the files themselves
			else
			{
				Resource currentResource;
				foreach(Object r in Manifest.Resources.ResourceList) 
				{			
					currentResource = (Resource)(((DictionaryEntry)r).Value);
					string url = UpdateUrl + currentResource.Name;
					string FilePath = currentResource.FilePath;
					if( WebFileLoader.CheckForFileUpdate(url, FilePath))
						retValue =  true;	
				}
			}
			
			//Fire the OnUpdateDetected Event on the UI thread
			if (retValue == true)
			{
				if (OnUpdateDetected != null)
				{
					foreach ( UpdateDetectedEventHandler UC in  OnUpdateDetected.GetInvocationList())
					{
						EventControl.BeginInvoke(UC,new object[] {this,new EventArgs()});
					}
				}
			}

			return retValue;
		}

		//**************************************************************
		// GetLatestInstalledVersion()	
		//**************************************************************
		public static Version GetLatestInstalledVersion()
		{
			//Load the AppStart config File
			string ConfigFilePath =Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
			ConfigFilePath = Path.Combine(Directory.GetParent(ConfigFilePath).FullName,"AppStart.config");
			AppStartConfig Config = AppStartConfig.Load(ConfigFilePath);

			AssemblyName AN = AssemblyName.GetAssemblyName(Config.AppExePath);
			return AN.Version;
		}

		//**************************************************************
		// RestartApp()	
		// - Shuts down the app, returns the AppStart restart return code
		// - May not work for all apps.  Application.Exit() is a soft shutdown
		// - if your app has threads hanging around, the shutdown won't work
		// - Use the ApplicationExit() event
		//**************************************************************
		public void RestartApp()
		{
			Environment.ExitCode = RestartAppReturnValue;
			Application.Exit();
			Poller.Stop();
			Downloader.Stop();
		}

		//**************************************************************
		// OnDownloaderComplete()	
		//**************************************************************
		private void OnDownloaderComplete(object sender, UpdateCompleteEventArgs args)
		{
			if (OnUpdateComplete != null)
			{
				foreach ( UpdateCompleteEventHandler UC in  OnUpdateComplete.GetInvocationList())
				{
					EventControl.BeginInvoke(UC,new object[] {sender, args});
				}
			}

			EventControl.BeginInvoke(new UpdateCompleteEventHandler(UpdateCompleteOps),new object[] {sender, args});
		}

		//**************************************************************
		// UpdateCompleteOps()	
		//**************************************************************
		private void UpdateCompleteOps(object sender, UpdateCompleteEventArgs args)
		{
			if (ShowDefaultUI)
			{
				if (args.UpdateSucceeded)
				{
					UpdateForm F = new UpdateForm();
					
					if (F.ShowDialog()==DialogResult.Yes)
						RestartApp();
				} 
				else
				{
					string ErrorMessage;
					ErrorMessage = "The auto-update of this application failed with the following error message:  \r\n\r\n"
						+ args.ErrorMessage + "\r\n\r\n" 
						+ "To correct this problem, try rebooting the computer & re-launching this application.";
					MessageBox.Show(ErrorMessage,"Application Update Failed",MessageBoxButtons.OK,MessageBoxIcon.Warning);
				}
			}
		}

		//**************************************************************
		// OnAssemblyLoad()	
		// - Generates a manifest of files when doing DirectFile check style
		//   update checks
		//**************************************************************
		private void OnAssemblyLoad(Object sender, AssemblyLoadEventArgs args)
		{
			string[] AssemblyNames = args.LoadedAssembly.Location.Split(new Char[] {'\\'});
			int index = AssemblyNames.Length-1;
			string AssemblyName= AssemblyNames[index];

			if (!Manifest.Resources.ResourceExists(AssemblyName) && IsLocalAssembly(args.LoadedAssembly))
			{
				Resource newResource = new Resource();
				newResource.FilePath = args.LoadedAssembly.Location;
				newResource.Name = AssemblyName;
				newResource.AddedAtRuntime = true;
				Manifest.Resources.AddResource(newResource);
				Manifest.Update();
			}
		}

		//**************************************************************
		// OnAssemblyResolve()	
		// - This code is what does the auto-download of missing files
		//**************************************************************
		private Assembly OnAssemblyResolve(Object sender,ResolveEventArgs args)
		{

			//Check to see if the AssemblyLoad in this event is what caused the
			//event to fire again.  If so, the load failed.
			if (LoadingAssembly==true)
				return null;

			LoadingAssembly=true;
			
			string[] AssemblyNameParts = args.Name.Split(new Char[] {','}, 2);
			string AssemblyName = AssemblyNameParts[0] + ".dll";
			string FilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AssemblyName);

			string url;
			if (ChangeDetectionMode == ChangeDetectionModes.DirectFileCheck)
				url = UpdateUrl + AssemblyName;
			else
			{
				ServerManifest SM = new ServerManifest();
				SM.Load(UpdateUrl);
				url = Path.Combine(SM.ApplicationUrl,AssemblyName);
			}

			Debug.WriteLine("APPMANAGER:  Auto-downloading assembly:  " + AssemblyName + ".  From:  " + url);

			try 
			{
				WebFileLoader.UpdateFile(url, FilePath);
			} 
			catch (Exception e)
			{
				Debug.WriteLine("APPMANAGER:  Failed to download the missing assembly from the web server.");
				Debug.WriteLine("APPMANAGER:  " + e.ToString());
				if (ShowDefaultUI)
					MessageBox.Show("Unable to auto-download the missing parts of the application from:\r\n"
						+ url + "\r\n\r\n" 
						+ "Make sure your connected to the network.  If the problem persists re-install the application.");
				return null;
			}

			Assembly assembly;
			try 
			{
				assembly = Assembly.Load(args.Name);
			} 
			catch (Exception e)
			{
				Debug.WriteLine("APPMANAGER:  Failed to load the auto-downloaded assembly.");
				Debug.WriteLine("APPMANAGER:  " + e.ToString());
				return null; 
			}
			finally
			{
				LoadingAssembly=false;
			}

			return assembly;
		}


		//**************************************************************
		// IsLocalAssembly()	
		//**************************************************************
		private bool IsLocalAssembly(Assembly assembly)
		{
			string AssemblyLocation  = assembly.Location.Replace("/","\\").ToLower(new CultureInfo("en-US"));
			string AppPath = AppDomain.CurrentDomain.BaseDirectory.ToLower(new CultureInfo("en-US"));

			//Only log & replicate assemblies in the default app path
			if ((AssemblyLocation.StartsWith(AppPath.ToLower(new CultureInfo("en-US")))) && 
				(!assembly.FullName.StartsWith("AppManager")))
				return true;
			else 
				return false;
		}

		//**************************************************************
		// OnThreadExit()	
		//**************************************************************
		private void OnApplicationExit(Object sender, EventArgs args)
		{
			//stop the poller thread if it isn't already.
			Poller.Stop();
			Downloader.Stop();
		}

		//**************************************************************
		// EnableFileAutoLoad()	
		//**************************************************************
		private void EnableFileAutoLoad()
		{
			AppDomain App = AppDomain.CurrentDomain;
			App.AssemblyResolve += new ResolveEventHandler(OnAssemblyResolve);
		}

		//**************************************************************
		// DisableFileAutoLoad()	
		//**************************************************************
		private void DisableFileAutoLoad()
		{
			AppDomain App = AppDomain.CurrentDomain;
			App.AssemblyResolve -= new ResolveEventHandler(OnAssemblyResolve);
		}
	
		//**************************************************************
		// EnableManifestGeneration()	
		//**************************************************************
		private void EnableManifestGeneration()
		{	
			//Register for Assembly Load events
			AppDomain App = AppDomain.CurrentDomain;
			App.AssemblyLoad += new AssemblyLoadEventHandler(OnAssemblyLoad);

			//Make sure the currently loaded assemblies are in the manifest
			//The assembly load event will catch any assemblies loaded later
			Assembly[] loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
			for (int i=0; i<loadedAssemblies.Length;i++)
			{
				if (IsLocalAssembly(loadedAssemblies[i]))
				{
					Resource newResource = new Resource();
					string[] AssemblyName = loadedAssemblies[i].Location.Split(new Char[] {'\\'});
					int index = AssemblyName.Length-1;

					newResource.FilePath = loadedAssemblies[i].Location;
					newResource.Name = AssemblyName[index];
					newResource.AddedAtRuntime = true;
					Manifest.Resources.AddResource(newResource);
					Manifest.Update();
				}
			}
		}

		//**************************************************************
		// DisableManifestGeneration()	
		//**************************************************************
		private void DisableManifestGeneration()
		{	
			//Register for Assembly Load events
			AppDomain App = AppDomain.CurrentDomain;
			App.AssemblyLoad -= new AssemblyLoadEventHandler(OnAssemblyLoad);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -