FTPHelper.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Net;
  6. using System.Xml.Linq;
  7. namespace prBrowser.Weblogic
  8. {
  9. public class FTPHelper
  10. {
  11. public string currentPath = "";
  12. public string serverPath = "";
  13. public string serverFTPName = "RKFTP";
  14. public string serverFTPPassword = "pryk@2020";
  15. public string ftpDefaultFolder = "browser";
  16. public string DownLoad(string filePath, string fileName, string ftpPath, string ftpUserName, string ftpPassWord)
  17. {
  18. try
  19. {
  20. if (!Directory.Exists(filePath))
  21. {
  22. Directory.CreateDirectory(filePath);
  23. }
  24. FileStream outputStream = new FileStream(filePath + fileName, FileMode.Create);
  25. FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath + fileName));
  26. reqFTP.Method = "RETR";
  27. reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPassWord);
  28. reqFTP.UseBinary = true;
  29. FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
  30. Stream ftpStream = response.GetResponseStream();
  31. long cl = response.ContentLength;
  32. int bufferSize = 2048;
  33. byte[] buffer = new byte[bufferSize];
  34. for (int readCount = ftpStream.Read(buffer, 0, bufferSize); readCount > 0; readCount = ftpStream.Read(buffer, 0, bufferSize))
  35. {
  36. outputStream.Write(buffer, 0, readCount);
  37. }
  38. ftpStream.Close();
  39. outputStream.Close();
  40. response.Close();
  41. reqFTP = null;
  42. return "0";
  43. }
  44. catch (Exception ex)
  45. {
  46. return ex.Message;
  47. }
  48. }
  49. public string getUpdateList()
  50. {
  51. string message = "0";
  52. try
  53. {
  54. string uri = serverPath + ftpDefaultFolder + "/UpdateList/";
  55. FtpWebRequest reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri));
  56. reqFTP.Credentials = new NetworkCredential(serverFTPName, serverFTPPassword);
  57. reqFTP.Method = "LIST";
  58. WebResponse response = reqFTP.GetResponse();
  59. StreamReader reader = new StreamReader(response.GetResponseStream());
  60. for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
  61. {
  62. if (!line.Contains("<DIR>"))
  63. {
  64. string msg = line.Substring(39).Trim();
  65. string rtn = DownLoad(currentPath + "UpdateListServer\\", msg, uri, serverFTPName, serverFTPPassword);
  66. if (rtn != "0")
  67. {
  68. message = rtn;
  69. break;
  70. }
  71. }
  72. }
  73. reader.Close();
  74. response.Close();
  75. }
  76. catch (Exception ex)
  77. {
  78. message = ex.Message;
  79. }
  80. return message;
  81. }
  82. public int CheckUpdate()
  83. {
  84. currentPath = AppDomain.CurrentDomain.BaseDirectory;
  85. serverPath = getXMLValue("prBrowser.exe.config", "UpdateUrl");
  86. int checkFlag = 0;
  87. string updateListMsg = getUpdateList();
  88. if (updateListMsg != "0")
  89. {
  90. return checkFlag;
  91. }
  92. DirectoryInfo dirRoot = new DirectoryInfo(currentPath + "UpdateList\\");
  93. string latestFileName = "";
  94. FileInfo[] files = dirRoot.GetFiles();
  95. foreach (FileInfo f in files)
  96. {
  97. latestFileName = f.Name;
  98. }
  99. DirectoryInfo dirRootServer = new DirectoryInfo(currentPath + "UpdateListServer\\");
  100. string latestFileNameServer = "";
  101. FileInfo[] files2 = dirRootServer.GetFiles();
  102. foreach (FileInfo f2 in files2)
  103. {
  104. latestFileNameServer = f2.Name;
  105. }
  106. if (latestFileNameServer != latestFileName)
  107. {
  108. checkFlag = 1;
  109. }
  110. delUpdateList();
  111. return checkFlag;
  112. }
  113. public void delUpdateList()
  114. {
  115. DirectoryInfo dirObj = new DirectoryInfo(currentPath + "UpdateListServer\\");
  116. dirObj.Delete(recursive: true);
  117. }
  118. private string getXMLValue(string path, string attr)
  119. {
  120. string xmlValue = "";
  121. XDocument document = XDocument.Load(path);
  122. XElement root = document.Root;
  123. XElement settingEle = root.Element("appSettings");
  124. IEnumerable<XElement> enumerable = settingEle.Elements();
  125. foreach (XElement item in enumerable)
  126. {
  127. if (item.Attribute("key").Value == attr)
  128. {
  129. xmlValue = item.Attribute("value").Value;
  130. }
  131. }
  132. return xmlValue;
  133. }
  134. public void ExecuteAsAdmin(string fileName)
  135. {
  136. Process proc = new Process();
  137. proc.StartInfo.FileName = fileName;
  138. proc.StartInfo.UseShellExecute = true;
  139. proc.StartInfo.Verb = "runas";
  140. proc.Start();
  141. }
  142. }
  143. }