You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

520 lines
17 KiB
C#

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
namespace POSV
{
public partial class DownloadCefDependentFile : BusinessForm
{
private List<string> m_FileName = new List<string>();
private List<string> m_DownloadFile = new List<string>();
public bool IsAccomplish = false;
private const string M_SOURCEFILE = "http://canyin.ffcygl.com/cef/cef.zip";
private string M_DESFILE = Application.StartupPath;
private Task task = null;
private bool m_stoptask = false;
private NLog.Logger Logger = null;
public DownloadCefDependentFile()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
this.pictureBox1.Refresh();
Logger = NLog.LogManager.GetLogger(GetType().FullName);
//所需下载文件清单
m_FileName.Add("cef.pak");
m_FileName.Add("cef_100_percent.pak");
m_FileName.Add("cef_200_percent.pak");
m_FileName.Add("cef_extensions.pak");
m_FileName.Add("CefSharp.BrowserSubprocess.Core.dll");
m_FileName.Add("CefSharp.BrowserSubprocess.exe");
m_FileName.Add("d3dcompiler_43.dll");
m_FileName.Add("d3dcompiler_47.dll");
m_FileName.Add("devtools_resources.pak");
m_FileName.Add("icudtl.dat");
m_FileName.Add("libcef.dll");
m_FileName.Add("libEGL.dll");
m_FileName.Add("libGLESv2.dll");
m_FileName.Add("natives_blob.bin");
m_FileName.Add("snapshot_blob.bin");
m_FileName.Add("widevinecdmadapter.dll");
}
/// <summary>
/// 添加下载内容
/// </summary>
private string Lblcontent
{
set { this.lblcontent.Text = "正在下载" + value + "文件..."; }
}
public bool Stoptask
{
get { return m_stoptask; }
set
{
m_stoptask = value;
this.Close();
}
}
public bool Needtodownload()
{
if (IsInstallation())
{
return IsFile();
}
return false;
}
private void EnvironmentDetection()
{
bool C = false;
bool All = false;
//1、检查C++环境
{
this.lblcontent.Text = "检测C++环境是否安装...";
All = C = this.IsInstallation();
if (Stoptask)
{
return;
}
}
//2、检查程序路径cef依赖文件
{
if (C)
{
this.lblcontent.Text = "检查程序路径CEF依赖文件...";
All = this.IsFile();
if (Stoptask)
{
return;
}
}
}
//3、下载文件
{
this.lblcontent.Text = "正在下载餐饮系统关键文件,请稍后...";
if (!All)
{
this.DeownloadFile(M_SOURCEFILE, M_DESFILE);
if (Stoptask)
{
return;
}
}
else
{
this.lblcontent.Text = "运行环境正常...";
Stoptask = true;
if (Stoptask)
{
this.IsAccomplish = Stoptask;
return;
}
}
}
//4、解压文件
{
this.lblcontent.Text = "正在解压文件...";
this.ExtractFile();
if (Stoptask)
{
return;
}
}
//5、安装文件
{
this.lblcontent.Text = "正在安装C++2013运行库";
var cmd = new ProcessStartInfo("cmd.exe", $"/c " + M_DESFILE + "\\vcredist_x86.exe" + " /S")
{
WindowStyle = ProcessWindowStyle.Hidden//启动时隐藏
};
var process = Process.Start(cmd);
process.WaitForExit();
process.Close();
process.Dispose();
if (Stoptask)
{
return;
}
}
//6、判断文件完整性
{
this.lblcontent.Text = "梳理文件中...";
All = this.IsInstallation();
if (All)
{
All = this.IsFile();
}
else
{
Logger.Info("梳理文件中失败C++环境没有安装成功");
}
this.IsAccomplish = All;
if (IsAccomplish)
{
this.Close();
return;
}
else
{
Logger.Info("梳理文件中失败cef文件解压异常");
this.Close();
return;
}
}
}
#region 环境判断
/// <summary>
/// 获取安装程序 Adobe Reader
/// </summary>
/// <returns>true: 有安裝, false:沒有安裝</returns>
private bool IsInstallation()
{
try
{
var reg = new string[] {
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
};
string tempType = null;
int softNum = 0;//所有已经安装的程序数量
RegistryKey currentKey = null;
var ls = new List<Dictionary<string, string>>();
foreach (var item222 in reg)
{
object displayName = null, uninstallString = null, installLocation = null, releaseType = null;
RegistryKey pregkey = Registry.LocalMachine.OpenSubKey(item222);//获取指定路径下的键
foreach (string item in pregkey.GetSubKeyNames()) //循环所有子键
{
currentKey = pregkey.OpenSubKey(item);
displayName = currentKey.GetValue("DisplayName"); //获取显示名称
installLocation = currentKey.GetValue("InstallLocation"); //获取安装路径
uninstallString = currentKey.GetValue("UninstallString"); //获取卸载字符串路径
releaseType = currentKey.GetValue("ReleaseType"); //发行类型,值是Security Update为安全更新,Update为更新
if (displayName == null)
{
continue;
}
if ((displayName as String).Contains("Microsoft Visual C++ 2013"))
{
if ((displayName as String).Contains("x86"))
{
return true;
}
}
#region ..
//if (releaseType != null)
//{
// tempType = releaseType.ToString();
// if (tempType == "Security Update" || tempType == "Update")
// {
// isSecurityUpdate = true;
// }
//}
//if (!isSecurityUpdate && displayName != null && uninstallString != null)
//{
// softNum++;
// if (installLocation == null)
// {
// ls.Add(new Dictionary<string, string> { { displayName.ToString(), "" } });
// }
// else
// {
// ls.Add(new Dictionary<string, string> { { displayName.ToString(), installLocation.ToString() } });
// }
//}
#endregion
}
}
return false;
}
catch (Exception ex)
{
LOGGER.Info(ex);
return false;
}
}
/// <summary>
/// 判断缺失所需文件
/// </summary>
private bool IsFile()
{
foreach (var _fileitem in m_FileName)
{
if (!File.Exists(System.Environment.CurrentDirectory + "\\" + _fileitem))
{
m_DownloadFile.Add(_fileitem);
return false;
}
}
return true;
}
#endregion
private void touchButtonX1_Click(object sender, EventArgs e)
{
if (!task.IsCompleted)
{
var _BoxButtons = MessageBox.Show("如果运行没有完全安装完毕无法正常运行程序,是否继续!", "提示", MessageBoxButtons.OKCancel);
if (_BoxButtons == DialogResult.OK)
{
this.Stoptask = true;
}
}
else
{
this.Close();
}
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="sourceFile"></param>
/// <param name="desFile"></param>
/// <returns></returns>
public bool DeownloadFile(string sourceFile, string desFile)
{
bool flag = false;
long SPosition = 0;
FileStream FStream = null;
Stream cefStream = null;
string fileName = sourceFile.Substring(sourceFile.LastIndexOf(@"/") + 1);
if (desFile.EndsWith("\\"))
{
desFile = desFile + fileName;
}
else
{
desFile = desFile + "\\" + fileName;
}
try
{
//下载总大小
long serverFileLength = this.GetHttpLength(sourceFile);
//进度条最大值
this.DeownloadBar.Maximum += (int)serverFileLength;
//判断要下载的文件夹是否存在
if (File.Exists(desFile))
{
//断开下载容易出现文件损坏,删除重新下载
File.Delete(desFile);
//文件不保存创建一个文件
FStream = new FileStream(desFile, FileMode.Create);
SPosition = 0;
////打开上次下载的文件
//FStream = File.OpenWrite(desFile);
////获取已经下载的长度
//SPosition = FStream.Length;
//if (SPosition == serverFileLength)
//{
// //文件是完整的,直接结束下载任务
// return true;
//}
//FStream.Seek(SPosition, SeekOrigin.Current);
}
else
{
//文件不保存创建一个文件
FStream = new FileStream(desFile, FileMode.Create);
SPosition = 0;
}
//打开网络连接
HttpWebRequest _reception = (HttpWebRequest)HttpWebRequest.Create(sourceFile);
if (SPosition > 0)
{
_reception.AddRange(SPosition); //设置Range值
}
//向服务器请求,获得服务器的回应数据流
cefStream = _reception.GetResponse().GetResponseStream();
int Downloadsize = 512;
//定义一个字节数据
byte[] btContent = new byte[Downloadsize];
int intSize = 0;
intSize = cefStream.Read(btContent, 0, Downloadsize);
while (intSize > 0)
{
if (Stoptask)
{
return false;
}
FStream.Write(btContent, 0, intSize);
intSize = cefStream.Read(btContent, 0, Downloadsize);
if (intSize <= 0)
{
LOGGER.Info("");
}
DeownloadBar.Value += intSize;
ProgressPercent();
};
flag = true; //返回true下载成功
}
catch (Exception ex)
{
Logger.Info("下载文件时异常:" + ex.Message);
// SysLog.WriteTxtLog("下载文件时异常:" + ex.Message);
}
finally
{
//关闭流
if (cefStream != null)
{
cefStream.Close();
cefStream.Dispose();
}
if (FStream != null)
{
FStream.Close();
FStream.Dispose();
}
}
return flag;
}
/// <summary>
/// 获取文件大小
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private long GetHttpLength(string url)
{
long length = 0;
try
{
var req = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
req.Method = "HEAD";
req.Timeout = 5000;
var res = (HttpWebResponse)req.GetResponse();
if (res.StatusCode == HttpStatusCode.OK)
{
length = res.ContentLength;
}
res.Close();
return length;
}
catch (WebException wex)
{
Logger.Info("下载文件时异常:" + wex.Message);
return 0;
}
}
/// <summary>
/// 计算百分比
/// </summary>
private void ProgressPercent()
{
try
{
this.lblprogress.Text = (((double)this.DeownloadBar.Value / (double)this.DeownloadBar.Maximum) * 1.00).ToString("0%");
}
catch
{
}
}
/// <summary>
/// 解压文件
/// </summary>
/// <returns></returns>
private bool ExtractFile()
{
ZipStorer _Zip = new ZipStorer();
try
{
if (!File.Exists(M_DESFILE + "\\cef.zip"))
{
this.lblcontent.Text = "检测不到下载内容";
return false;
}
_Zip = ZipStorer.Open(M_DESFILE + "\\cef.zip", FileAccess.Read);
this.lblcontent.Text = "正在解压文件...";
List<ZipStorer.ZipFileEntry> dir = _Zip.ReadCentralDir();
foreach (ZipStorer.ZipFileEntry entry in dir)
{
if (Stoptask)
{
return false;
}
this.lblcontent.Text = "正在解压" + entry.FilenameInZip + "文件...";
_Zip.ExtractFile(entry, M_DESFILE + "\\" + entry.FilenameInZip);
}
this.lblcontent.Text = "解压完成";
_Zip.Close();
return true;
}
catch (Exception ex)
{
this.lblcontent.Text = "解压出现了异常,请联系管理员";
return false;
}
}
private void DownloadCefDependentFile_Load(object sender, EventArgs e)
{
task = Task.Factory.StartNew(() =>
{
this.EnvironmentDetection();
});
}
}
}