public UpdateForm(UpdateWork _work) { work = _work; InitializeComponent(); UpdateUIDelegate = new UpdateUI((obj) => { this.updateBar.Value = obj; }); work.OnUpdateProgess += new UpdateWork.UpdateProgess((obj) => { this.Invoke(UpdateUIDelegate, (int)obj); }); }
public MainForm(UpdateWork _updateWork) { InitializeComponent(); updateWork = _updateWork; var res = _updateWork.UpdateVerList[_updateWork.UpdateVerList.Count - 1].VersionDesc; /* * var temp = WebRequest.Create(res); * var stream = temp.GetResponse().GetResponseStream(); * using (StreamReader reader = new StreamReader(stream, System.Text.Encoding.Default)) * { * string text = reader.ReadToEnd(); * this.lblContent.Text = text; * } */ foreach (var item in _updateWork.UpdateVerList[_updateWork.UpdateVerList.Count - 1].VersionDesc.Split('$')) { this.lblContent.Text = this.lblContent.Text + item + Environment.NewLine; } }
static void Main(string[] args) { if (f) { try { string isClickUpdate = "0"; string localAddress = AppDomain.CurrentDomain.BaseDirectory; if (args.Length == 3) { isClickUpdate = args[2]; } UpdateWork updateWork = new UpdateWork(localAddress, isClickUpdate); string programName = updateWork.localInfo.AppName; string silentUpdate = updateWork.localInfo.SilentUpdate; if (updateWork.UpdateVerList.Count != 0) { /* 当前用户是管理员的时候,直接启动应用程序 * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 */ //获得当前登录的Windows用户标示 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); //创建Windows用户主题 Application.EnableVisualStyles(); System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); //判断当前登录用户是否为管理员 if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) { if (silentUpdate == "1") { updateWork.Do(); } else { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm(updateWork)); } } 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 = " " + programName + " " + silentUpdate }; //如果不是管理员,则启动UAC System.Diagnostics.Process.Start(startInfo); } else { if (silentUpdate == "1") { updateWork.Do(); } else { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm(updateWork)); } } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }