Files
2026-07-10 15:25:05 +08:00

301 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using CommonLib;
using CommonLib.data;
namespace Lskj.Main.sub
{
/// <summary>
/// 文件更新类
/// </summary>
public partial class FrmDownLoad : FormBase
{
/// <summary>
/// 文件位置下载模式结构
/// </summary>
private class FileLocationData
{
public string Location = "";
public string FileName = "";
public int DownloadModel = 0;
}
//扩展名数据存储列表
private SortedList<string, FileLocationData> extList;
//核心程序更新列表
private SortedList<string, FileLocationData> kernalList;
//主文件名称(为空表示不存在更新)
private string m_MainFileName = "";
public FrmDownLoad()
{
InitializeComponent();
}
private void FrmDownLoad_Load(object sender, EventArgs e)
{
//默认升级失败
DialogResult = System.Windows.Forms.DialogResult.Abort;
if (ERPInfo.Instance.UpdateIP == string.Empty)
return;
extList = new SortedList<string, FileLocationData>();
kernalList = new SortedList<string, FileLocationData>();
string strDir = "\\\\" + ERPInfo.Instance.UpdateIP + "\\update";
//if (false)
// strDir = "D:\\update";
//这里不再继续对路径做存在判断了
//if (Directory.Exists(strDir) == false)
// return;
try
{
this.Text = "";
string strSql = "select FileName,Location,DownloadModel from FileLocationRelationTab";
using (SqlDataReader dr = SqlHelper.ExecuteReader(CommandType.Text, strSql, null))
{
while (dr.Read())
{
FileLocationData data = null;
string strKey = strDir+"\\"+dr["Location"].ToString().Trim() ;
strKey = Path.GetDirectoryName(strKey) +"\\"+ dr["FileName"].ToString().Trim();
if (extList.TryGetValue(strKey, out data) == false)
{
data = new FileLocationData();
extList.Add(strKey, data);
}
data.Location = dr["Location"].ToString().Trim();
data.DownloadModel = int.Parse( dr["DownloadModel"].ToString());
data.FileName = dr["FileName"].ToString().Trim();
}
}
BeginCheckFile(strDir);
//升级成功
DialogResult = System.Windows.Forms.DialogResult.OK;
if (m_MainFileName != "")
{
if (Directory.Exists(PubUtil.AbsolutelyPath + "tempfiles") == false)
Directory.CreateDirectory(PubUtil.AbsolutelyPath + "tempfiles");
StreamWriter sw = new StreamWriter(PubUtil.AbsolutelyPath + "tempfiles/a_12_m_1z.bat", false, System.Text.Encoding.Default);
foreach (string item in kernalList.Keys)
{
sw.WriteLine("@echo off ");
sw.WriteLine(":delcontinue ");
sw.WriteLine(string.Format("del {0} /f /q ", kernalList[item].FileName));
sw.WriteLine(string.Format("if exist {0} goto delcontinue ", kernalList[item].FileName));
sw.WriteLine(":renamecontinue ");
sw.WriteLine(string.Format("rename {0} {1} ", item, kernalList[item].FileName));
sw.WriteLine(string.Format("if not exist {0} goto renamecontinue ", kernalList[item].FileName));
//sw.WriteLine(string.Format("{0} ", PubUtil.AbsolutelyFileName));
}
//sw.WriteLine("@echo off ");
//sw.WriteLine(":delcontinue ");
//sw.WriteLine(string.Format("del {0} /f /q ", PubUtil.AbsolutelyFileName));
//sw.WriteLine(string.Format("if exist {0} goto delcontinue ", PubUtil.AbsolutelyFileName));
//sw.WriteLine(":renamecontinue ");
//sw.WriteLine(string.Format("rename {0} {1} ", m_MainFileName, Path.GetFileName(PubUtil.AbsolutelyFileName)));
//sw.WriteLine(string.Format("if not exist {0} goto renamecontinue ", PubUtil.AbsolutelyFileName));
//sw.WriteLine(string.Format("{0} ", PubUtil.AbsolutelyFileName));
sw.Flush();
sw.Close();
MessageBox.Show("由于系统发现更新了核心程序,因此需要马上关闭系统进入强制更新程序\r\n更新完成后请重新进入系统!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
PubUtil.WinExec(PubUtil.AbsolutelyPath + "tempfiles/a_12_m_1z.bat", 0);
//MessageBox.Show("由于系统发现更新了核心程序,因此需要马上关闭系统进入强制更新程序\r\n更新完成后请重新进入系统!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
//PubUtil.WinExec(PubUtil.AbsolutelyPath + "UpdateXlFile.exe", 0);
//主程序有更新,必须全部关闭并重启
Application.Exit();
}
}
catch (Exception ex)
{
PubUtil.WriteError("升级出错", ex.Message);
MessageBox.Show("升级出错!请重新登录");
}
}
//拷贝成功的数量
private int m_SuccessCount = 0;
//拷贝失败的数量
private int m_FailedCount = 0;
private void BeginCheckFile(string strDir)
{
string filePath = Application.StartupPath;
progressBar1.Minimum = 0;
string strMainFileName = Path.GetFileName(PubUtil.AbsolutelyFileName);
CopyFile(strDir, filePath + "\\", new byte[102400], strMainFileName);
if (m_SuccessCount != 0 || m_FailedCount != 0)
MessageBox.Show(string.Format("更新完毕,{0}个成功,{1}个失败!", m_SuccessCount, m_FailedCount));
}
/// <summary>
///
/// </summary>
/// <param name="srcdir"></param>
/// <param name="desdir"></param>
private void CopyFile(string srcdir, string desdir, byte[] byteData, string strMainFileName)
{
try
{
if (!Directory.Exists(desdir))
{
Directory.CreateDirectory(desdir);
}
//遍历目录
string[] dirNames = Directory.GetDirectories(srcdir);
foreach (string sDir in dirNames)
{
string[] arr = sDir.Split(new string[]{ "\\"},StringSplitOptions.RemoveEmptyEntries);
string strLastPath = arr[arr.Length-1];
CopyFile(sDir, desdir + strLastPath + "\\", byteData, strMainFileName);
}
string[] filenames = Directory.GetFiles(srcdir);// .GetFileSystemEntries(srcdir);
// 遍历所有的文件和目录
foreach (string file in filenames)
{
string strFileName = Path.GetFileName(file);
string dstFileName = "";
FileLocationData data =null;
int DownloadModel = 1;
if (extList.TryGetValue(srcdir + "\\", out data) || extList.TryGetValue(srcdir + "\\" + strFileName, out data))
{
DownloadModel = data.DownloadModel;
dstFileName = desdir + strFileName;
}
else
{
dstFileName = desdir + strFileName;
}
//只有目标文件存在的情况下才比较
if (File.Exists(dstFileName))
{
//文件版本比较模式下不是最新版本不予下载
if (DownloadModel == 1)
{
System.Diagnostics.FileVersionInfo fiSrc = System.Diagnostics.FileVersionInfo.GetVersionInfo(file);
System.Diagnostics.FileVersionInfo fiDst = System.Diagnostics.FileVersionInfo.GetVersionInfo(dstFileName);
if (string.Compare(fiSrc.FileVersion, fiDst.FileVersion) < 1)
{
FileInfo fiSrc1 = new FileInfo(file);
FileInfo fiDst1 = new FileInfo(dstFileName);
if (fiSrc1.LastWriteTime <= fiDst1.LastWriteTime)
continue;
}
}
//按照最后更新时间模式下不是最新文件不予下载
if (DownloadModel == 0)
{
FileInfo fiSrc = new FileInfo(file);
FileInfo fiDst = new FileInfo(dstFileName);
if (fiSrc.LastWriteTime <= fiDst.LastWriteTime)
continue;
}
}
this.Text = Path.GetFileName(file);
//File.Copy(file, srcfileName, true);无法控制进度
//查看下是否主文件有更新
if (((strMainFileName == Path.GetFileName(dstFileName))) ||
(Path.GetFileName(dstFileName).ToLower() == "commonlib.dll")||
(Path.GetFileName(dstFileName).ToLower() == "controllib.dll")||
(Path.GetFileName(dstFileName).ToLower() == "xlpublic.dll")||
(Path.GetFileName(dstFileName).ToLower() == "delphidll.dll")||
(Path.GetFileName(dstFileName).ToLower() == "publiclibrary.dll"))
{
Random rnd = new Random();
dstFileName = Path.GetFileName(dstFileName);
string srcFileName = Path.GetFileName(dstFileName);
dstFileName = rnd.Next(1000000).ToString() + dstFileName;
dstFileName =desdir + dstFileName;
//MessageBox.Show("destFileName:" + dstFileName + "\n\r sourceFile:"+srcFileName);
m_MainFileName = dstFileName;
string strKey = dstFileName;
if (kernalList.TryGetValue(strKey, out data) == false)
{
data = new FileLocationData();
kernalList.Add(strKey, data);
data.FileName = srcFileName;
}
//ontinue;
}
FileStream fsSource = null;
FileStream fsDest = null;
try
{
fsSource = File.OpenRead(file);
progressBar1.Maximum = (int)fsSource.Length;
progressBar1.Value = 0;
fsDest = File.Create(dstFileName);
while (fsSource.Position < fsSource.Length)
{
int pos = (int)fsSource.Position;
int len = fsSource.Read(byteData, 0, byteData.Length);
fsDest.Write(byteData,0, len);
progressBar1.Value = (int)fsSource.Position;
}
m_SuccessCount++;
}
catch (Exception ex)
{
PubUtil.WriteError("升级出错",ex.Message);
m_FailedCount++;
}
finally
{
if (fsSource != null)
fsSource.Close();
if (fsDest != null)
fsDest.Close();
}
}
}
catch (Exception ex)
{
PubUtil.WriteError("文件升级错误" , ex.Message);
}
}
}
}