249 lines
9.5 KiB
C#
249 lines
9.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Update
|
|
{
|
|
static class Program
|
|
{
|
|
public static string softName = "";
|
|
public static int oldVersion = 0;
|
|
public static int newVersion = 0;
|
|
public static string updateUrl = "";
|
|
public static List<string> updateFileUrls = new List<string>();
|
|
public static int progress = 0;
|
|
|
|
/// <summary>
|
|
/// 应用程序的主入口点。
|
|
/// </summary>
|
|
[STAThread]
|
|
static void Main(string[] args)
|
|
{
|
|
if (args.Length > 0)
|
|
{
|
|
try
|
|
{
|
|
var arr = args[0].TrimStart('-').Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
|
|
if (arr.Length > 3)
|
|
{
|
|
softName = arr[0];
|
|
oldVersion = Convert.ToInt32(arr[1]);
|
|
newVersion = Convert.ToInt32(arr[2]);
|
|
updateUrl = arr[3];
|
|
|
|
if (oldVersion == 0 || string.IsNullOrEmpty(softName) || string.IsNullOrEmpty(updateUrl))
|
|
return;
|
|
|
|
// 如果有新版本,就继续升级
|
|
if (newVersion > oldVersion)
|
|
{
|
|
//Application.Current.Shutdown(); // 强制升级
|
|
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
Application.Run(new MainForm());
|
|
}
|
|
// 仍然删除旧版数据防止残留
|
|
if (File.Exists("UpdateConfig.ini"))
|
|
File.Delete("UpdateConfig.ini");
|
|
return;
|
|
}
|
|
}
|
|
catch (Exception ex) { }
|
|
}
|
|
|
|
if (File.Exists("UpdateConfig.ini"))
|
|
{
|
|
try
|
|
{
|
|
var text = File.ReadAllText("UpdateConfig.ini");
|
|
File.Delete("UpdateConfig.ini");
|
|
|
|
var arr = text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
|
|
if (arr.Length > 3)
|
|
{
|
|
softName = arr[0];
|
|
oldVersion = Convert.ToInt32(arr[1]);
|
|
newVersion = Convert.ToInt32(arr[2]);
|
|
updateUrl = arr[3];
|
|
|
|
if (oldVersion == 0 || string.IsNullOrEmpty(softName) || string.IsNullOrEmpty(updateUrl))
|
|
return;
|
|
|
|
if (newVersion > oldVersion)
|
|
{
|
|
//Application.Current.Shutdown(); // 强制升级
|
|
|
|
Application.EnableVisualStyles();
|
|
Application.SetCompatibleTextRenderingDefault(false);
|
|
Application.Run(new MainForm());
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex) { }
|
|
}
|
|
}
|
|
|
|
//public static string HttpRequestPost(string url, string postData, string cookies = "", string codeType = "utf-8", string accept = "", string contentType = "")
|
|
//{
|
|
// HttpWebResponse response = null;
|
|
// string result;
|
|
// try
|
|
// {
|
|
// HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
// request.Method = "POST";
|
|
// request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
|
|
// if (!string.IsNullOrEmpty(accept))
|
|
// request.Accept = accept;
|
|
// request.ContentType = "application/x-www-form-urlencoded; charset=" + codeType;
|
|
// if (!string.IsNullOrEmpty(contentType))
|
|
// request.ContentType = contentType;
|
|
|
|
// request.Referer = url;
|
|
// request.AllowAutoRedirect = false;
|
|
// if (!string.IsNullOrEmpty(cookies))
|
|
// request.Headers.Set(HttpRequestHeader.Cookie, cookies);
|
|
|
|
// byte[] byteData = Encoding.GetEncoding(codeType).GetBytes(postData);
|
|
// request.ContentLength = byteData.Length;
|
|
// using (Stream requestStream = request.GetRequestStream())
|
|
// {
|
|
// requestStream.Write(byteData, 0, byteData.Length);
|
|
// }
|
|
|
|
// response = (HttpWebResponse)request.GetResponse();
|
|
// }
|
|
// catch (WebException ex)
|
|
// {
|
|
// if (ex.Status == WebExceptionStatus.ProtocolError)
|
|
// {
|
|
// response = (HttpWebResponse)ex.Response;
|
|
// result = ReadResponse(response);
|
|
// if (response != null) response.Close();
|
|
// return result;
|
|
// }
|
|
// else return ex.Message;
|
|
// }
|
|
// catch (Exception e)
|
|
// {
|
|
// if (response != null) response.Close();
|
|
// return e.Message;
|
|
// }
|
|
// result = ReadResponse(response);
|
|
// if (response != null) response.Close();
|
|
// return result;
|
|
//}
|
|
|
|
//public static string ReadResponse(HttpWebResponse response)
|
|
//{
|
|
// using (Stream responseStream = response.GetResponseStream())
|
|
// {
|
|
// Stream streamToRead = responseStream;
|
|
// if (response.ContentEncoding.ToLower().Contains("gzip"))
|
|
// {
|
|
// streamToRead = new GZipStream(streamToRead, CompressionMode.Decompress);
|
|
// }
|
|
// else if (response.ContentEncoding.ToLower().Contains("deflate"))
|
|
// {
|
|
// streamToRead = new DeflateStream(streamToRead, CompressionMode.Decompress);
|
|
// }
|
|
|
|
// using (StreamReader streamReader = new StreamReader(streamToRead, Encoding.UTF8))
|
|
// {
|
|
// return streamReader.ReadToEnd();
|
|
// }
|
|
// }
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 获取json中的int值
|
|
/// </summary>
|
|
/// <param name="html"></param>
|
|
/// <param name="reg"></param>
|
|
/// <returns></returns>
|
|
//public static int GetIntJson(ref string html, string reg)
|
|
//{
|
|
// string temp = GetMatchRegexString(html, @"['""]" + reg + @"['""][\s]*:[\s]*['""]([^']+?)['""]");
|
|
// if (string.IsNullOrEmpty(temp))
|
|
// temp = GetMatchRegexString(html, @"['""]" + reg + @"['""][\s]*:[\s]*([\d-]+)");
|
|
// int i;
|
|
// if (int.TryParse(temp, out i))
|
|
// return i;
|
|
// return 0;
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 获取字符串
|
|
/// </summary>
|
|
/// <param name="html"></param>
|
|
/// <param name="reg"></param>
|
|
/// <returns></returns>
|
|
//public static string GetStringJson(ref string html, string reg)
|
|
//{
|
|
// string temp = GetMatchRegexString(html, @"['""]" + reg + @"['""][\s]*:[\s]*['""]([^']+?)['""]");
|
|
// if (string.IsNullOrEmpty(temp))
|
|
// temp = GetMatchRegexString(html, @"['""]" + reg + @"['""][\s]*:[\s]*([\d]+)");
|
|
// if (!string.IsNullOrEmpty(temp) && temp.Contains("\""))
|
|
// temp = temp.Substring(0, temp.IndexOf("\""));
|
|
// if (!string.IsNullOrEmpty(temp) && temp.Contains("'"))
|
|
// temp = temp.Substring(0, temp.IndexOf("'"));
|
|
// return temp;
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 返回单个正则匹配项
|
|
/// </summary>
|
|
/// <param name="sourceString"></param>
|
|
/// <param name="RegexString"></param>
|
|
/// <returns></returns>
|
|
//public static string GetMatchRegexString(string sourceString, string RegexString)
|
|
//{
|
|
// if (!string.IsNullOrEmpty(sourceString))
|
|
// {
|
|
// MatchCollection mc = Regex.Matches(sourceString, RegexString, RegexOptions.IgnoreCase);
|
|
// if (mc.Count > 0)
|
|
// {
|
|
// return mc[0].Groups[1].Value.ToString();
|
|
// }
|
|
// }
|
|
// return null;
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 下载文件
|
|
/// </summary>
|
|
/// <param name="Urlstring">下载地址</param>
|
|
/// <param name="FilePath">保存目录</param>
|
|
/// <returns></returns>
|
|
public static bool DownloadFile(string Urlstring, string FilePath)
|
|
{
|
|
try
|
|
{
|
|
WebClientUtils webClient = new WebClientUtils();
|
|
webClient.Timeout = 1200; // 20分钟
|
|
if (File.Exists(FilePath))
|
|
File.Delete(FilePath);
|
|
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
|
|
var uri = new Uri(Urlstring);
|
|
webClient.DownloadFileAsync(uri, FilePath);
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private static void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
|
|
{
|
|
progress = e.ProgressPercentage;
|
|
}
|
|
}
|
|
}
|