Commit abac8e37 authored by tr's avatar tr

1、自动更新程序优化:剔除冗余方法和文件

parent 751936b2
......@@ -59,8 +59,6 @@
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SmpDB.cs" />
<Compile Include="Utils.cs" />
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.cs</DependentUpon>
</EmbeddedResource>
......
using System;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.IO;
......@@ -69,8 +71,8 @@ namespace AutoUpdate
private void AppUpdate()
{
string link = Utils.GetDbLinkString204("FTT_Work");
DataTable dt = new SmpDB().GetDataTable(link, "SELECT TOP 1 * FROM dbo.FTT_AutoUpdate ORDER BY UDate DESC", "FTT_AutoUpdate");
string link = this.GetDbLinkString204("FTT_Work");
DataTable dt = this.GetDataTable(link, "SELECT TOP 1 * FROM dbo.FTT_AutoUpdate ORDER BY UDate DESC", "FTT_AutoUpdate");
if (dt.DefaultView.Count > 0)
{
//结束原进程
......@@ -94,7 +96,7 @@ namespace AutoUpdate
File.WriteAllBytes(fileFullName, bytes);
this.AddProcessText(60);
//解压更新包到指定目录
Utils.ZipDecompress(fileFullName, this._path);
this.ZipDecompress(fileFullName, this._path);
this.AddProcessText(90);
//删除更新包
File.Delete(fileFullName);
......@@ -106,6 +108,87 @@ namespace AutoUpdate
this.textBox1.Text += $"进度完成{value}%\r\n";
}
#region 工具方法
/// <summary>
/// 获取数据表
/// </summary>
/// <param name="strLink">数据库连接字串</param>
/// <param name="strSql">SQL语句</param>
/// <param name="strTableName">表名</param>
/// <param name="Schema">是否获取结构信息</param>
/// <returns></returns>
public DataTable GetDataTable(string strLink, string strSql, string strTableName, bool Schema = true)
{
DataTable dt = new DataTable(strTableName);
SqlConnection conn = null;
try
{
conn = new SqlConnection(strLink);
conn.Open();
var ada = new SqlDataAdapter(strSql, conn);
if (Schema) ada.FillSchema(dt, SchemaType.Mapped);//取出数据结构
ada.Fill(dt);//填充数据
}
catch (Exception ex)
{
if (conn.State == ConnectionState.Open)
conn.Close();
throw (new Exception("获取数据出错:\r\n" + ex.Message));
}
return dt;
}
/// <summary>
/// 获取204的数据库链接
/// </summary>
/// <param name="dbName"></param>
/// <returns></returns>
public string GetDbLinkString204(string dbName)
{
return $"Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog={dbName};Data Source=jhdsoft.gicp.net,3204";
}
/// <summary>
/// Zip:解压文件
/// </summary>
/// <param name="zipFileName">压缩包路径</param>
/// <param name="outFilePath">解压文件路径(缺省时,默认为桌面路径)</param>
public void ZipDecompress(string zipFileName, string outFilePath = "")
{
if (!File.Exists(zipFileName))
{
MessageBox.Show($"Cannot find file '{zipFileName}'");
return;
}
if (string.IsNullOrEmpty(outFilePath))
{
outFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
}
using ZipInputStream zip = new ZipInputStream(File.OpenRead(zipFileName));
//zip.Password = "123";
byte[] buffer = new byte[1024]; //缓冲区大小
ZipEntry entry;
while ((entry = zip.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(entry.Name);
string fileName = Path.GetFileName(entry.Name);
if (string.IsNullOrEmpty(directoryName) || string.IsNullOrEmpty(fileName))
continue;
//创建解压文件路径(压缩只考虑相对路径,所以这里创建时要加上输出路径)
Directory.CreateDirectory($@"{outFilePath}\{directoryName}");
using FileStream fs = File.Create($@"{outFilePath}\{entry.Name}"); //entry.Name <=> directoryName + fileName
//写入解压流
int size = 0;
while ((size = zip.Read(buffer, 0, buffer.Length)) > 0)
{
fs.Write(buffer, 0, size);
}
}
}
#endregion
}
}
This diff is collapsed.
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AutoUpdate
{
public class Utils
{
/// <summary>
/// 获取204的数据库链接
/// </summary>
/// <param name="dbName"></param>
/// <returns></returns>
public static string GetDbLinkString204(string dbName)
{
return $"Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog={dbName};Data Source=jhdsoft.gicp.net,3204";
}
/// <summary>
/// Zip:解压文件
/// </summary>
/// <param name="zipFileName">压缩包路径</param>
/// <param name="outFilePath">解压文件路径(缺省时,默认为桌面路径)</param>
public static void ZipDecompress(string zipFileName, string outFilePath = "")
{
if (!File.Exists(zipFileName))
{
MessageBox.Show($"Cannot find file '{zipFileName}'");
return;
}
if (string.IsNullOrEmpty(outFilePath))
{
outFilePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
}
using ZipInputStream zip = new ZipInputStream(File.OpenRead(zipFileName));
//zip.Password = "123";
byte[] buffer = new byte[1024]; //缓冲区大小
ZipEntry entry;
while ((entry = zip.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(entry.Name);
string fileName = Path.GetFileName(entry.Name);
if (string.IsNullOrEmpty(directoryName) || string.IsNullOrEmpty(fileName))
continue;
//创建解压文件路径(压缩只考虑相对路径,所以这里创建时要加上输出路径)
Directory.CreateDirectory($@"{outFilePath}\{directoryName}");
using FileStream fs = File.Create($@"{outFilePath}\{entry.Name}"); //entry.Name <=> directoryName + fileName
//写入解压流
int size = 0;
while ((size = zip.Read(buffer, 0, buffer.Length)) > 0)
{
fs.Write(buffer, 0, size);
}
}
}
}
}
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