在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
更新思想:根据url下载安装包,再在本地解压,将图标在桌面显示。自动更新的时候使用后台线程(当程序退出的时候,线程也退出)。 private void lbVersion_Click(object sender, RoutedEventArgs e)//设置一个按钮来确定是否自动更新 { AutoUpdateManager.AutoUpdate(); } public static void AutoUpdate()//将程序放入线程池中(线程池线程默认为后台线程, 应用程序的主线程以及使用Thread构造的线程都默认为前台线程,线程池线程也就是使用 ThreadPool.QueueUserWorkItem()和Task工厂创建的线程都默认为后台线程) private static void CheckUpdateAction(object state) { if (state != null)//这里没啥用,防止以后加功能 { Thread.CurrentThread.Priority = (ThreadPriority)state; } { WorkFileService service = new WorkFileService();//new一个类 WorkFile file = service.NetDownAutoUpdateFiles(ConfigurationManager.AppSettings["fileBigType"], ConfigurationManager.AppSettings["fileSmallType"], CurVersion);//根据URL下载信息,根据Http下载,代码在下面 if (file != null && !string.IsNullOrEmpty(file.fileNo)) { if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"DownFiles\"))//穿件一个文件夹放安装程序 { Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"DownFiles\"); } string path = MakeTempUpdateAction(Path.Combine(AppDomain.CurrentDomain.BaseDirectory + @"DownFiles\", file.fileName));//解压程序,并在桌面上放图标 if (OnUpdated != null) { OnUpdated.Invoke(file.fileName.Replace(".zip", "").Replace(".rar", ""), path); } } else { if (OnUpdated != null) { OnUpdated.Invoke(string.Empty, string.Empty); } } } } public class WorkFile : EntityBase public WorkFile NetDownAutoUpdateFiles(string big, string small, string version)//下载程序 { WorkFile file = new WorkFile(); string result = HttpClient.DownLoadData(ConfigurationManager.AppSettings["API_AutoUpdateProgram"], new { fileBigType = big, fileSmallType = small, fileVersion = version });//代码后面后 if (!string.IsNullOrEmpty(result))//若返回的数据不为空,反序列化到类中 { var Msg = JsonHelper.Deserialize<ApiMessage<WorkFile>>(result); if (Msg != null) { file = Msg.data; if (file != null && !string.IsNullOrEmpty(file.fileNo)) { if (HttpClient.HttpDownloadAutoUpdateFile(file.filePath, file.fileName, file.md5))//md5检验 { rep.Add(file); } UnitOfWorkFile.Commit();//使用事务,防止下载到一半断网了 } } } return file; } public static string DownLoadData(string url, object requestData)//根据url下载,使用post传输,用webclient下载,未转换成类,是json字符串 {
/// <summary> /// HTTP下载文件 /// </summary> /// <param name="url">Http相对路径</param> /// <param name="fileName">文件名</param> /// <returns></returns> public static bool HttpDownloadAutoUpdateFile(string url, string fileName, string md5) { try { /// <summary> /// md5校验 /// </summary> /// <param name="FilePath">文件路径</param> /// <returns></returns> private static bool CheckFileMd5ByFilePath(string FilePath, string RightMd5) { bool Right = false; try { if (File.Exists(FilePath)) { using (System.IO.FileStream fileStream = new System.IO.FileStream(FilePath, System.IO.FileMode.Open))//用的本地路径 { using (System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider()) { byte[] retVal = md5.ComputeHash(fileStream); string filemd5 = string.Join("", retVal.Select(b => b.ToString("x2")).ToList()); Right = (filemd5 == RightMd5); } fileStream.Close(); } } } catch (Exception ex) { } return Right; }
/// <summary> /// 操作本地临时压缩文件更新程序 /// </summary> /// <param name="zipFilePath">临时压缩文件位置</param> private static string MakeTempUpdateAction(string zipFilePath) { if (File.Exists(zipFilePath)) { string ParentPath = Path.Combine(Path.GetFullPath("../.."), string.Format("{0}{1}", "MacCheck", DateTime.Now.ToString("yyMMddHHmm"))); if (Directory.Exists(ParentPath)) { Directory.Delete(ParentPath, true); } Directory.CreateDirectory(ParentPath); if (ZipHelper.UnZip(zipFilePath, ParentPath)) { ParentPath = Path.Combine(ParentPath, "MacCheck.Bin"); File.Copy(Path.GetFullPath(@"MacCheck_DB.db"), Path.Combine(ParentPath, @"MacCheck_DB.db"), true); File.Copy(Path.GetFullPath(@"MacCheck.exe.config"), Path.Combine(ParentPath, @"MacCheck.exe.config"), true); //File.Copy(Path.GetFullPath(@"SupConConfig.json"), Path.Combine(ParentPath, @"SupConConfig.json"), true); Utils.CreateShortCut("设备点巡检", Path.Combine(ParentPath, @"MacCheck.exe"), ParentPath, "设备点检程序"); return Path.Combine(ParentPath, @"MacCheck.exe"); }; } return string.Empty; }
|
请发表评论