static void Main(string[] args) { bool silent = (args.Length > 0 && string.Compare("-silent", args[0]) == 0) ? true : false; try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); XmlDocument updateServerFile = new XmlDocument(); updateServerFile.Load(AppDomain.CurrentDomain.BaseDirectory + "UpdateConfig.xml"); XmlNodeList serverNodes = updateServerFile.GetElementsByTagName("Update"); string location = null, config = null; if (serverNodes.Count > 0 && serverNodes[0].NodeType == XmlNodeType.Element) { location = (serverNodes[0] as XmlElement).GetAttribute("Location"); config = (serverNodes[0] as XmlElement).GetAttribute("Config"); } if (string.IsNullOrEmpty(location) || string.IsNullOrEmpty(config)) { return; } string updataConfigPath = AppDomain.CurrentDomain.BaseDirectory + "latest.xml"; string adbFileName = AppDomain.CurrentDomain.BaseDirectory + "ADB.exe"; string updateConfigURL = location + "/" + config; WebClient client = new WebClient(); client.DownloadFile(updateConfigURL, updataConfigPath); XmlDocument updataConfig = new XmlDocument(); updataConfig.Load(updataConfigPath); AssemblyName adb = AssemblyName.GetAssemblyName(adbFileName); Version currentVersion = adb.Version; XmlElement ADBNode = updataConfig.GetElementsByTagName("Update")[0] as XmlElement; Version latestVersion = new Version(ADBNode.GetAttribute("Latest")); if (currentVersion < latestVersion) { if (MessageBox.Show(Resources.strTip2, Resources.strTip, MessageBoxButtons.YesNo) == DialogResult.Yes) { UpdateProgress updataProgress = new UpdateProgress( ADBNode.GetAttribute("URL"), AppDomain.CurrentDomain.BaseDirectory + "update.zip" ); if (updataProgress.ShowDialog() == DialogResult.OK) { bool autoUpdate = true; while (!DeleteFile(adbFileName)) { if (MessageBox.Show(Resources.strTip3, Resources.strTip, MessageBoxButtons.OKCancel) == DialogResult.Cancel) { autoUpdate = false; break; } } if (autoUpdate) { CompressUtility.UnZip( AppDomain.CurrentDomain.BaseDirectory + "update.zip", AppDomain.CurrentDomain.BaseDirectory ); DeleteFile(AppDomain.CurrentDomain.BaseDirectory + "update.zip"); string extent = ADBNode.GetAttribute("Extent"); if (!string.IsNullOrEmpty(extent)) { Process extProcess = Process.Start(AppDomain.CurrentDomain.BaseDirectory + extent); extProcess.WaitForExit(); } if (MessageBox.Show(Resources.strTip5, Resources.strTip, MessageBoxButtons.YesNo) == DialogResult.Yes) { Process.Start(adbFileName); } if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Update.txt")) { Process.Start(AppDomain.CurrentDomain.BaseDirectory + "Update.txt"); } } } } } else { if (!silent) { MessageBox.Show(Resources.strTip1, Resources.strTip); } } } catch (Exception ex) { if (!silent) { MessageBox.Show(ex.Message, Resources.strError); } } }
static void Main(string[] args) { try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); XmlDocument setting_conf_doc = new XmlDocument(); setting_conf_doc.Load(AppDomain.CurrentDomain.BaseDirectory + "Setting.conf"); XmlNodeList service_url_node = setting_conf_doc.DocumentElement.GetElementsByTagName("ServiceUrl"); string service_url = null; if (service_url_node.Count > 0 && service_url_node[0].NodeType == XmlNodeType.Element) { service_url = (service_url_node[0] as XmlElement).InnerText; } else { return; } string client_filename = AppDomain.CurrentDomain.BaseDirectory + "Client.exe"; string update_config_localpath = AppDomain.CurrentDomain.BaseDirectory + "latest.xml"; string update_config_url = service_url + "/Update/latest.xml?" + Math.Abs(DateTime.Now.Ticks).ToString(); WebClient client = new WebClient(); client.DownloadFile(update_config_url, update_config_localpath); XmlDocument updata_config = new XmlDocument(); updata_config.Load(update_config_localpath); AssemblyName client_assembly = AssemblyName.GetAssemblyName(client_filename); Version current_version = client_assembly.Version; XmlElement update_node = updata_config.GetElementsByTagName("Update")[0] as XmlElement; Version latestVersion = new Version(update_node.GetAttribute("Latest")); if (current_version < latestVersion) { UpdateProgress updata_progress = new UpdateProgress( update_node.GetAttribute("URL"), AppDomain.CurrentDomain.BaseDirectory + "update.zip" ); if (updata_progress.ShowDialog() == DialogResult.OK) { bool auto_update = true; while (!DeleteFile(client_filename)) { if (MessageBox.Show(Resources.strTip3, Resources.strTip, MessageBoxButtons.OKCancel) == DialogResult.Cancel) { auto_update = false; break; } } if (auto_update) { try { CompressUtility.UnZip( AppDomain.CurrentDomain.BaseDirectory + "update.zip", AppDomain.CurrentDomain.BaseDirectory ); Process.Start(client_filename); } catch { MessageBox.Show("更新应用程序失败,请您手动解压安装目录下的update.zip文件更新应用程序!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand); } } } } else { MessageBox.Show(Resources.strTip1, Resources.strTip); } } catch (Exception ex) { MessageBox.Show(ex.Message, Resources.strError); } }