|
|
@@ -1,10 +1,11 @@
|
|
|
using System;
|
|
|
+using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Net;
|
|
|
using System.Text;
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
-namespace MES接口
|
|
|
+namespace MES_Interface
|
|
|
{
|
|
|
public class ftpOperater
|
|
|
{
|
|
|
@@ -19,10 +20,10 @@ namespace MES接口
|
|
|
|
|
|
public ftpOperater()
|
|
|
{
|
|
|
- string[] FTPInf = "".Split('#');
|
|
|
- this.ftpServerIP = "ftp://richwellgroup.com.cn";
|
|
|
- this.ftpUser = "mesconfig";
|
|
|
- this.ftpPwd = "Administrator1@";
|
|
|
+ string[] FTPInf = Properties.Settings.Default.Properties["FTPAddress"].DefaultValue.ToString().Split('|');
|
|
|
+ this.ftpServerIP = FTPInf[0];
|
|
|
+ this.ftpUser = FTPInf[1];
|
|
|
+ this.ftpPwd = FTPInf[2];
|
|
|
}
|
|
|
|
|
|
public void UpLoadFile(string filepath, string filename)
|
|
|
@@ -67,6 +68,50 @@ namespace MES接口
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ //获取指定的文件的内容
|
|
|
+ public string GetFileContent(string filename)
|
|
|
+ {
|
|
|
+ FtpWebRequest reqFTP;
|
|
|
+ reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + filename));
|
|
|
+ reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
|
|
|
+ reqFTP.UseBinary = true;
|
|
|
+ reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
|
|
|
+
|
|
|
+ FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
|
|
|
+
|
|
|
+ Stream ftpStream = response.GetResponseStream();
|
|
|
+
|
|
|
+ int bufferSize = GetFileContentLength(filename);
|
|
|
+ byte[] buffer = new byte[bufferSize];
|
|
|
+ ftpStream.Read(buffer, 0, bufferSize);
|
|
|
+ ftpStream.Close();
|
|
|
+ return Encoding.Default.GetString(buffer);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //获取指定文件的内容的字节长度
|
|
|
+ public int GetFileContentLength(string filename)
|
|
|
+ {
|
|
|
+ FtpWebRequest reqFTP;
|
|
|
+ reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP + "/" + filename));
|
|
|
+ reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
|
|
|
+ reqFTP.UseBinary = true;
|
|
|
+ reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPwd);
|
|
|
+
|
|
|
+ FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
|
|
|
+
|
|
|
+ Stream ftpStream = response.GetResponseStream();
|
|
|
+
|
|
|
+ List<byte> buf = new List<byte>();
|
|
|
+ while (ftpStream.ReadByte() != -1)
|
|
|
+ {
|
|
|
+ buf.Add((byte)ftpStream.ReadByte());
|
|
|
+ }
|
|
|
+ ftpStream.Close();
|
|
|
+ return buf.ToArray().Length * 2;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 获取ftp服务器上的文件信息
|
|
|
/// </summary>
|