必须下载nuget上的donetzip的dll引用文件。
using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
namespace UpgraderA
{
public partial class UpgraderA : Form
{
private string currentPath = Application.StartupPath;
public UpgraderA()
{
InitializeComponent();
ShowInTaskbar = false;
WindowState = FormWindowState.Minimized;
}
private void Upgrade()
{
DirectoryInfo info = new DirectoryInfo(Application.StartupPath);
string path = currentPath + "\\upgradefiles\\";
string version = string.Empty;
string sourcePath = string.Empty;
XmlTextReader textReader = new XmlTextReader(info.Parent.FullName + "\\AutoUpgrader.xml");
while (textReader.Read())
{
if (textReader.Name == "versionNo")
{
version = textReader.GetAttribute("Number");
textReader.Close();
break;
}
}
if (version != null)
{
sourcePath = currentPath + "\\upgradefiles\\Debug(" + version + ").zip";
}
else
{
return;
}
try
{
using (ZipFile zip = new ZipFile(sourcePath))
{
zip.ExtractAll(path, ExtractExistingFileAction.OverwriteSilently);
}
List<FileInfo> listinfos = FindAllFiles(currentPath + "\\upgradefiles\\Debug(" + version + ")");
foreach (var file in listinfos)
{
//包含文件夹的文件复制不过去,必须先建立相应的路径文件夹
string[] strArr = file.FullName.Split(new[] { "Debug(" + version + ")" }, StringSplitOptions.None);
if (!File.Exists(info.Parent.FullName + strArr[1]))
{
string str = (info.Parent.FullName + strArr[1]).Replace(file.Name,"");
if (!Directory.Exists(str))
{
Directory.CreateDirectory(str);
}
}
File.Copy(file.FullName, info.Parent.FullName + strArr[1], true);
}
}
catch (Exception ex)
{
MessageBox.Show("升级出错,请联系管理员或开发人员!" + ex.Message);
}
finally
{
string filename = currentPath + "\\..\\LawRecorderWs.exe";
if (File.Exists(sourcePath))
{
File.Delete(sourcePath);
}
if (Directory.Exists(currentPath + "\\upgradefiles\\Debug(" + version + ")"))
{
Directory.Delete(currentPath + "\\upgradefiles\\Debug(" + version + ")", true);
}
FileInfo newFile = new FileInfo(filename);
if (newFile.Exists)
{
Process.Start(filename);
Process.GetCurrentProcess().Kill();
}
}
}
/// <summary>
/// 使用递归方法查询路径下的所有文件信息
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private static List<FileInfo> FindAllFiles(string filePath)
{
List<FileInfo> infoList = new List<FileInfo>();
if (Directory.Exists(filePath))
{
FileInfo[] allFile = new DirectoryInfo(filePath).GetFiles();
foreach (FileInfo file in allFile)
{
infoList.Add(file);
}
DirectoryInfo[] allDir = new DirectoryInfo(filePath).GetDirectories();
foreach (DirectoryInfo dir in allDir)
{
infoList.AddRange(FindAllFiles(dir.FullName));
}
}
return infoList;
}
private void UpgraderA_Load(object sender, EventArgs e)
{
try
{
if (currentPath.Substring(currentPath.Length - 2) == "\\")
{
currentPath = currentPath.Substring(0, currentPath.Length - 2);
}
//用子线程工作
Thread t = new Thread(new ThreadStart(Upgrade))
{
IsBackground = true//设为后台线程
};
t.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}