using System; using System.Collections.Generic; using System.Windows.Forms; using System.IO; using System.Net; using System.Diagnostics; using System.Threading; using System.Xml; using System.Web.Hosting; using System.Deployment.Application; using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Collections; using Newtonsoft.Json; using System.Configuration; using MAutoUpdate.common; using MAutoUpdate.Models; namespace MAutoUpdate { static class Program { static bool f; static Process pCurrent = Process.GetCurrentProcess(); static Mutex m = new Mutex(true, pCurrent.MainModule.FileName.Replace(":", "").Replace(@"\", "") + "MAutoUpdate", out f);//互斥, static string ip = ConfigurationManager.AppSettings["IP"].ToString(); /// /// 程序主入口 /// /// [0]程序名称,[1]静默更新 0:否 1:是 [3] 0:程序启动检测 1:手动点击检查更新按钮(在于忽略更新的情况下,手动检测时候还是要弹出来的) [STAThread] static void Main(string[] args) { if (f) { try { if (ApplicationDeployment.IsNetworkDeployed) { LogTool.AddLog("当前版本" + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString()); } CheckVersion(); #region //string programName = "EapForIdle";//args[0]; //string silentUpdate = "1";// args[1]; //string isClickUpdate = "0"; //// string localAddress = HostingEnvironment.ApplicationPhysicalPath; //string localAddress = AppDomain.CurrentDomain.BaseDirectory; //if (string.IsNullOrEmpty(programName) == false) //{ // UpdateWork updateWork = null; // if (GetPidByProcessName(programName) == 0 && !startprocess()) // updateWork = new UpdateWork(programName, localAddress, isClickUpdate, 1); // else // { // updateWork = new UpdateWork(programName, localAddress, isClickUpdate, 2); // } // 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)); // } // } // } // } //} #endregion } catch (Exception ex) { MessageBox.Show(ex.Message); } } } public static int GetPidByProcessName(string processName) { Process[] arrayProcess = Process.GetProcessesByName(processName); foreach (Process p in arrayProcess) { return p.Id; } return 0; } public static bool startprocess() { try { Process.Start(Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"Debug\", "EapForIdle.exe")); return true; } catch { return false; } } public static void CheckVersion() { try { string programName = "EapForIdle"; string silentUpdate = "1"; string isClickUpdate = "0"; startprocess(); string localAddress = AppDomain.CurrentDomain.BaseDirectory + @"Debug"; if (!Directory.Exists(localAddress)) { Directory.CreateDirectory(localAddress); } var files = Directory.GetFiles(localAddress, "*.*"); Hashtable hashtable = new Hashtable(); DirectoryInfo folder = new DirectoryInfo(localAddress); foreach (FileInfo file in folder.GetFiles("*.*")) { hashtable[file.Name] = GetMD5HashFromFile(file.FullName); } CompareVersion compareVersion = new CompareVersion(); compareVersion.Versions = hashtable; string Json = JsonConvert.SerializeObject(compareVersion); string ss = ip + "CompareVersion/CheckVersionName"; string result = UntilApi.HttpPost(ss, Json); EapResponse response = JsonConvert.DeserializeObject(result); if (response != null) { if (response.Code == 1) { var ht = JsonConvert.DeserializeObject(response.Data.ToString()); if (ht.Count > 0) { UpdateWork updateWork = new UpdateWork(programName, localAddress, isClickUpdate, 1, ht); updateWork.Do(); } } } } catch (Exception ex) { LogTool.AddLog(ex.ToString()+ex.StackTrace); } } public static string GetMD5HashFromFile(string fileName) { try { FileStream file = new FileStream(fileName, System.IO.FileMode.Open, FileAccess.Read); MD5 md5 = new MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(file); file.Close(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString(); } catch (Exception ex) { throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message); } } static void CheckForUpdate() { try { if (ApplicationDeployment.IsNetworkDeployed) { ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment; if (ad.CheckForUpdate()) { LogTool.AddLog("更新版本" + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString()); //Application.ExitThread(); //Restart(); ad.UpdateAsync(); ad.UpdateCompleted += (s, ee) => { if (ee.Error == null) { LogTool.AddLog("更新版本成功" + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString()); //var vserson= ApplicationDeployment.CurrentDeployment.CurrentVersion; //var vserson = Assembly.GetExecutingAssembly().GetName().Version; //ForUpdate FU = new ForUpdate(this); //FU.ShowDialog(); //FU.Focus(); } else { LogTool.AddLog("更新版本失败" + ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString()); MessageBox.Show(ee.Error.ToString()); } }; } else { LogTool.AddLog("不需要更新版本"); } } } catch (Exception ex) { LogTool.AddLog(ex.Message); } } } }