old_flsystem/类库/Grant.Framework/UpdateSoft.cs

365 lines
14 KiB
C#
Raw Normal View History

2022-09-20 03:10:29 +00:00
using CsharpHttpHelper.Down;
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using UI.Framework.Forms;
namespace Grant.Framework
{
public partial class UpdateSoft : BaseForm
{
public UpdateSoft(List<assembly> list)
{
InitializeComponent();
this.list = list;
}
List<assembly> list;
private void UpdateSoft_Load(object sender, EventArgs e)
{
try
{
string html = GetStringBulider(list);
this.webBrowser1.DocumentText = html;
var temp = Process.GetCurrentProcess().MainModule.FileName;
var index = temp.LastIndexOf("\\");
path = temp.Substring(0, index + 1);
exe = temp.Substring(index + 1);
}
catch (Exception)
{ }
}
public string path;
public string exe;
string GetStringBulider(List<assembly> list)
{
StringBuilder builer = new StringBuilder();
try
{
builer.Append("<!DOCTYPE html>").Append("\r\n");
builer.Append("<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">").Append("\r\n");
builer.Append("<head>").Append("\r\n");
builer.Append(" <meta charset=\"utf-8\" />").Append("\r\n");
builer.Append(" <title></title>").Append("\r\n");
builer.Append("</head>").Append("\r\n");
builer.Append("<body style=\"background-color:floralwhite\">").Append("\r\n");
foreach (assembly item in list)
{
builer.Append(" <fieldset>").Append("\r\n");
builer.Append(" <legend>版本编号:" + item.version + "</legend>").Append("\r\n");
builer.Append(" ").Append("\r\n");
builer.Append(" <ul style=\"list-style:decimal\">").Append("\r\n");
string[] infos = item.message.Split(new string[] { "\r\n" }, StringSplitOptions.None);
foreach (string info in infos)
{
if (string.IsNullOrWhiteSpace(info)) continue;
builer.Append("<li>" + info + "</li>").Append("\r\n");
}
builer.Append(" </ul>").Append("\r\n");
builer.Append(" </fieldset>").Append("\r\n");
builer.Append(" <br>").Append("\r\n");
}
builer.Append("").Append("\r\n");
builer.Append("</body>").Append("\r\n");
builer.Append("</html>").Append("\r\n");
}
catch (Exception)
{ }
return builer.ToString();//结果
}
private static Stream st = null;
private static Stream so = null;
internal static void DownloadFile(string URL, string filename, ProgressBarControl prog)
{
float percent = 0;
try
{
if (File.Exists(filename))
File.Delete(filename);
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
long totalBytes = myrp.ContentLength;
prog.Invoke(new Action(delegate
{
prog.Properties.Maximum = (int)totalBytes;
}));
st = myrp.GetResponseStream();
so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
try
{
long totalDownloadedByte = 0;
byte[] by = new byte[1024];
int osize = st.Read(by, 0, (int)by.Length);
while (osize > 0)
{
totalDownloadedByte = osize + totalDownloadedByte;
Application.DoEvents();
so.Write(by, 0, osize);
prog.Invoke(new Action(delegate { prog.Position = (int)totalDownloadedByte; }));
osize = st.Read(by, 0, (int)by.Length);
percent = (float)totalDownloadedByte / (float)totalBytes * 100;
Application.DoEvents(); //必须加注这句代码否则label1将因为循环执行太快而来不及显示信息
}
}
catch (Exception ex)
{
if (so != null)
{
so.Close();
so.Dispose();
so = null;
}
if (st != null)
{
st.Close();
st.Dispose();
st = null;
}
throw ex;
}
finally
{
if (so != null)
{
so.Close();
so.Dispose();
so = null;
}
if (st != null)
{
st.Close();
st.Dispose();
st = null;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
private void UpdateSoft_FormClosing(object sender, FormClosingEventArgs e)
{
if (so != null)
{
so.Close();
so.Dispose();
so = null;
}
if (st != null)
{
st.Close();
st.Dispose();
st = null;
}
}
private void download(string path, string dir)
{
try
{
FileDownloader loader = new FileDownloader(path, dir, (int)5);
loader.data.Clear();
this.Invoke(new MethodInvoker(() =>
{
try
{
statusBar_process.Properties.Maximum = (int)loader.getFileSize();
}
catch (Exception)
{
}
}));
DownloadProgressListener linstenter = new DownloadProgressListener();
linstenter.doSendMsg = new DownloadProgressListener.dlgSendMsg(SendMsgHander);
loader.download(linstenter);
}
catch (Exception ex)
{
DownMsg msg = new DownMsg();
msg.tag = -1;
SendMsgHander(msg);
//Console.WriteLine(ex.Message);
}
}
private void SendMsgHander(DownMsg msg)
{
try
{
switch (msg.tag)
{
case 1:
this.Invoke(new MethodInvoker(() =>
{
try
{
statusBar_process.Position = (int)msg.size;
float count = (float)statusBar_process.Position / (float)statusBar_process.Properties.Maximum;
//Console.WriteLine(msg.size + " " + msg.speed);
if (statusBar_process.Properties.Maximum == msg.size)
{
stop.Stop();
this.statusBar_process.Position = 100;
WriteUpdateLog(list);
Grant.Framework.GrantClient.StartProcess(path + "\\ZipExtract.exe", exe + " Update.zip");
is_update = true;
this.DialogResult = DialogResult.OK;
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "更新失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}));
break;
case -1:
MessageBox.Show("下载失败");
break;
}
}
catch (Exception)
{ }
}
public void WriteUpdateLog(List<assembly> new_log)
{
try
{
var file = path + "\\Cache\\Update.log";
string text = string.Empty;
if (File.Exists(file))
{
text = File.ReadAllText(file);
if (text.Length > 2000)
{
text.Remove(1000, 1000);
text = text.Trim();
text += "...";
}
}
StringBuilder sb = new StringBuilder();
foreach (var item in new_log)
{
sb.Append(item.version);
sb.Append("\r\n");
sb.Append(item.message);
sb.Append("\r\n\r\n");
}
sb.Append(text);
File.WriteAllText(file, sb.ToString());
}
catch (Exception)
{ }
}
Stopwatch stop;
private void button1_Click(object sender, EventArgs e)
{
if (this.button1.Text == "开始更新")
{
this.button1.Text = "更新中...";
this.button1.Enabled = false;
try
{
System.Threading.ThreadPool.QueueUserWorkItem(f =>
{
try
{
var asy = System.Reflection.Assembly.GetExecutingAssembly();
string c = System.IO.Directory.GetCurrentDirectory();
var a = list[0];
DownloadFile(a.url, c + "\\Update.zip", this.statusBar_process);
//string exe = "ZipExtract.exe";
//Grant.Framework.GrantClient.StartProcess(c + "\\" + exe, asy.ManifestModule.Name + " Update.zip");
WriteUpdateLog(list);
this.is_update = true;
GrantClient.StartProcess(c + "\\ZipExtract.exe", exe + " Update.zip");
this.Invoke(new Action(delegate
{
try
{
this.DialogResult = DialogResult.OK;
this.Close();
}
catch (Exception)
{
}
}));
}
catch (Exception ex)
{
try
{
this.Invoke(new Action(delegate
{
MessageBox.Show(ex.Message, "更新失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}));
}
catch (Exception)
{ }
}
});
}
catch (Exception ex)
{
this.Invoke(new Action(delegate
{
MessageBox.Show(ex.Message, "更新失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.button1.Text = "更新";
this.button1.Enabled = true;
}));
}
}
}
string c = System.IO.Directory.GetCurrentDirectory();
public bool is_update = false;
//private void DownThread_doSendMsg(CsharpHttpHelper.Down.DownMsg msg)
//{
// this.Invoke(new Action(delegate
// {
// switch (msg.Tag)
// {
// case CsharpHttpHelper.Down.DownStatus.Start:
// break;
// case CsharpHttpHelper.Down.DownStatus.GetLength:
// //this.Invoke(new Action(delegate
// //{
// // this.statusBar_process.Properties.Maximum = (int)msg.Length;
// //}));
// break;
// case CsharpHttpHelper.Down.DownStatus.DownLoad:
// this.statusBar_process.Position = (int)msg.Progress;
// break;
// case CsharpHttpHelper.Down.DownStatus.End:
// this.statusBar_process.Position = 100;
// // string exe = "ZipExtract.exe";
// // var location = this.GetType().Assembly.Location;
// Grant.Framework.GrantClient.StartProcess(path + "\\ZipExtract.exe" ,exe +" Update.zip");
// is_update = true;
// this.Close();
// break;
// case CsharpHttpHelper.Down.DownStatus.Error:
// ShowError(msg.ErrMessage);
// this.Close();
// break;
// default:
// break;
// }
// }));
//}
}
}