private void Update() { try { Thread.Sleep(1000); ProcessHelper proc = new ProcessHelper(); proc.SetExitHandler((s, e1) => { string updateResultPath = System.Windows.Forms.Application.StartupPath + "\\Update.ini"; MyIniFile ini = new MyIniFile(updateResultPath); ini.IniWriteValue("UpdateResult", localFileName, "1"); CheckRegAndRun(); }); proc.PrintDoc(localFileName, "/S"); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show("升级失败,请联系管理员", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); Log4NetHelper.Error(this, "升级失败:", ex); Environment.Exit(1); } }
/// <summary> /// 解压功能(解压压缩文件到指定目录) /// </summary> /// <param name="fileToUnZip">待解压的文件</param> /// <param name="zipedFolder">指定解压目标目录</param> /// <param name="password">密码</param> /// <returns>解压结果</returns> public static bool UnZip(string fileToUnZip, string zipedFolder, string password) { bool result = true; ZipInputStream zipStream = null; ZipEntry ent = null; string fileName = string.Empty; if (!Directory.Exists(zipedFolder)) { Directory.CreateDirectory(zipedFolder); } try { zipStream = new ZipInputStream(File.OpenRead(fileToUnZip)); if (!string.IsNullOrEmpty(password)) { zipStream.Password = password; } while ((ent = zipStream.GetNextEntry()) != null) { if (!string.IsNullOrEmpty(ent.Name)) { fileName = Path.Combine(zipedFolder, ent.Name); fileName = fileName.Replace('/', '\\'); if (fileName.EndsWith("\\")) { Directory.CreateDirectory(fileName); continue; } FileStream fs = File.Create(fileName); int size = 2048; byte[] data = new byte[size]; while (true) { size = zipStream.Read(data, 0, data.Length); if (size > 0) { fs.Write(data, 0, size); } else { break; } } if (fs != null) { fs.Close(); fs.Dispose(); } } } } catch (Exception ex) { result = false; Log4NetHelper.Error(typeof(ZipHelper), "解压文件名:" + fileName + " 报错信息:" + ex.Message.ToString() + System.Environment.NewLine + ex.StackTrace); } finally { if (zipStream != null) { zipStream.Close(); } } return(result); }