Ultron.Ngrok/Ultron.Update/UpdateForm.cs

143 lines
4.5 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Windows.Forms;
namespace Ultron.Update
{
public partial class UpdateForm : Form
{
public string ExecuteFile { get; set; }
public UpdateForm()
{
InitializeComponent();
string fileVersion;
if (File.Exists("uNgrok.exe"))
{
try
{
FileVersionInfo file1 = FileVersionInfo.GetVersionInfo("uNgrok.exe");
fileVersion = string.Format("{0}.{1}.{2}.{3}", file1.FileMajorPart, file1.FileMinorPart, file1.FileBuildPart, file1.FilePrivatePart);
string checkUrl = "https://blog.wixy.cn/api/release/Ultron.Ngrok/" + fileVersion;
HttpGet(checkUrl);
}
catch (Exception)
{
fileVersion = "";
}
}
else
{
label1.Visible = false;
label2.Visible = true;
label2.Text = "未检测到uNgrok";
}
}
public UpdateForm(string[] args)
{
InitializeComponent();
ExecuteFile = args[1];
string updateFile = Directory.GetCurrentDirectory() + @"\"+ ExecuteFile;
if (File.Exists(updateFile))
File.Delete(updateFile);
if (File.Exists(Directory.GetCurrentDirectory() + @"\ngrok_x64.exe"))
File.Delete(Directory.GetCurrentDirectory() + @"\ngrok_x64.exe");
if (File.Exists(Directory.GetCurrentDirectory() + @"\ngrok_x86.exe"))
File.Delete(Directory.GetCurrentDirectory() + @"\ngrok_x86.exe");
DownLoadFile(args[0], args[1]);
}
public void HttpGet(string url)
{
WebClient wc = new WebClient();
Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(uri);
}
public void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
StreamReader read = new StreamReader(e.Result);
string str = read.ReadToEnd();
if (str.Contains("\"code\":1"))
{
label1.Visible = false;
label2.Visible = true;
label2.Text = "未检测到新版本";
return;
}
else
{
label2.Visible = false;
label1.Visible = true;
if (MessageBox.Show("检测到新版本,立即更新?", "版本更新", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
int verStart = str.IndexOf("\"version\":\"") + 11;
int verEnd = str.IndexOf("\",\"title\"");
string version = str.Substring(verStart, verEnd - verStart);
DownLoadFile("https://cloud-disk-1251608065.cos.ap-guangzhou.myqcloud.com/uNgrok_" + version + ".exe", "uNgrok.exe");
}
}
}
private void DownLoadFile(string fileUrl,string fileName)
{
WebClient wc = new WebClient();
wc.DownloadProgressChanged += wc_DownloadProgressChanged;
wc.DownloadFileCompleted += Wc_DownloadFileCompleted;
wc.DownloadFileAsync(new Uri(fileUrl), fileName);
}
private void Wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
label1.Text = "完成";
RunNewApp();
}
// int index = 0;
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
try
{
Action act = () =>
{
progressBar1.Value = e.ProgressPercentage;
label1.Text = e.ProgressPercentage + "%";
};
Invoke(act);
}
catch(Exception ex)
{
}
}
public void RunNewApp()
{
try
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "uNgrok.exe";
startInfo.WindowStyle = ProcessWindowStyle.Normal;
Process.Start(startInfo);
Application.Exit();
}
catch (Exception ex)
{
}
}
}
}