Commit 38ae9d22 authored by Damon's avatar Damon

完成下载安装功能

parent 3c203046
using System.Net.Http; using System.Data;
using System.Linq;
using System.Net.Http;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -6,7 +8,7 @@ namespace Setup.Common ...@@ -6,7 +8,7 @@ namespace Setup.Common
{ {
internal class ApiHelper internal class ApiHelper
{ {
private const string _url = "http://crm.yilongex.cn/api/client/product/list?name=BaibaoTest"; private const string _url = "http://crm.yilongex.cn/api/client/product/list?productId=P15615";
private static readonly HttpClient _httpClient = new HttpClient(); private static readonly HttpClient _httpClient = new HttpClient();
public static async Task<string> CheckAsync() public static async Task<string> CheckAsync()
...@@ -26,7 +28,10 @@ namespace Setup.Common ...@@ -26,7 +28,10 @@ namespace Setup.Common
var match = Regex.Match(input, pattern); var match = Regex.Match(input, pattern);
if (match.Groups.Count > 0) if (match.Groups.Count > 0)
{ {
return match.Groups[0].Value; var values = match.Groups[0].Value.Split(':');
if (values.Count() != 2) return null;
var url = values[1].Trim('"');
return url.Contains("http:") ? url : "http:" + url;
} }
else else
{ {
......
using System; using System;
using System.IO; using System.IO;
using System.Net;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace Dqkj.Setup namespace Dqkj.Setup
{ {
...@@ -53,5 +55,82 @@ namespace Dqkj.Setup ...@@ -53,5 +55,82 @@ namespace Dqkj.Setup
return dir; return dir;
} }
} }
/// <summary>
/// Http方式下载文件
/// </summary>
/// <param name="url">http地址</param>
/// <param name="localfile">本地文件</param>
/// <returns></returns>
public static async Task<bool> DownloadAsync(string url, string directory, string fileName)
{
bool flag = false;
long startPosition = 0; // 上次下载的文件起始位置
FileStream writeStream; // 写入本地文件流对象
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var localFile = directory + fileName;
// 判断要下载的文件夹是否存在
//if (File.Exists(localFile))
//{
// writeStream = File.OpenWrite(localFile); // 存在则打开要下载的文件
// startPosition = writeStream.Length; // 获取已经下载的长度
// writeStream.Seek(startPosition, SeekOrigin.Current); // 本地文件写入位置定位
//}
//else
//{
// writeStream = new FileStream(localFile, FileMode.Create);// 文件不保存创建一个文件
// startPosition = 0;
//}
if (File.Exists(localFile))
{
//不断点续传
File.Delete(localFile);
}
writeStream = new FileStream(localFile, FileMode.Create);// 文件不保存创建一个文件
startPosition = 0;
try
{
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);// 打开网络连接
if (startPosition > 0)
{
myRequest.AddRange((int)startPosition);// 设置Range值,与上面的writeStream.Seek用意相同,是为了定义远程文件读取位置
}
var response = await myRequest.GetResponseAsync();
Stream readStream = response.GetResponseStream();// 向服务器请求,获得服务器的回应数据流
byte[] btArray = new byte[512];// 定义一个字节数据,用来向readStream读取内容和向writeStream写入内容
int contentSize = await readStream.ReadAsync(btArray, 0, btArray.Length);// 向远程文件读第一次
while (contentSize > 0)// 如果读取长度大于零则继续读
{
await writeStream.WriteAsync(btArray, 0, contentSize);// 写入本地文件
contentSize = await readStream.ReadAsync(btArray, 0, btArray.Length);// 继续向远程文件读取
}
//关闭流
writeStream.Close();
readStream.Close();
flag = true; //返回true下载成功
}
catch (Exception e)
{
writeStream.Close();
flag = false; //返回false下载失败
}
return flag;
}
} }
} }
...@@ -65,8 +65,8 @@ namespace Dqkj.Setup ...@@ -65,8 +65,8 @@ namespace Dqkj.Setup
public async void Download() public async void Download()
{ {
var serverVersion = await GetUpdateInfo(); //var serverVersion = await GetUpdateInfo();
if (serverVersion == null) { throw new Exception("获取安装配置异常!"); } //if (serverVersion == null) { throw new Exception("获取安装配置异常!"); }
if (ProcessHelper.CheckProcess(executeName.Replace(".exe", ""))) if (ProcessHelper.CheckProcess(executeName.Replace(".exe", "")))
{ {
...@@ -82,7 +82,7 @@ namespace Dqkj.Setup ...@@ -82,7 +82,7 @@ namespace Dqkj.Setup
DownloadWebClient client = new DownloadWebClient(); DownloadWebClient client = new DownloadWebClient();
client.DownloadFileCompleted += Client_DownloadFileCompleted; client.DownloadFileCompleted += Client_DownloadFileCompleted;
client.DownloadProgressChanged += Client_DownloadProgressChanged; client.DownloadProgressChanged += Client_DownloadProgressChanged;
client.DownloadFileAsync(new Uri(serverVersion.Url), zip_temp, serverVersion); client.DownloadFileAsync(new Uri(defaultUpdateXmlUrl), zip_temp);
//Application.Run(frm); //Application.Run(frm);
} }
...@@ -103,28 +103,28 @@ namespace Dqkj.Setup ...@@ -103,28 +103,28 @@ namespace Dqkj.Setup
{ {
if (File.Exists(zip_temp)) if (File.Exists(zip_temp))
{ {
var serverVersion = (e.UserState as UpdateInfo); //var serverVersion = (e.UserState as UpdateInfo);
//通过文件MD5检测文件是否下载完整 //通过文件MD5检测文件是否下载完整
var md5 = Utils.GetMD5HashFromFile(zip_temp); //var md5 = Utils.GetMD5HashFromFile(zip_temp);
if (md5 == serverVersion.Md5) //if (md5 == serverVersion.Md5)
{ //{
DecompressZip(zip_temp, _installPath); //解压升级包 DecompressZip(zip_temp, _installPath); //解压升级包
//DirectoryHelper.CopyDirectory(unzip_temp, app); //替换进app目录中 //DirectoryHelper.CopyDirectory(unzip_temp, app); //替换进app目录中
VersionInfo localVersion = new VersionInfo(); VersionInfo localVersion = new VersionInfo();
//替换新版本号 //替换新版本号
localVersion.Version = serverVersion.Version; //localVersion.Version = serverVersion.Version;
//刷新、保存本地版本信息 //刷新、保存本地版本信息
localVersion.Version = serverVersion.Version; //localVersion.Version = serverVersion.Version;
File.WriteAllText(Path.Combine(_installPath, "version.xml"), Encrypt.DesEncrypt(XmlHelper.Serializer<VersionInfo>(localVersion))); File.WriteAllText(Path.Combine(_installPath, "version.xml"), Encrypt.DesEncrypt(XmlHelper.Serializer<VersionInfo>(localVersion)));
Completed(); Completed();
} //}
else //else
{ //{
throw new Exception("下载的安装文件不一致,请稍后再尝试安装"); // throw new Exception("下载的安装文件不一致,请稍后再尝试安装");
} //}
} }
} }
catch (Exception ex) catch (Exception ex)
...@@ -157,6 +157,7 @@ namespace Dqkj.Setup ...@@ -157,6 +157,7 @@ namespace Dqkj.Setup
{ {
info = XmlHelper.Deserialize<UpdateInfo>(serverXml); info = XmlHelper.Deserialize<UpdateInfo>(serverXml);
} }
var result = await Utils.DownloadAsync(defaultUpdateXmlUrl, @"d:\", "test.zip");
} }
catch { } catch { }
return info; return info;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment