321 lines
12 KiB
C#
321 lines
12 KiB
C#
using SelfFix.Properties;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using System.Xml;
|
||
|
||
namespace SelfFix
|
||
{
|
||
class Program
|
||
{
|
||
static void Main(string[] args)
|
||
{
|
||
try
|
||
{
|
||
Console.ForegroundColor = ConsoleColor.Green;
|
||
Console.WriteLine("正在检测软件运行环境...");
|
||
var path = Environment.CurrentDirectory;
|
||
Console.ForegroundColor = ConsoleColor.Cyan;
|
||
Console.WriteLine("正在检测是否有运行的软件");
|
||
|
||
#region 关闭运行的程序
|
||
Process[] MyProcesses = Process.GetProcesses();
|
||
var proNames = new List<string>() { "FLSystem".ToLower(), "VersionUpdate".ToLower() };
|
||
foreach (Process MyProcess in MyProcesses)
|
||
{
|
||
try
|
||
{
|
||
var name = MyProcess.ProcessName.ToLower();
|
||
foreach (var proName in proNames)
|
||
{
|
||
if (name == proName && MyProcess.MainModule.FileName.StartsWith(path))
|
||
{
|
||
try
|
||
{
|
||
Console.WriteLine($"关闭运行中的软件:{MyProcess.Id},路径:{MyProcess.MainModule.FileName},进程名称:{name}");
|
||
MyProcess.Kill();
|
||
}
|
||
catch (Exception)
|
||
{ }
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine($"未知异常:{ex.Message} - {ex.StackTrace}");
|
||
}
|
||
}
|
||
#endregion
|
||
Console.ForegroundColor = ConsoleColor.Blue;
|
||
Console.WriteLine($"正在检测更新配置文件状态");
|
||
#region 检查配置文件
|
||
var vuName = "VersionUpdate";
|
||
var xml = path + $@"\{vuName}.xml";
|
||
|
||
for (int i = 0; i < 3; i++)
|
||
{
|
||
try
|
||
{
|
||
if (File.Exists(xml))
|
||
try { File.Delete(xml); } catch { }
|
||
FileStream fs = new FileStream(xml, FileMode.OpenOrCreate, FileAccess.ReadWrite);
|
||
StreamWriter sw = new StreamWriter(fs);
|
||
sw.Write(Resources.VersionUpdate);
|
||
sw.Flush();
|
||
sw.Close();
|
||
if (File.Exists(xml))
|
||
break;
|
||
}
|
||
catch (Exception)
|
||
{ }
|
||
}
|
||
#endregion
|
||
|
||
var exe = path + $@"\{vuName}.exe";
|
||
if (File.Exists(exe))
|
||
try { File.Delete(exe); } catch { }
|
||
|
||
Console.WriteLine($"正在检测更新程序");
|
||
WriteResource(exe, Resources.VersionUpdate1);
|
||
|
||
try
|
||
{
|
||
MapPath(@"Cache\VersionUpdate");
|
||
var versions = GetVersions(xml);
|
||
if (versions.Count > 0)
|
||
{
|
||
Console.ForegroundColor = ConsoleColor.White;
|
||
foreach (var item in versions)
|
||
{
|
||
Console.WriteLine("下载文件:" + $@"{path}\Cache\VersionUpdate\{item.Name}_{item.Version}.zip");
|
||
DownloadFile(item.Url, $@"{path}\Cache\VersionUpdate\{item.Name}_{item.Version}.zip");
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{ }
|
||
Console.ForegroundColor = ConsoleColor.Black;
|
||
Console.WriteLine("修复完毕!!!!");
|
||
StartProcess("VersionUpdate.exe", "FLSystem.exe");
|
||
for (int i = 5; i < 1; i++)
|
||
{
|
||
Console.WriteLine($"{i}秒后自动退出");
|
||
Thread.Sleep(1000);
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{ }
|
||
}
|
||
|
||
public static void WriteResource(string fileName, byte[] writeData)
|
||
{
|
||
var writeDataStr = BitConverter.ToString(writeData);
|
||
if (File.Exists(fileName))
|
||
{
|
||
var oldFile = File.ReadAllBytes(fileName);
|
||
var oldFileStr = BitConverter.ToString(oldFile);//将字节数组装换为字符串
|
||
if (writeDataStr != oldFileStr)
|
||
{
|
||
File.WriteAllBytes(fileName, writeData);
|
||
}
|
||
}
|
||
else File.WriteAllBytes(fileName, writeData);
|
||
}
|
||
|
||
public static string MapPath(string path = "", bool CreateDirectory = true)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(path))
|
||
{
|
||
return Environment.CurrentDirectory + "\\";
|
||
}
|
||
path = Path.Combine(Environment.CurrentDirectory + "\\", path);
|
||
if (!(!CreateDirectory || Directory.Exists(path)))
|
||
{
|
||
Directory.CreateDirectory(path);
|
||
}
|
||
return path;
|
||
}
|
||
|
||
public static List<UpdateClass> GetVersions(string XMLPath)
|
||
{
|
||
List<UpdateClass> msgs = new List<UpdateClass>();
|
||
try
|
||
{
|
||
XmlDocument XmlDoc = new XmlDocument();
|
||
|
||
if (!File.Exists(XMLPath)) throw new Exception("找不到VersionUpdate.xml必备文件!");
|
||
//使用的时候,首先声明一个XmlDocument对象,然后调用Load方法,从指定的路径加载XML文件.
|
||
|
||
XmlReaderSettings settings = new XmlReaderSettings();
|
||
settings.IgnoreComments = true;//忽略文档里面的注释
|
||
using (XmlReader reader = XmlReader.Create(XMLPath, settings))
|
||
{
|
||
XmlDoc.Load(reader);
|
||
XmlNode xn = XmlDoc.SelectSingleNode("Files");
|
||
// 得到根节点的所有子节点
|
||
XmlNodeList xnl = xn.ChildNodes;
|
||
foreach (XmlNode node in xnl)
|
||
{
|
||
try
|
||
{
|
||
var Name = node.Attributes["Name"].Value;
|
||
var Url = node.Attributes["Url"].Value;
|
||
var CurVersion = Version.Parse(node.Attributes["Version"].Value);
|
||
|
||
XmlDocument temp_xml = new XmlDocument();
|
||
temp_xml.Load(Url);
|
||
var temp_node = temp_xml.SelectSingleNode("History");
|
||
|
||
var temp_datas = temp_node.SelectNodes("Data");
|
||
//更新内容
|
||
List<XmlNode> temp_update = new List<XmlNode>();
|
||
foreach (XmlNode item in temp_datas)
|
||
{
|
||
var ver = Version.Parse(item.Attributes["Ver"].Value);
|
||
if (ver > CurVersion)
|
||
{
|
||
//发现有更新
|
||
temp_update.Add(item);
|
||
}
|
||
}
|
||
|
||
if (temp_update.Count > 0)
|
||
{
|
||
var MSG = new UpdateClass() { Name = Name, Url = temp_update[0].Attributes["Url"].Value, Message = new List<string>(), Version = temp_update[0].Attributes["Ver"].Value };
|
||
msgs.Add(MSG);
|
||
foreach (var item in temp_update)
|
||
{
|
||
var temps = item.Attributes["Msg"].Value.Split(new string[] { "[br]" }, StringSplitOptions.None);
|
||
foreach (var temp in temps) MSG.Message.Add(temp);
|
||
}
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{
|
||
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception)
|
||
{ }
|
||
return msgs;
|
||
}
|
||
|
||
public static bool DownloadFile(string URL, string filename)
|
||
{
|
||
try
|
||
{
|
||
float percent = 0;
|
||
if (File.Exists(filename)) File.Delete(filename);
|
||
|
||
Stream st = null;
|
||
Stream so = null;
|
||
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;
|
||
|
||
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);
|
||
osize = st.Read(by, 0, (int)by.Length);
|
||
percent = (float)totalDownloadedByte / (float)totalBytes * 100;
|
||
//Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
|
||
}
|
||
return true;
|
||
}
|
||
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)
|
||
{
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public class UpdateClass
|
||
{
|
||
public string Name { get; set; }
|
||
public string Url { get; set; }
|
||
public string Version { get; set; }
|
||
public List<string> Message { get; set; }
|
||
}
|
||
|
||
public static bool StartProcess(string filename, string param = null)
|
||
{
|
||
try
|
||
{
|
||
if (!File.Exists(filename))
|
||
return false;
|
||
|
||
try
|
||
{
|
||
if (!System.IO.File.Exists(filename))
|
||
return false;
|
||
|
||
if (param != null)
|
||
System.Diagnostics.Process.Start(filename, param);
|
||
else
|
||
System.Diagnostics.Process.Start(filename);
|
||
System.Threading.Thread.Sleep(100);
|
||
}
|
||
catch (Exception)
|
||
{
|
||
// MessageBox.Show("启动应用程序时出错!原因:" + ex.Message);
|
||
}
|
||
return false;
|
||
|
||
}
|
||
catch (Exception)
|
||
{
|
||
// MessageBox.Show("启动应用程序时出错!原因:" + ex.Message);
|
||
}
|
||
return false;
|
||
}
|
||
}
|
||
}
|