/// <summary> /// 下载服务器上版本信息 /// </summary> /// <param name="serverUrl"></param> /// <returns></returns> private RemoteVersionInfo DownloadUpdateInfo(string serverUrl) { string updateJson = ""; using (WebClient updateClt = new WebClient()) { try { byte[] bJson = updateClt.DownloadData(serverUrl); updateJson = System.Text.Encoding.UTF8.GetString(bJson); } catch (Exception ex) { LogManger.Instance.Error("下载服务器上版本信息错误", ex); HHMessageBox.Show(string.Format("升级信息从 {0} 下载失败:{1}", serverUrl, ex.Message), "错误"); return(null); } try { RemoteVersionInfo info = new RemoteVersionInfo(); info = JsonConvert.DeserializeObject <RemoteVersionInfo>(updateJson); return(info); } catch (Exception ex) { LogManger.Instance.Error("升级 json 文件错误", ex); HHMessageBox.Show(string.Format("升级 json 文件错误:{0}\r\n{0}", ex.Message, updateJson), "错误"); return(null); } } }
private void btnOk_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtLaunchAppName.Text)) { HHMessageBox.Show("请填写正确的应用程序名称", "提示"); return; } if (string.IsNullOrEmpty(txtServerUpdateUrl.Text)) { HHMessageBox.Show("请填写正确的升级信息路径", "提示"); return; } Program.LaunchAppName = txtLaunchAppName.Text; Program.ServerUpdateUrl = txtServerUpdateUrl.Text; this.DialogResult = DialogResult.OK; return; }
private void MainForm_Load(object sender, EventArgs e) { //通过业务应用程序名,获取其进程信息 launchProcess = Process.GetProcessesByName(launchAppName); if (launchProcess.Length > 0) { launchAppDirectoryName = Path.GetDirectoryName(launchProcess[0].MainModule.FileName); launchAppVer = launchProcess[0].MainModule.FileVersionInfo.ProductVersion; } else { HHMessageBox.Show("应用程序未启动: _" + launchAppName); Application.Exit(); } //下载服务器上版本更新信息 verInfo = DownloadUpdateInfo(Program.ServerUpdateUrl); if (verInfo != null) { //比较版本号 if (VersionCompare(launchAppVer, verInfo.ReleaseVersion) >= 0) { //this.Hide();//隐藏当前窗口 if (checkMode == "1") { HHMessageBox.Show("当前版本已经是最新版本"); } Application.Exit(); } else { this.lblContent.Text = verInfo.VersionDesc; } } else { Application.Exit(); } }
static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //C# Mutex:(互斥锁)线程同步 //避免程序重复运行 System.Threading.Mutex mutex = new System.Threading.Mutex(true, "HHUpdateApp_OnlyRunOneInstance", out bool isRuned); //第一个参数:true--给调用线程赋予互斥体的初始所属权 //第一个参数:TQ_WinClient_OnlyRunOneInstance--互斥体的名称 //第三个参数:返回值isRuned,如果调用线程已被授予互斥体的初始所属权,则返回true if (isRuned) { try { if (args.Length == 0) { SettingForm set = new SettingForm(); set.ShowDialog(); } else { //拉起更新请求的业务程序,稍后更新时,根据这个值关闭对应的进程 LaunchAppName = args[0]; //检查更新模式:0,自动更新;1,手动检查(区别就是,自动更新的状态下,如果有新版本更新,才会显示提示框) CheckMode = args[1]; } /* * 当前用户是管理员的时候,直接启动应用程序 * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 */ //获得当前登录的Windows用户标示 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); //判断当前登录用户是否为管理员 if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) { Application.Run(new MainForm(LaunchAppName, CheckMode)); } else { string result = Environment.GetEnvironmentVariable("systemdrive"); if (AppDomain.CurrentDomain.BaseDirectory.Contains(result)) { //创建启动对象 ProcessStartInfo startInfo = new ProcessStartInfo { //设置运行文件 FileName = System.Windows.Forms.Application.ExecutablePath, //设置启动动作,确保以管理员身份运行 Verb = "runas", Arguments = " " + LaunchAppName }; //如果不是管理员,则启动UAC System.Diagnostics.Process.Start(startInfo); } else { Application.Run(new MainForm(LaunchAppName, CheckMode)); } } } catch (Exception ex) { HHMessageBox.Show(ex.Message, "错误"); } } }
public static DialogResult Show(string text, string caption) { HHMessageBox msgbox = new HHMessageBox(text, caption); return(msgbox.ShowDialog()); }