总述
上位机开发中很少提及软件的升级,一般都是直接打包发送,相对不是很友好,有时无法及时更新软件中的问题和特性。
软件升级
流程
实现
下载文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace RemoteUpgrade
{
class DownloadPack
{
public DownloadPack()
{
}
public delegate void UpdateState(int percent, bool is_error);
public event UpdateState UpdateProcess;
public bool DownloadFile(string url_path, string dest_path)
{
try
{
if (!Directory.Exists(dest_path))
Directory.CreateDirectory(dest_path);
WebClient wc = new WebClient();
string filename = "Update_NewVersion.zip";
filename = Path.Combine(dest_path, filename);
File.Delete(filename);
//wc.DownloadFile(download, filename);
wc.DownloadFileAsync(new Uri(url_path), filename);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wc_DownloadFileCompleted);
//wc.Dispose();
}
catch(Exception e)
{
return false;
}
return true;
}
void wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
(sender as WebClient).Dispose();
if (e.Error != null)
{
UpdateProcess(0, true);
}
else
{
UpdateProcess(100, false);
}
}
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
if (e.ProgressPercentage < 100)
UpdateProcess(e.ProgressPercentage, false);
}
}
}
解压文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ICSharpCode.SharpZipLib.Zip;
namespace RemoteUpgrade
{
class Unpack
{
public Unpack()
{
}
public bool UnZIP(string zipFilePath, string saveDirectory)
{
if (!File.Exists(zipFilePath))
{
return false;
}
try
{
if (!Directory.Exists(saveDirectory))
{
//解压后存放的 文件夹路径
Directory.CreateDirectory(saveDirectory);
}
using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
{
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
// create directory
if (directoryName.Length > 0)
{
//创建目录
string saveDir = Path.Combine(saveDirectory, directoryName);
Directory.CreateDirectory(saveDir);
}
if (fileName != String.Empty)
{
string saveFilePath = Path.Combine(saveDirectory, theEntry.Name);
using (FileStream streamWriter = File.Create(saveFilePath))
{
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
}
}
}
}
}
catch(Exception ex)
{
return false;
}
return true;
}
}
}
应用传参
ShellExecute(IntPtr.Zero, "open", 应用路径, 参数, null , 1);
总结
升级流程相对比较简单,但是要考虑异常情况的处理,防止升级后的软件打开情况,需要做好校验工作。文章来源:https://www.toymoban.com/news/detail-618969.html
获取程序
关注微信公众号 Chipcode 发送bms文章来源地址https://www.toymoban.com/news/detail-618969.html
到了这里,关于BMS上位机(三)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!