Beispiel #1
0
 //软件升级
 private void SoftwareUpdata_InSetup(Object sender, EventArgs eArgs)
 {
     if (File.Exists("Setup.exe"))
     {
         File.Delete("Setup.exe");
     }
     if (updater == null)
     {
         updater                        = Updater.CreateUpdaterInstance("http://47.102.136.78:8888/Win10APPS/{0}", "update_c.xml");
         updater.Error                 += (s, e) => { Log4netHelper.Error(updater.Context.Exception); _setupForm.ShowToast_InSetup("更新发生了错误:", updater.Context.Exception.Message, 1); };
         updater.UpdatesFound          += (s, e) => { _setupForm.ShowToast_InSetup("发现了新版本:", updater.Context.UpdateInfo.AppVersion, 1); };
         updater.NoUpdatesFound        += (s, e) => { _setupForm.ShowToast_InSetup("提示", "没有新版本!", 1); };
         updater.MinmumVersionRequired += (s, e) => { _setupForm.ShowToast_InSetup("提示", "当前版本过低无法使用自动更新!", 1); };
     }
     Updater.CheckUpdateSimple();
 }
		/// <summary>
		/// 使用指定的信息类来提供应用程序需要的信息
		/// </summary>
		public Updater()
		{
			//ensure update dialog handler;
			var mh = FoundUpdateDialog.Handle;

			Trace.AutoFlush = true;
			Context = new UpdateContext();
			PackagesToUpdate = new List<PackageInfo>();
			UpdatesFound += Instance_UpdatesFound;
			MinmumVersionRequired += Instance_MinmumVersionRequired;
			if (_instance == null) _instance = this;

			//初始化工作参数
			InitializeParameters();
		}
		/// <summary>
		/// 提供一个最简单的自动更新入口
		/// </summary>
		/// <param name="templateUrl">更新模板URL. 如果不传递或传递空的地址, 请使用 <see cref="T:FSLib.App.SimpleUpdater.UpdateableAttribute"/> 属性来标记更新地址</param>
		/// <param name="xmlFileName">更新XML信息文件名</param>
		/// <returns>返回是否开始检查操作</returns>
		public static bool CheckUpdateSimple(string templateUrl, string xmlFileName)
		{
			if (_instance == null)
				_instance = CreateUpdaterInstance(null, null, new UpdateServerInfo[] { new UpdateServerInfo(templateUrl, xmlFileName) });
			else if (!string.IsNullOrEmpty(templateUrl))
			{
				_instance.Context.UpdateDownloadUrl = templateUrl;
				_instance.Context.UpdateInfoFileName = xmlFileName;
			}
			_instance.Context.EnableEmbedDialog = true;

			return Instance.BeginCheckUpdateInProcess();
		}
		/// <summary>
		/// 使用指定的信息类来提供应用程序需要的信息
		/// </summary>
		public Updater()
		{
			Trace.AutoFlush = true;
			Context = new UpdateContext();
			PackagesToUpdate = new List<PackageInfo>();
			UpdatesFound += Instance_UpdatesFound;
			MinmumVersionRequired += Instance_MinmumVersionRequired;
			if (_instance == null) _instance = this;

			//初始化工作参数
			InitializeParameters();
		}
		/// <summary>
		/// 创建自动更新客户端
		/// </summary>
		/// <param name="appVersion">应用程序版本,留空将会使用自动判断</param>
		/// <param name="appDirectory">应用程序目录,留空将会使用自动判断</param>
		/// <param name="servers">升级服务器地址</param>
		/// <param name="switchIfNoUpdate">当没有找到更新时,是否也切换服务器地址。默认为 <see langword="false"/></param>
		/// <returns></returns>
		public static Updater CreateUpdaterInstance(Version appVersion, string appDirectory, bool switchIfNoUpdate, params UpdateServerInfo[] servers)
		{
			CheckInitialized();

			if (servers == null || servers.Length < 2)
			{
				if (appVersion == null && string.IsNullOrEmpty(appDirectory))
					_instance = new Updater();
				else _instance = new Updater(appVersion, appDirectory);

				if (servers.Length > 0)
				{
					_instance.Context.UpdateDownloadUrl = servers[0].Url;
					_instance.Context.UpdateInfoFileName = servers[0].InfoFileName;
				}
			}
			else
			{
				if (appVersion == null && string.IsNullOrEmpty(appDirectory))
					_instance = new MultiServerUpdater(servers) { SwitchIfNoUpdatesFound = switchIfNoUpdate };
				else _instance = new MultiServerUpdater(appVersion, appDirectory, servers) { SwitchIfNoUpdatesFound = switchIfNoUpdate };
			}

			return _instance;
		}