Commit d1141d25 authored by 段廷瑞's avatar 段廷瑞

1、自动更新功能优化;增加进度文本功能及异步UI支持

parent 7ed9a8f5
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<appSettings> <appSettings>
<add key="LoginInfo" value="wwF/xpBzWek=" /> <add key="LoginInfo" value="wwF/xpBzWek=" />
<add key="LoginVerify" value="v86f5Q3Xa7U=" /> <add key="LoginVerify" value="v86f5Q3Xa7U=" />
<add key="UpdateTime" value="2021-11-26 14:21:49.650" /> <add key="UpdateTime" value="2021-11-26 16:12:01.887" />
</appSettings> </appSettings>
<startup> <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
......
...@@ -96,4 +96,10 @@ ...@@ -96,4 +96,10 @@
<UserId>sa</UserId> <UserId>sa</UserId>
<Pwd>a*1</Pwd> <Pwd>a*1</Pwd>
</links> </links>
<links>
<Link>192.168.1.31</Link>
<DbName>FTT_PDM</DbName>
<UserId>sa</UserId>
<Pwd>a*1</Pwd>
</links>
</DocumentElement> </DocumentElement>
\ No newline at end of file
namespace Tools.MyCommon
{
partial class wProgressText
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.BackColor = System.Drawing.Color.LightGreen;
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.ForeColor = System.Drawing.Color.Black;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(179, 22);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// wProgressBar
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Window;
this.ClientSize = new System.Drawing.Size(179, 22);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "wProgressBar";
this.Text = "wProgressBar";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Label label1;
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
//
using Tools.MyUtils;
namespace Tools.MyCommon
{
public partial class wProgressText : wBasic
{
int _max = 0;
public wProgressText()
{
InitializeComponent();
//隐藏窗体边框
this.ShowInTaskbar = false; //不显示在任务栏
this.FormBorderStyle = FormBorderStyle.None;
this.MaximumSize = new Size(180, 22);
this.MinimumSize = new Size(180, 22);
this.FormClosing += WProgressText_FormClosing;
}
/// <summary>
/// 事件-窗口关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WProgressText_FormClosing(object sender, FormClosingEventArgs e)
{
Task.Run(() => Thread.Sleep(200)).Wait(); //窗口结束时停顿0.2秒
}
/// <summary>
/// 设置进度条最大值
/// </summary>
/// <param name="total"></param>
public void SetMaximum(int max)
{
this.AsyncUI(() => {
this._max = max;
this.label1.Text = $"共{max}条数据 已处理0条";
this.Text = $"0/{max}";
});
}
/// <summary>
/// 设置进度条当前位置
/// </summary>
/// <param name="pos"></param>
public void SetPosition(int pos)
{
this.AsyncUI(() => {
this.label1.Text = $"共{_max}条数据 已处理{pos}条";
this.Text = $"{pos}/{_max}";
});
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
\ No newline at end of file
using System; using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Configuration; using System.Configuration;
using System.Data; using System.Data;
...@@ -9,6 +10,7 @@ using System.Security.Cryptography; ...@@ -9,6 +10,7 @@ using System.Security.Cryptography;
using System.Text; using System.Text;
// //
using System.Windows.Forms; using System.Windows.Forms;
using Tools.MyCommon;
namespace Tools.MyUtils namespace Tools.MyUtils
{ {
...@@ -262,6 +264,59 @@ namespace Tools.MyUtils ...@@ -262,6 +264,59 @@ namespace Tools.MyUtils
#endregion #endregion
#region ZIP压缩
/// <summary>
/// Zip:打包专用压缩方法(进度提示)
/// </summary>
/// <param name="bar">进度提示</param>
/// <param name="zipFileName">压缩文件保存路径</param>
/// <param name="fileNames">待压缩的文件列表</param>
/// <param name="rootPath">待压缩文件的根路径:用于定位压缩相对路径</param>
public static void ZipCompress4Package(wProgressText bar, string zipFileName, List<string> fileNames, string rootPath)
{
try
{
using (ZipOutputStream zip = new ZipOutputStream(File.Create(zipFileName)))
{
zip.SetLevel(9); // 压缩级别 0-9
byte[] buffer = new byte[1024]; //缓冲区大小
int iCount = 0;
bar?.SetMaximum(fileNames.Count);
foreach (string path in fileNames)
{
//进度条显示
bar?.SetPosition(++iCount);
//EntryName用以指定压缩层级
string EntryName = path.Substring($@"{rootPath}\".Length); //这里取根目录的相对路径
//创建新入口
ZipEntry entry = new ZipEntry(EntryName) {
DateTime = DateTime.Now
};
zip.PutNextEntry(entry);
//写入源数据
using (FileStream fs = File.OpenRead(path))
{
int size;
while ((size = fs.Read(buffer, 0, buffer.Length)) > 0)
{
zip.Write(buffer, 0, size);
}
}
}
zip.Finish();
zip.Close();
}
}
catch (Exception ex)
{
TipException(ex);
}
}
#endregion
#region winform:提示;帮助窗口;异步UI支持 #region winform:提示;帮助窗口;异步UI支持
/// <summary> /// <summary>
...@@ -541,6 +596,29 @@ namespace Tools.MyUtils ...@@ -541,6 +596,29 @@ namespace Tools.MyUtils
return a <= high && a >= low; return a <= high && a >= low;
} }
#endregion
#region (扩展)控件异步UI
/// <summary>
/// 异步UI:执行方法
/// </summary>
/// <param name="ctrl"></param>
/// <param name="action"></param>
public static void AsyncUI(this Control ctrl, Action action)
{
if (ctrl.Disposing || ctrl.IsDisposed)
return;
//
if (ctrl.InvokeRequired)
{
ctrl.Invoke(action);
}
else
{
action();
}
}
#endregion #endregion
#region 图片分离 #region 图片分离
...@@ -934,5 +1012,6 @@ namespace Tools.MyUtils ...@@ -934,5 +1012,6 @@ namespace Tools.MyUtils
#endregion #endregion
} }
} }
...@@ -106,6 +106,12 @@ ...@@ -106,6 +106,12 @@
<Compile Include="MyCommon\controls\TabControlEx.Designer.cs"> <Compile Include="MyCommon\controls\TabControlEx.Designer.cs">
<DependentUpon>TabControlEx.cs</DependentUpon> <DependentUpon>TabControlEx.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="MyCommon\wProgressText.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="MyCommon\wProgressText.Designer.cs">
<DependentUpon>wProgressText.cs</DependentUpon>
</Compile>
<Compile Include="MyData\AnlysisHelper.cs" /> <Compile Include="MyData\AnlysisHelper.cs" />
<Compile Include="MyData\DBHelper.cs" /> <Compile Include="MyData\DBHelper.cs" />
<Compile Include="MyData\SqlHelper.cs" /> <Compile Include="MyData\SqlHelper.cs" />
...@@ -340,6 +346,9 @@ ...@@ -340,6 +346,9 @@
<EmbeddedResource Include="MyCommon\controls\TabControlEx.resx"> <EmbeddedResource Include="MyCommon\controls\TabControlEx.resx">
<DependentUpon>TabControlEx.cs</DependentUpon> <DependentUpon>TabControlEx.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>
<EmbeddedResource Include="MyCommon\wProgressText.resx">
<DependentUpon>wProgressText.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="MyUtils\wDbSetting.resx"> <EmbeddedResource Include="MyUtils\wDbSetting.resx">
<DependentUpon>wDbSetting.cs</DependentUpon> <DependentUpon>wDbSetting.cs</DependentUpon>
</EmbeddedResource> </EmbeddedResource>
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.btnUpload = new System.Windows.Forms.Button(); this.btnGenZip = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label();
...@@ -43,17 +43,17 @@ ...@@ -43,17 +43,17 @@
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
// //
// btnUpload // btnGenZip
// //
this.btnUpload.FlatStyle = System.Windows.Forms.FlatStyle.Flat; this.btnGenZip.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnUpload.Location = new System.Drawing.Point(74, 303); this.btnGenZip.Location = new System.Drawing.Point(74, 303);
this.btnUpload.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); this.btnGenZip.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.btnUpload.Name = "btnUpload"; this.btnGenZip.Name = "btnGenZip";
this.btnUpload.Size = new System.Drawing.Size(117, 33); this.btnGenZip.Size = new System.Drawing.Size(117, 33);
this.btnUpload.TabIndex = 2; this.btnGenZip.TabIndex = 2;
this.btnUpload.Text = "选择压缩包"; this.btnGenZip.Text = "生成压缩包";
this.btnUpload.UseVisualStyleBackColor = true; this.btnGenZip.UseVisualStyleBackColor = true;
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click); this.btnGenZip.Click += new System.EventHandler(this.btnGenZip_Click);
// //
// label1 // label1
// //
...@@ -181,7 +181,7 @@ ...@@ -181,7 +181,7 @@
this.Controls.Add(this.label3); this.Controls.Add(this.label3);
this.Controls.Add(this.label2); this.Controls.Add(this.label2);
this.Controls.Add(this.label1); this.Controls.Add(this.label1);
this.Controls.Add(this.btnUpload); this.Controls.Add(this.btnGenZip);
this.Controls.Add(this.pictureBox1); this.Controls.Add(this.pictureBox1);
this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
...@@ -198,7 +198,7 @@ ...@@ -198,7 +198,7 @@
#endregion #endregion
private System.Windows.Forms.PictureBox pictureBox1; private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button btnUpload; private System.Windows.Forms.Button btnGenZip;
private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label3;
......
...@@ -17,10 +17,36 @@ namespace Tools.UI.sys ...@@ -17,10 +17,36 @@ namespace Tools.UI.sys
{ {
public partial class wUploadUpdate : wEditBasic public partial class wUploadUpdate : wEditBasic
{ {
/// <summary>
/// 进度文本提示
/// </summary>
wProgressText _bar = null;
/// <summary>
/// 生成的压缩文件列表
/// </summary>
List<string> GenZipFileList = new List<string>();
/// <summary>
/// 构造函数
/// </summary>
public wUploadUpdate() public wUploadUpdate()
{ {
InitializeComponent(); InitializeComponent();
} }
/// <summary>
/// 析构函数
/// </summary>
~wUploadUpdate()
{
//删除所有的压缩包
this.GenZipFileList.ForEach(file =>
{
if (File.Exists(file))
{
File.Delete(file);
}
});
}
private void wUploadUpdate_Load(object sender, EventArgs e) private void wUploadUpdate_Load(object sender, EventArgs e)
{ {
...@@ -57,36 +83,61 @@ namespace Tools.UI.sys ...@@ -57,36 +83,61 @@ namespace Tools.UI.sys
return base.SaveCheck(); return base.SaveCheck();
} }
private void btnUpload_Click(object sender, EventArgs e) private void btnGenZip_Click(object sender, EventArgs e)
{
//对话框
OpenFileDialog dlg = new OpenFileDialog()
{
Multiselect = false,
Filter = "压缩包|*.zip",
InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
};
//结果处理
if (dlg.ShowDialog() == DialogResult.OK)
{ {
//显示进度条窗口
this._bar = new wProgressText();
this._bar.TopMost = true; //顶层显示;
this._bar.Show();
//异步调用
Task.Run(() => {
string Name = "工具平台";
string RootPath = Directory.GetParent(Utils.StartupPath).FullName;
string ZipFileName = $@"{RootPath}\{Name}-{DateTime.Now.ToString("yyyyMMdd-hhmmss")}.zip";
this.GenZipFileList.Add(ZipFileName); //记录压缩文件名称,可能点多次
//路径检查
string path = $@"{RootPath}\AppHome";
if (!Directory.Exists(path))
return;
//打包所有文件
DirectoryInfo dirInfo = new DirectoryInfo(path);
var fileInfos = dirInfo.GetFiles("*.*", SearchOption.AllDirectories).ToList();
fileInfos = fileInfos.Where(
f => !f.FullName.Contains("AutoUpdate")
&& !f.FullName.Contains("Tools.exe.")
).ToList(); //剔除自动更新相关目录
//构造压缩文件路径集合
List<string> FileNames = new List<string>();
FileNames.AddRange(fileInfos.Select(f => f.FullName));
//调用压缩方法
Utils.ZipCompress4Package(this._bar, ZipFileName, FileNames, RootPath);
//赋值基本信息 //赋值基本信息
var bytes = File.ReadAllBytes(dlg.FileName); var bytes = File.ReadAllBytes(ZipFileName);
var drv = this._ds.Tables[0].DefaultView[0];
var name = Path.GetFileName(dlg.FileName);
var size = (bytes.Length / 1000M / 1000M).ToString("0.##"); var size = (bytes.Length / 1000M / 1000M).ToString("0.##");
this.tbUName.Text = name; var name = Path.GetFileName(ZipFileName);
this.tbUSize.Text = size; //数据源赋值
var drv = this._ds.Tables[0].DefaultView[0];
drv["UName"] = name; drv["UName"] = name;
drv["USize"] = size; drv["USize"] = size;
drv["UData"] = bytes; drv["UData"] = bytes;
drv.EndEdit(); drv.EndEdit();
//上传成功,更新图片 //更新UI
this.AsyncUI(() => {
this.tbUName.Text = name;
this.tbUSize.Text = size;
this.pictureBox1.Image = Properties.Resources.压缩包; this.pictureBox1.Image = Properties.Resources.压缩包;
} });
}).ContinueWith((x) => {
if (x.IsCompleted)
this.AsyncUI(() => this._bar.Close());
});
} }
private void btnSave_Click(object sender, EventArgs e) private void btnSave_Click(object sender, EventArgs e)
{ {
this.Save(); this.Save();
//刷新更新日期,即默认本机为最新版本
Utils.AppSettings(APP.UPDATE_TIME, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
} }
} }
} }
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