背景
之所以用到自动更新,是因为客户端经常用老版本作业,造成各种各样的问题.
导入此机制的目的是:
只要系统管理者和开发者,上传了压缩文件到服务器,无论作业员愿不愿意,系统打开时都将自动触发更新后台
之所以放在系统打开时,是为了避免影响到正在作业中数据,并且一般状况下,上下班时都会打开或关闭系统.
特别说明
1. 需要特别引用两个DLL,其他都是系统自带:
ICSharpCode.SharpZipLib处理压缩文件
Renci.SshNet用于从sftp服务器上下载文件
2. 版本比对,用到了:System.Version,例如: Version LocalAPtVersion = new Version(APVersion);
3. 不能用7z的压缩后缀,如果是7z的后缀会报错:Wrong Local header signature: 0xAFBC7A37
4. 压缩档可以用自己写的程序,也可以用FileZilla上传,只要sftp空间没问题,下载和解压缩都不会有问题
5. 程序中有sftp上传的逻辑,可以参考
程序逻辑
1.首先编辑两个项目:MES和AutoUpdate
2.在MES的Login Form中检查判断是否有新版本,有则打开AutoUpdate程序
3.AutoUpdate是一个Console程序,AutoUpdate的功能是:
3.1 关闭MES系统
3.2 从sftp下载MES.zip压缩文件
3.3 解压缩并覆盖原文件
3.4 打开MES
3.5 删除压缩文件
Login程序中有关检查版本的代码
string CurrentProjectPath = Environment.CurrentDirectory.ToString();
string APPName = Application.ProductName;
string APVersion = Application.ProductVersion;
DataTable GetAPDefineInfo = GetAPInfoByAPName(APPName);
if (GetAPDefineInfo.Rows.Count > 0)
{
string TempVersion = GetAPDefineInfo.Rows[0]["ap_version"].ToString();
Version DBDefineVersion = new Version(TempVersion);
Version LocalAPtVersion = new Version(APVersion);
if (DBDefineVersion > LocalAPtVersion)
{
Process[] procList = Process.GetProcessesByName("AutoUpdate");
foreach (Process proc in procList)
{
proc.Kill();
}
Process.Start(CurrentProjectPath + "\\" + "AutoUpdate.exe");
}
}
AutoUpdate程序逻辑
using System;
using System.IO;
using System.Diagnostics;
using Renci.SshNet;
using ICSharpCode.SharpZipLib.Zip;
namespace AutoUpdate
{
class Program
{
private static string APPath = Environment.CurrentDirectory.ToString();
private static string ZIPFileName = "MES.zip";
static void Main(string[] args)
{
string Message="Begin";
Message = CloseMESAP();
Console.WriteLine(Message);
Message = DownloadZIPFromSFTP();
Console.WriteLine(Message);
Message = UnMESZIP();
Console.WriteLine(Message);
Message = OpenMESAP();
Console.WriteLine(Message);
Message = DeleteZIP();
Console.WriteLine(Message);
Console.WriteLine("Auto Update Finish");
Console.ReadKey();
}
public static string CloseMESAP()
{
Process[] procList = Process.GetProcessesByName("MES");
foreach (Process proc in procList)
{
proc.Kill();
}
return "Close MES OK";
}
public static string DownloadZIPFromSFTP()
{
string ServerIP = "192.168.1.2";
string UseID = "shmes";
string UserPWD = "mes123456!";
DownloadFile(ZIPFileName, ServerIP, UseID, UserPWD);
return "DownLoad Zip OK";
}
public static string UnMESZIP()
{
string MesStr = UnZip(ZIPFileName, APPath, "MESZIPPassword");
return MesStr;
}
public static string OpenMESAP()
{
System.Diagnostics.Process.Start(APPath + "\\" + "MES.exe");
return "Open MES OK";
}
public static string DeleteZIP()
{
File.Delete(APPath + "\\" + ZIPFileName);
return "DeleteZIP OK";
}
public static string UploadFile(string FileName, string sftpServerIP, string sftpUserID, string sftpUserPWD)
{
string LocalFileName = FileName;
string LocalFullName = Path.Combine(APPath, LocalFileName);
string rndWorkPathName = "MES";
string RemoteFullPath = "/uploads/" + rndWorkPathName + "/" + LocalFileName;
using (var sftpclient = new SftpClient(sftpServerIP, sftpUserID, sftpUserPWD))
{
sftpclient.Connect();
rndWorkPathName = "/uploads/" + rndWorkPathName + "/";
sftpclient.ChangeDirectory("/uploads/");
if (!sftpclient.Exists("MES"))
{
sftpclient.CreateDirectory("MES");
}
sftpclient.ChangeDirectory(rndWorkPathName);
using (var fileStream = new FileStream(LocalFullName, FileMode.Open))
{
sftpclient.BufferSize = 8 * 1024;
sftpclient.UploadFile(fileStream, RemoteFullPath);
fileStream.Close();
}
sftpclient.Dispose();
}
return "OK";
}
public static string DownloadFile(string FileName,string sftpServerIP,string sftpUserID,string sftpUserPWD)
{
string LocalFileName = FileName;
string LocalFullName = Path.Combine(APPath, LocalFileName);
string rndWorkPathName = "MES";
string RemoteFullPath = "/uploads/" + rndWorkPathName + "/" + LocalFileName;
using (var sftpclient = new SftpClient(sftpServerIP, sftpUserID, sftpUserPWD))
{
sftpclient.Connect();
rndWorkPathName = "/uploads/" + rndWorkPathName + "/";
sftpclient.ChangeDirectory(rndWorkPathName);
using (var fileStream = new FileStream(LocalFullName, FileMode.Create))
{
sftpclient.DownloadFile(RemoteFullPath, fileStream);
fileStream.Dispose();
}
sftpclient.Dispose();
}
return "OK";
}
public static string UnZip(string ZipFile, string TargetDirectory, string Password, bool OverWrite = true)
{
string ReturnStr = "UnZIP OK";
if (!TargetDirectory.EndsWith("\\"))
{
TargetDirectory = TargetDirectory + "\\";
}
using (ZipInputStream zipfiles = new ZipInputStream(File.OpenRead(ZipFile)))
{
if (!string.IsNullOrEmpty(Password))
{
zipfiles.Password = Password;
}
ZipEntry theEntry;
while ((theEntry = zipfiles.GetNextEntry()) != null)
{
string directoryName = "";
string pathToZip = "";
pathToZip = theEntry.Name;
if (pathToZip != "")
{
directoryName = Path.GetDirectoryName(pathToZip) + "\\";
directoryName = directoryName.Replace("MES\\","");
}
string fileName = Path.GetFileName(pathToZip);
if (!Directory.Exists(TargetDirectory + directoryName))
{
Directory.CreateDirectory(TargetDirectory + directoryName);
}
if (fileName != "")
{
if ((File.Exists(TargetDirectory + directoryName + fileName) && OverWrite) || (!File.Exists(TargetDirectory + directoryName + fileName)))
{
using (FileStream streamWriter = File.Create(TargetDirectory + directoryName + fileName))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = zipfiles.Read(data, 0, data.Length);
if (size > 0)
streamWriter.Write(data, 0, size);
else
break;
}
streamWriter.Close();
}
}
}
}
zipfiles.Close();
}
return ReturnStr;
}
}
}