123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Net;
- using System.Threading;
- using System.Windows.Forms;
- using System.Xml.Linq;
- namespace prBrowserUpdate
- {
- public class MainForm : Form
- {
- public string currentPath = "";
- public string serverPath = "";
- public string serverFTPName = "RKFTP";
- public string serverFTPPassword = "pryk@2020";
- public string ftpDefaultFolder = "browser";
- private IContainer components;
- private Label label3;
- private Button BtnUpdate;
- private Button BtnCancel;
- private Label labelUpdateFile;
- public MainForm()
- {
- InitializeComponent();
- }
- private void MainForm_Load(object sender, EventArgs e)
- {
- getPath();
- }
- private void getPath()
- {
- currentPath = AppDomain.CurrentDomain.BaseDirectory;
- serverPath = getXMLValue("prBrowser.exe.config", "UpdateUrl");
- }
- private string getVersion(string path)
- {
- string version = "";
- try
- {
- FileVersionInfo file = FileVersionInfo.GetVersionInfo(path);
- return $"{file.FileMajorPart}.{file.FileMinorPart}.{file.FileBuildPart}.{file.FilePrivatePart}";
- }
- catch
- {
- return "";
- }
- }
- private string getXMLValue(string path, string attr)
- {
- string xmlValue = "";
- foreach (XElement item in XDocument.Load(path).Root.Element("appSettings").Elements())
- {
- if (item.Attribute("key").Value == attr)
- {
- xmlValue = item.Attribute("value").Value;
- }
- }
- return xmlValue;
- }
- private void BtnCancel_Click(object sender, EventArgs e)
- {
- this.Close();
-
- }
- private void BtnUpdate_Click(object sender, EventArgs e)
- {
- UpdateApp();
- BtnUpdate.Enabled = false;
- }
-
-
-
- private void UpdateApp()
- {
- if (getUpdateList() != "0")
- {
- return;
- }
- DirectoryInfo directoryInfo = new DirectoryInfo(currentPath + "UpdateList\\");
- string latestFileName = "";
- FileInfo[] files = directoryInfo.GetFiles();
- for (int i = 0; i < files.Length; i++)
- {
- latestFileName = files[i].FullName;
- }
- if (latestFileName == "")
- {
- return;
- }
- IEnumerable<XElement> enumerable = XDocument.Load(latestFileName).Root.Element("list").Elements();
- string message = "";
- foreach (XElement item in enumerable)
- {
- string itemKey = item.Attribute("key").Value;
- string itemValue = item.Attribute("value").Value;
- string serverItemValue = itemValue;
- serverItemValue = ((!(serverItemValue == "")) ? ("/" + serverItemValue) : "/");
- string localPath = currentPath + itemValue;
- string uri = serverPath + ftpDefaultFolder + serverItemValue.Replace("\\", "/");
- message = DownLoad(localPath, itemKey, uri, serverFTPName, serverFTPPassword);
- if (message == "0")
- {
- setUpdateFile(currentPath + itemValue + itemKey);
- continue;
- }
- break;
- }
- if (message == "0")
- {
- MessageBox.Show("更新完成");
-
- System.Environment.Exit(0);
- ExecuteAsAdmin("prBrowser.exe");
-
- }
- else
- {
- MessageBox.Show(message);
- }
- }
-
-
-
-
- private string getUpdateList()
- {
- string message = "0";
- try
- {
- string uri = serverPath + ftpDefaultFolder + "/UpdateList/";
- FtpWebRequest obj = (FtpWebRequest)WebRequest.Create(new Uri(uri));
- obj.Credentials = new NetworkCredential(serverFTPName, serverFTPPassword);
- obj.Method = "LIST";
- WebResponse response = obj.GetResponse();
- StreamReader reader = new StreamReader(response.GetResponseStream());
- for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
- {
- if (!line.Contains("<DIR>"))
- {
- string msg = line.Substring(39).Trim();
- string rtn = DownLoad(currentPath + "UpdateList\\", msg, uri, serverFTPName, serverFTPPassword);
- if (rtn != "0")
- {
- message = rtn;
- break;
- }
- }
- }
- reader.Close();
- response.Close();
- return message;
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- private void delUpdateList()
- {
- new DirectoryInfo(currentPath + "UpdateList\\").Delete(recursive: true);
- }
- public void setUpdateFile(string fileName)
- {
- labelUpdateFile.Text = fileName;
- labelUpdateFile.Refresh();
- label3.Refresh();
- BtnUpdate.Refresh();
- BtnCancel.Refresh();
- }
-
-
-
-
-
-
-
-
-
- private string DownLoad(string filePath, string fileName, string ftpPath, string ftpUserName, string ftpPassWord)
- {
- try
- {
- if (!Directory.Exists(filePath))
- {
- Directory.CreateDirectory(filePath);
- }
- FileStream outputStream = new FileStream(filePath + fileName, FileMode.Create);
- FtpWebRequest obj = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath + fileName));
- obj.Method = "RETR";
- obj.Credentials = new NetworkCredential(ftpUserName, ftpPassWord);
- obj.UseBinary = true;
- FtpWebResponse response = (FtpWebResponse)obj.GetResponse();
- Stream ftpStream = response.GetResponseStream();
- _ = response.ContentLength;
- int bufferSize = 2048;
- byte[] buffer = new byte[bufferSize];
- for (int readCount = ftpStream.Read(buffer, 0, bufferSize); readCount > 0; readCount = ftpStream.Read(buffer, 0, bufferSize))
- {
- outputStream.Write(buffer, 0, readCount);
- }
- ftpStream.Close();
- outputStream.Close();
- response.Close();
- return "0";
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- public string FTPDownLoad(string filePath, string fileName, string ftpPath, string RequedstPath, string ftpUserName, string ftpPassWord)
- {
- try
- {
- string uri = ftpPath + RequedstPath;
- FtpWebRequest obj = (FtpWebRequest)WebRequest.Create(new Uri(uri));
- obj.Credentials = new NetworkCredential(ftpUserName, ftpPassWord);
- obj.Method = "LIST";
- WebResponse response = obj.GetResponse();
- StreamReader reader = new StreamReader(response.GetResponseStream());
- string line = reader.ReadLine();
- while (line != null)
- {
- if (line.Contains("web.config") || line.Contains("debug.log") || line.Contains("prBrowser.exe.config"))
- {
- line = reader.ReadLine();
- continue;
- }
- if (!line.Contains("<DIR>"))
- {
- string msg = line.Substring(39).Trim();
- setUpdateFile(filePath + msg);
- if (!(DownLoad(filePath, msg, uri + "/", ftpUserName, ftpPassWord) == "0"))
- {
- }
- }
- else
- {
- string msg2 = line.Substring(line.LastIndexOf("<DIR>") + 5).Trim();
- if (msg2 != "log")
- {
- FTPDownLoad(filePath + msg2 + "/", "", uri + "/", msg2, ftpUserName, ftpPassWord);
- }
- }
- line = reader.ReadLine();
- }
- reader.Close();
- response.Close();
- return "0";
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- public void ExecuteAsAdmin(string fileName)
- {
- Process process = new Process();
- process.StartInfo.FileName = fileName;
- process.StartInfo.UseShellExecute = true;
- process.StartInfo.Verb = "runas";
- process.Start();
- }
- private void BtnUpdateDelay(object sender, EventArgs e)
- {
- Thread.Sleep(1000);
- UpdateApp();
- BtnUpdate.Enabled = false;
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing && components != null)
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- private void InitializeComponent()
- {
- System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(prBrowserUpdate.MainForm));
- label3 = new System.Windows.Forms.Label();
- BtnUpdate = new System.Windows.Forms.Button();
- BtnCancel = new System.Windows.Forms.Button();
- labelUpdateFile = new System.Windows.Forms.Label();
- SuspendLayout();
- label3.AutoSize = true;
- label3.Location = new System.Drawing.Point(33, 50);
- label3.Name = "label3";
- label3.Size = new System.Drawing.Size(77, 12);
- label3.TabIndex = 2;
- label3.Text = "更新中......";
- BtnUpdate.Location = new System.Drawing.Point(104, 163);
- BtnUpdate.Name = "BtnUpdate";
- BtnUpdate.Size = new System.Drawing.Size(75, 23);
- BtnUpdate.TabIndex = 5;
- BtnUpdate.Text = "更新";
- BtnUpdate.UseVisualStyleBackColor = true;
- BtnUpdate.Click += new System.EventHandler(BtnUpdate_Click);
- BtnCancel.Location = new System.Drawing.Point(283, 163);
- BtnCancel.Name = "BtnCancel";
- BtnCancel.Size = new System.Drawing.Size(75, 23);
- BtnCancel.TabIndex = 6;
- BtnCancel.Text = "退出";
- BtnCancel.UseVisualStyleBackColor = true;
- BtnCancel.Click += new System.EventHandler(BtnCancel_Click);
- labelUpdateFile.Location = new System.Drawing.Point(33, 72);
- labelUpdateFile.Name = "labelUpdateFile";
- labelUpdateFile.Size = new System.Drawing.Size(408, 65);
- labelUpdateFile.TabIndex = 7;
- base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
- base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- base.ClientSize = new System.Drawing.Size(478, 243);
- base.Controls.Add(labelUpdateFile);
- base.Controls.Add(BtnCancel);
- base.Controls.Add(BtnUpdate);
- base.Controls.Add(label3);
- base.Icon = (System.Drawing.Icon)resources.GetObject("$this.Icon");
- base.Name = "MainForm";
- base.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- Text = "更新";
- base.Load += new System.EventHandler(MainForm_Load);
- base.Shown += new System.EventHandler(BtnUpdateDelay);
- ResumeLayout(false);
- PerformLayout();
- }
- }
- }
|