123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Net;
- using System.Xml.Linq;
- namespace prBrowser.Weblogic
- {
- public class FTPHelper
- {
- public string currentPath = "";
- public string serverPath = "";
- public string serverFTPName = "RKFTP";
- public string serverFTPPassword = "pryk@2020";
- public string ftpDefaultFolder = "browser";
- public 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 reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath + fileName));
- reqFTP.Method = "RETR";
- reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPassWord);
- reqFTP.UseBinary = true;
- FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
- Stream ftpStream = response.GetResponseStream();
- long cl = 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();
- reqFTP = null;
- return "0";
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- public string getUpdateList()
- {
- string message = "0";
- try
- {
- string uri = serverPath + ftpDefaultFolder + "/UpdateList/";
- FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri));
- reqFTP.Credentials = new NetworkCredential(serverFTPName, serverFTPPassword);
- reqFTP.Method = "LIST";
- WebResponse response = reqFTP.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 + "UpdateListServer\\", msg, uri, serverFTPName, serverFTPPassword);
- if (rtn != "0")
- {
- message = rtn;
- break;
- }
- }
- }
- reader.Close();
- response.Close();
- }
- catch (Exception ex)
- {
- message = ex.Message;
- }
- return message;
- }
- public int CheckUpdate()
- {
- currentPath = AppDomain.CurrentDomain.BaseDirectory;
- serverPath = getXMLValue("prBrowser.exe.config", "UpdateUrl");
- int checkFlag = 0;
- string updateListMsg = getUpdateList();
- if (updateListMsg != "0")
- {
- return checkFlag;
- }
- DirectoryInfo dirRoot = new DirectoryInfo(currentPath + "UpdateList\\");
- string latestFileName = "";
- FileInfo[] files = dirRoot.GetFiles();
- foreach (FileInfo f in files)
- {
- latestFileName = f.Name;
- }
- DirectoryInfo dirRootServer = new DirectoryInfo(currentPath + "UpdateListServer\\");
- string latestFileNameServer = "";
- FileInfo[] files2 = dirRootServer.GetFiles();
- foreach (FileInfo f2 in files2)
- {
- latestFileNameServer = f2.Name;
- }
- if (latestFileNameServer != latestFileName)
- {
- checkFlag = 1;
- }
- delUpdateList();
- return checkFlag;
- }
- public void delUpdateList()
- {
- DirectoryInfo dirObj = new DirectoryInfo(currentPath + "UpdateListServer\\");
- dirObj.Delete(recursive: true);
- }
- private string getXMLValue(string path, string attr)
- {
- string xmlValue = "";
- XDocument document = XDocument.Load(path);
- XElement root = document.Root;
- XElement settingEle = root.Element("appSettings");
- IEnumerable<XElement> enumerable = settingEle.Elements();
- foreach (XElement item in enumerable)
- {
- if (item.Attribute("key").Value == attr)
- {
- xmlValue = item.Attribute("value").Value;
- }
- }
- return xmlValue;
- }
- public void ExecuteAsAdmin(string fileName)
- {
- Process proc = new Process();
- proc.StartInfo.FileName = fileName;
- proc.StartInfo.UseShellExecute = true;
- proc.StartInfo.Verb = "runas";
- proc.Start();
- }
- }
- }
|