123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- using System;
- using System.Windows.Forms;
- using SevenZip;
- using System.IO;
- using System.Threading;
- using System.Windows;
- namespace AutomaticUpdate
- {
- public partial class FormMain : Form
- {
- public FormMain()
- {
- InitializeComponent();
- this.tbc_Main.TabPages.Remove(this.tp_Compress);
- this.Shown += FormMain_Shown;
-
- }
- //要结束的进程文件路径
- private static string killPath = "LYFZ.Software.PhotoStudio.exe";
- /// <summary>
- /// 要结束的进程文件路径
- /// </summary>
- public static string KillPath
- {
- get { return FormMain.killPath; }
- set { FormMain.killPath = value; }
- }
- static string inputCompressFilePath = "";
- /// <summary>
- /// 输入的解压文件路径
- /// </summary>
- public static string InputCompressFilePath
- {
- get { return FormMain.inputCompressFilePath; }
- set { FormMain.inputCompressFilePath = value; }
- }
- public static string runApplicationPath = System.Windows.Forms.Application.StartupPath + "\\LYFZ.Software.PhotoStudio.exe";
- void FormMain_Shown(object sender, EventArgs e)
- {
- // System.Threading.Thread.Sleep(500);
- bool create;//LYFZ.Software.PhotoStudio
- using (Mutex mu = new Mutex(true, "LYFZ.Software.PhotoStudio", out create))
- {
-
- if (create)
- {
- KillProcessExists(killPath);
- b_Extract_Click(null, null);
- }
- else
- {
- MessageBox.Show("客户端程序已经在运行,将关闭后重试!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- KillProcessExists(killPath);
- b_Extract_Click(null, null);
- }
- }
-
-
- }
- private void KillProcessExists(string _killPath)
- {
- // string str = Application.ExecutablePath;
- try
- {
- string processesByName = System.IO.Path.GetFileNameWithoutExtension(_killPath);
- System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(processesByName);
- foreach (System.Diagnostics.Process p in processes)
- {
- if (System.IO.Path.GetFileName(_killPath.ToLower()) == System.IO.Path.GetFileName(p.MainModule.FileName.ToLower()))
- {
- p.Kill();
- p.Close();
- }
- }
- }
- catch { }
- }
- private void FormMain_Load(object sender, EventArgs e)
- {
- //NewLatestVersion.rar
- string NewLatestVersionFilePath = System.Windows.Forms.Application.StartupPath + "\\UpdateTemp\\ClientNewLatestVersion";
- if (FormMain.inputCompressFilePath.Trim().Length <= 3)
- {
- if (System.IO.File.Exists(NewLatestVersionFilePath + ".rar"))
- {
- NewLatestVersionFilePath = NewLatestVersionFilePath + ".rar";
- }
- else if (System.IO.File.Exists(NewLatestVersionFilePath + ".zip"))
- {
- NewLatestVersionFilePath = NewLatestVersionFilePath + ".zip";
- }
- else if (System.IO.File.Exists(NewLatestVersionFilePath + ".7z"))
- {
- NewLatestVersionFilePath = NewLatestVersionFilePath + ".7z";
- }
- else
- {
- NewLatestVersionFilePath = NewLatestVersionFilePath + ".7z";
- }
- }
- else {
- NewLatestVersionFilePath = FormMain.inputCompressFilePath.Trim();
- }
- if (!System.IO.File.Exists(NewLatestVersionFilePath))
- {
- MessageBox.Show("没有找到相关更新包文件");
- this.Close();
- return;
- }
- cb_Format.Items.AddRange(Enum.GetNames(typeof(OutArchiveFormat)));
- cb_Method.Items.AddRange(Enum.GetNames(typeof(CompressionMethod)));
- cb_Format.SelectedIndex = (int)OutArchiveFormat.SevenZip;
- cb_Method.SelectedIndex = (int)CompressionMethod.Default;
- // tb_CompressDirectory.Text = @"C:\TEST\新管理软件201517\LYFZ_Server_V150117";
- // tb_CompressOutput.Text = @"C:\新管理软件201517.7z";
- tb_ExtractDirectory.Text =System.Windows.Forms.Application.StartupPath+ "\\";
- tb_ExtractArchive.Text = NewLatestVersionFilePath;
- trb_Level.Maximum = Enum.GetNames(typeof(CompressionLevel)).Length - 1;
- trb_Level.Value = (int)CompressionLevel.Normal;
- trb_Level_Scroll(this, EventArgs.Empty);
-
- }
- /// <summary>
- /// 运行外部应用程序
- /// </summary>
- /// <param name="path"></param>
- public static void RunStart(string path)
- {
- FormMain.RunStart(path, "UpdateSuccess");
- }
- /// <summary>
- /// 运行外部应用程序
- /// </summary>
- /// <param name="path"></param>
- public static void RunStart(string path, string arguments = "")
- {
- try
- {
- if (System.IO.File.Exists(System.Windows.Forms.Application.StartupPath + path))
- {
- System.Diagnostics.Process.Start(System.Windows.Forms.Application.StartupPath + path, arguments);
- }
- else
- {
- System.Diagnostics.Process.Start(path, arguments);
- }
- }
- catch
- {
- }
- }
- private void b_Compress_Click(object sender, EventArgs e)
- {
- SevenZipCompressor.SetLibraryPath(@"7z.dll");
- SevenZipCompressor cmp = new SevenZipCompressor();
- cmp.Compressing += new EventHandler<ProgressEventArgs>(cmp_Compressing);
- cmp.FileCompressionStarted += new EventHandler<FileNameEventArgs>(cmp_FileCompressionStarted);
- cmp.CompressionFinished += new EventHandler<EventArgs>(cmp_CompressionFinished);
- cmp.ArchiveFormat = (OutArchiveFormat)Enum.Parse(typeof(OutArchiveFormat), cb_Format.Text);
- cmp.CompressionLevel = (CompressionLevel)trb_Level.Value;
- cmp.CompressionMethod = (CompressionMethod)cb_Method.SelectedIndex;
- cmp.VolumeSize = chb_Volumes.Checked ? (int)nup_VolumeSize.Value : 0;
- string directory = tb_CompressDirectory.Text;
- string archFileName = tb_CompressOutput.Text;
- bool sfxMode = chb_Sfx.Checked;
- if (!sfxMode)
- {
- cmp.BeginCompressDirectory(directory, archFileName);
-
- }
- else
- {
- // Build SevenZipSharp with SFX
- /* SevenZipSfx sfx = new SevenZipSfx();
- using (MemoryStream ms = new MemoryStream())
- {
- cmp.CompressDirectory(directory, ms);
- sfx.MakeSfx(ms, archFileName.Substring(0, archFileName.LastIndexOf('.')) + ".exe");
- }*/
- }
- }
- void cmp_Compressing(object sender, ProgressEventArgs e)
- {
- pb_CompressProgress.Increment(e.PercentDelta);
- }
- void cmp_FileCompressionStarted(object sender, FileNameEventArgs e)
- {
- l_CompressProgress.Text = String.Format("格式 \"{0}\"", e.FileName);
- }
- void cmp_CompressionFinished(object sender, EventArgs e)
- {
- l_CompressProgress.Text = "完成";
- pb_CompressWork.Style = ProgressBarStyle.Blocks;
- pb_CompressProgress.Value = 0;
- }
- private void b_Browse_Click(object sender, EventArgs e)
- {
- if (fbd_Directory.ShowDialog() == DialogResult.OK)
- {
- tb_CompressDirectory.Text = fbd_Directory.SelectedPath;
- }
- }
- private void trb_Level_Scroll(object sender, EventArgs e)
- {
- l_CompressionLevel.Text = String.Format("压缩级别: {0}", (CompressionLevel)trb_Level.Value);
- }
- private void b_BrowseOut_Click(object sender, EventArgs e)
- {
- if (sfd_Archive.ShowDialog() == DialogResult.OK)
- {
- tb_CompressOutput.Text = sfd_Archive.FileName;
- }
- }
- void extr_ExtractionFinished(object sender, EventArgs e)
- {
-
- pb_ExtractWork.Style = ProgressBarStyle.Blocks;
- pb_ExtractProgress.Value = 0;
- pb_ExtractWork.Value = 0;
- l_ExtractProgress.Text = "更新完成";
- (sender as SevenZipExtractor).Dispose();
- if (System.IO.File.Exists(runApplicationPath))
- {
- RunStart(runApplicationPath);
- }
- this.Close();
-
- }
- void extr_FileExists(object sender, FileOverwriteEventArgs e)
- {
- tb_Messages.Text += String.Format("警告: \"{0}\" 已经存在;覆盖\r\n", e.FileName);
- }
- void extr_FileExtractionStarted(object sender, FileInfoEventArgs e)
- {
-
- l_ExtractProgress.Text = String.Format("提取 \"{0}\"", e.FileInfo.FileName);
- l_ExtractProgress.Refresh();
- pb_ExtractWork.Increment(1);
- pb_ExtractWork.Refresh();
-
- }
- void extr_Extracting(object sender, ProgressEventArgs e)
- {
-
- pb_ExtractProgress.Increment(e.PercentDelta);
- pb_ExtractProgress.Refresh();
-
- }
- private void b_Extract_Click(object sender, EventArgs e)
- {
- SevenZipExtractor.SetLibraryPath(@"7z.dll");
- string fileName = tb_ExtractArchive.Text;
- string directory = tb_ExtractDirectory.Text;
- if (System.IO.File.Exists(fileName))
- {
- try
- {
- var extr = new SevenZipExtractor(fileName);
- pb_ExtractWork.Maximum = (int)extr.FilesCount;
- extr.Extracting += new EventHandler<ProgressEventArgs>(extr_Extracting);
- extr.FileExtractionStarted += new EventHandler<FileInfoEventArgs>(extr_FileExtractionStarted);
- extr.FileExists += new EventHandler<FileOverwriteEventArgs>(extr_FileExists);
- extr.ExtractionFinished += new EventHandler<EventArgs>(extr_ExtractionFinished);
- System.Threading.Thread.Sleep(200);
- extr.BeginExtractArchive(directory);
- }
- catch(Exception ex) {
- MessageBox.Show("更新失败,请关闭正在运行的程序后重试。\r\n错误原因:"+ex.Message);
- }
- }
- else {
- if (System.IO.File.Exists(runApplicationPath))
- {
- RunStart(runApplicationPath);
- }
- this.Close();
- }
- }
- private void b_ExtractBrowseDirectory_Click(object sender, EventArgs e)
- {
- if (fbd_Directory.ShowDialog() == DialogResult.OK)
- {
- tb_ExtractDirectory.Text = fbd_Directory.SelectedPath;
- }
- }
- private void b_ExtractBrowseArchive_Click(object sender, EventArgs e)
- {
- if (ofd_Archive.ShowDialog() == DialogResult.OK)
- {
- tb_ExtractArchive.Text = ofd_Archive.FileName;
- using (SevenZipExtractor extr = new SevenZipExtractor(ofd_Archive.FileName))
- {
- var lines = new string[extr.ArchiveFileNames.Count];
- extr.ArchiveFileNames.CopyTo(lines, 0);
- tb_Messages.Lines = lines;
- }
- }
- }
- }
- }
|