1备货调整 2在线升级调整

This commit is contained in:
wule committed 2025-07-14 19:05:52 +08:00
1 parent dd52c9b2d3
commit fef54e1f0f
13 files changed
+290 -182

No files matched your search

+22 -1
View File
@@ -31,13 +31,14 @@ namespace Update
SetFormRoundRectRgn(this, 7);
SetFormRoundRectRgn(panel2, 7);
SetFormRoundRectRgn(this.btnOk, 7);
SetFormRoundRectRgn(this.btnCancel, 7);
}
private void btnOk_Click(object sender, EventArgs e)
{
if (lblMessage.Text == "升级完成,点击确定重启程序")
{
string extPath = System.Environment.CurrentDirectory + "\\" + Program.softName;
string extPath = System.Environment.CurrentDirectory + "\\" + Program.softName.Replace(".exe", "") + ".exe";
System.Diagnostics.Process.Start(extPath);
Application.Exit();
return;
@@ -73,6 +74,16 @@ namespace Update
string tempFile = upTempPath + "\\" + Program.updateUrl.Substring(Program.updateUrl.LastIndexOf("/") + 1);
bool result = Program.DownloadFile(Program.updateUrl, tempFile);
while (true)
{
Invoke(new Action(() =>
{
progressBar1.Value = Program.progress;
}));
if (Program.progress == 100)
break;
}
if (result)
{
ZipFile.ExtractToDirectory(tempFile, upTempPath);
@@ -110,6 +121,7 @@ namespace Update
Thread.Sleep(50);
Invoke(new Action(() =>
{
progressBar1.Value = 100;
lblMessage.Text = "升级完成,点击确定重启程序";
btnOk.Enabled = true;
}));
@@ -118,6 +130,7 @@ namespace Update
{
Invoke(new Action(() =>
{
progressBar1.Value = 100;
this.lblMessage.Text = "升级失败,未获取到升级文件";
btnOk.Enabled = true;
}));
@@ -204,10 +217,18 @@ namespace Update
private void CloseMainProgram()
{
string in_sAppName = Program.softName; //(一个word文档程序)
in_sAppName = in_sAppName.Replace(".exe", "");
try
{
System.Diagnostics.Process[] psiNewcas = System.Diagnostics.Process.GetProcessesByName(in_sAppName);
//System.Diagnostics.Process.GetProcessesByName("");
//var pro = System.Diagnostics.Process.GetProcesses();
//foreach(var p in pro)
//{
// Console.WriteLine(p.ProcessName);
//}
foreach (System.Diagnostics.Process p in psiNewcas)
{
+2 -1
View File
@@ -81,6 +81,7 @@ namespace Update
this.btnCancel.TabStop = false;
this.btnCancel.Text = "取 消";
this.btnCancel.UseVisualStyleBackColor = false;
this.btnCancel.Visible = false;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// panel1
@@ -103,7 +104,7 @@ namespace Update
this.btnClose.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnClose.Font = new System.Drawing.Font("微软雅黑", 11F);
this.btnClose.ForeColor = System.Drawing.Color.White;
this.btnClose.Location = new System.Drawing.Point(240, -8);
this.btnClose.Location = new System.Drawing.Point(238, -5);
this.btnClose.Name = "btnClose";
this.btnClose.Size = new System.Drawing.Size(17, 28);
this.btnClose.TabIndex = 11;
+109 -101
View File
@@ -17,6 +17,7 @@ namespace Update
public static int newVersion = 0;
public static string updateUrl = "";
public static List<string> updateFileUrls = new List<string>();
public static int progress = 0;
/// <summary>
/// 应用程序的主入口点。
@@ -29,7 +30,7 @@ namespace Update
try
{
var text = File.ReadAllText("UpdateConfig.ini");
//File.Delete("UpdateConfig.ini");
File.Delete("UpdateConfig.ini");
var arr = text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
if (arr.Length > 3)
@@ -56,76 +57,76 @@ namespace Update
}
}
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;
//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);
// 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);
}
// 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;
}
// 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);
}
//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();
}
}
}
// using (StreamReader streamReader = new StreamReader(streamToRead, Encoding.UTF8))
// {
// return streamReader.ReadToEnd();
// }
// }
//}
/// <summary>
/// 获取json中的int值
@@ -133,16 +134,16 @@ namespace Update
/// <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;
}
//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>
/// 获取字符串
@@ -150,17 +151,17 @@ namespace Update
/// <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;
}
//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>
/// 返回单个正则匹配项
@@ -168,18 +169,18 @@ namespace Update
/// <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;
}
//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>
/// 下载文件
@@ -192,10 +193,12 @@ namespace Update
try
{
WebClientUtils webClient = new WebClientUtils();
webClient.Timeout = 300;
webClient.Timeout = 600;
if (File.Exists(FilePath))
File.Delete(FilePath);
webClient.DownloadFile(Urlstring, FilePath);
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
var uri = new Uri(Urlstring);
webClient.DownloadFileAsync(uri, FilePath);
return true;
}
catch (Exception ex)
@@ -203,5 +206,10 @@ namespace Update
return false;
}
}
private static void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progress = e.ProgressPercentage;
}
}
}
+1 -1
View File
@@ -29,7 +29,7 @@
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<OutputPath>..\..\bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>