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("")) { 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 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(); } } }