|
@@ -8,14 +8,19 @@ namespace MOTINOVA_Motor_Factory_Set
|
|
|
{
|
|
|
public class ftp
|
|
|
{
|
|
|
- #region
|
|
|
-
|
|
|
string serverIP;
|
|
|
string serverPort;
|
|
|
string userId;
|
|
|
string passWord;
|
|
|
public bool IsNetConnected = false;
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 配置网络
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="serverIP"></param>
|
|
|
+ /// <param name="serverPort"></param>
|
|
|
+ /// <param name="userId"></param>
|
|
|
+ /// <param name="passWord"></param>
|
|
|
public void FtpOption(string serverIP, string serverPort, string userId, string passWord)
|
|
|
{
|
|
|
this.serverIP = serverIP;
|
|
@@ -24,6 +29,10 @@ namespace MOTINOVA_Motor_Factory_Set
|
|
|
this.passWord = passWord;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 检查网络
|
|
|
+ /// </summary>
|
|
|
+ /// <returns>bool</returns>
|
|
|
public bool CheckFtp()
|
|
|
{
|
|
|
try
|
|
@@ -32,143 +41,185 @@ namespace MOTINOVA_Motor_Factory_Set
|
|
|
FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(uri);
|
|
|
ftpRequest.Credentials = new NetworkCredential(userId, passWord);
|
|
|
ftpRequest.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;
|
|
|
- ftpRequest.Timeout = 2000;
|
|
|
FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
|
|
|
ftpResponse.Close();
|
|
|
IsNetConnected = true;
|
|
|
return true;
|
|
|
}
|
|
|
- catch (Exception ex)
|
|
|
+ catch (Exception)
|
|
|
{
|
|
|
IsNetConnected = false;
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- private FtpWebRequest OpenRequest(Uri uri, string ftpMethord)
|
|
|
+ /// <summary>
|
|
|
+ /// 文件上传
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="sourceFullPath">要上传文件全名</param>
|
|
|
+ /// <param name="targetPath">服务器端地址(temp)</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public bool UploadFile(string sourceFullPath, string targetPath)
|
|
|
{
|
|
|
- try
|
|
|
- {
|
|
|
- FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(uri);
|
|
|
- ftpRequest.Credentials = new NetworkCredential(userId, passWord);
|
|
|
- ftpRequest.Method = ftpMethord;
|
|
|
- ftpRequest.UseBinary = true;
|
|
|
- return ftpRequest;
|
|
|
- }
|
|
|
- catch (Exception ex)
|
|
|
+ FtpWebRequest reqFTP = null;
|
|
|
+ FileStream fs = null;
|
|
|
+ Stream strm = null;
|
|
|
+
|
|
|
+ FileInfo fileInf = new FileInfo(sourceFullPath);
|
|
|
+ string fileName = sourceFullPath.Substring(sourceFullPath.LastIndexOf("\\") + 1);
|
|
|
+
|
|
|
+ //远端存在该文件时,先删除
|
|
|
+ if (FileExist(targetPath, fileName))
|
|
|
{
|
|
|
- throw ex;
|
|
|
+ DeleteFile(targetPath + "/" + fileName);
|
|
|
}
|
|
|
- }
|
|
|
- private FtpWebResponse OpenResponse(Uri uri, string ftpMethord)
|
|
|
- {
|
|
|
+
|
|
|
+ Uri uri = new Uri("ftp://" + serverIP + ":" + serverPort + "/" + targetPath + "/" + Uri.EscapeDataString(fileName));
|
|
|
+ reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
|
|
|
+ reqFTP.Credentials = new NetworkCredential(userId, passWord);
|
|
|
+ reqFTP.KeepAlive = false;
|
|
|
+ reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
|
|
|
+ reqFTP.UseBinary = true;
|
|
|
+ reqFTP.UsePassive = false;
|
|
|
+ reqFTP.ContentLength = fileInf.Length;
|
|
|
+ byte[] buff = new byte[2048];
|
|
|
+ int contentLen = 0;
|
|
|
+ fs = fileInf.OpenRead();
|
|
|
try
|
|
|
{
|
|
|
- return this.OpenRequest(uri, ftpMethord).GetResponse() as FtpWebResponse;
|
|
|
+ strm = reqFTP.GetRequestStream();
|
|
|
+ while ((contentLen = fs.Read(buff, 0, buff.Length)) != 0)
|
|
|
+ {
|
|
|
+ strm.Write(buff, 0, contentLen);
|
|
|
+ }
|
|
|
+ fs.Close();
|
|
|
+ strm.Close();
|
|
|
+ return true;
|
|
|
}
|
|
|
- catch
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- throw new Exception("登录到Ftp服务器失败!");
|
|
|
+ fs.Close();
|
|
|
+ strm.Close();
|
|
|
+ throw new Exception("上传文件失败,原因: " + ex.Message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- #endregion
|
|
|
-
|
|
|
/// <summary>
|
|
|
- /// 下载(重命名)
|
|
|
+ /// 文件下载
|
|
|
/// </summary>
|
|
|
- /// <param name="sourceFullPath">下载文件全路径</param>
|
|
|
- /// <param name="targetFullPath">下载到本机全路径(包含文件名)</param>
|
|
|
- /// <returns></returns>
|
|
|
- public bool DownLoadReName(string sourceFullPath, string targetFullPath)
|
|
|
+ /// <param name="filePath">文件本地存放路径</param>
|
|
|
+ /// <param name="serverPath">远端文件路径</param>
|
|
|
+ /// <param name="fileName">文件名</param>
|
|
|
+ /// <returns>bool</returns>
|
|
|
+ public bool DownloadFile(string sourceFullPath, string targetFullPath)
|
|
|
{
|
|
|
+ FtpWebRequest ftpRequest = null;
|
|
|
+ FileStream outputStream = null;
|
|
|
+ FtpWebResponse response = null;
|
|
|
+ Stream ftpStream = null;
|
|
|
+
|
|
|
try
|
|
|
{
|
|
|
+ string fileName = sourceFullPath.Substring(sourceFullPath.LastIndexOf("/"));
|
|
|
+ outputStream = new FileStream(targetFullPath + fileName, FileMode.Create);
|
|
|
Uri uri = new Uri("ftp://" + serverIP + "/" + sourceFullPath);
|
|
|
- FtpWebResponse downloadResponse = OpenResponse(uri, WebRequestMethods.Ftp.DownloadFile);
|
|
|
- Stream responseStream = downloadResponse.GetResponseStream();
|
|
|
- FileStream fileStream = File.Create(targetFullPath);
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
- int bytesRead = 0;
|
|
|
- while (true)
|
|
|
+ ftpRequest = (FtpWebRequest)FtpWebRequest.Create(uri);
|
|
|
+ ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
|
|
|
+ ftpRequest.UseBinary = true;
|
|
|
+ ftpRequest.UsePassive = false;
|
|
|
+ ftpRequest.Proxy = null;
|
|
|
+ ftpRequest.Credentials = new NetworkCredential(userId, passWord);
|
|
|
+ response = (FtpWebResponse)ftpRequest.GetResponse();
|
|
|
+ ftpStream = response.GetResponseStream();
|
|
|
+ int readCount = 0;
|
|
|
+ byte[] buffer = new byte[2048];
|
|
|
+
|
|
|
+ while ((readCount = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
|
|
|
{
|
|
|
- bytesRead = responseStream.Read(buffer, 0, buffer.Length);
|
|
|
- if (bytesRead == 0)
|
|
|
- break;
|
|
|
- fileStream.Write(buffer, 0, bytesRead);
|
|
|
+ outputStream.Write(buffer, 0, readCount);
|
|
|
}
|
|
|
- fileStream.Close();
|
|
|
- responseStream.Close();
|
|
|
+ outputStream.Close();
|
|
|
+ ftpStream.Close();
|
|
|
+ response.Close();
|
|
|
return true;
|
|
|
}
|
|
|
- catch
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- throw new Exception("获取下载文件失败!");
|
|
|
+ outputStream.Close();
|
|
|
+ ftpStream.Close();
|
|
|
+ response.Close();
|
|
|
+ throw new Exception("下载文件失败,原因: " + ex.Message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 下载(按原名直接)
|
|
|
- /// </summary> www.jbxue.com
|
|
|
- /// <param name="loadFullPath">下载文件的全路径</param>
|
|
|
- /// <param name="targePath">下载到指定的地址(E:/)</param>
|
|
|
- /// <returns></returns>
|
|
|
- public bool DownLoadNotReName(string sourceFullPath, string targePath)
|
|
|
+ /// 删除文件
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="fileName"></param>
|
|
|
+ public void DeleteFile(string fileName)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- string fileName = sourceFullPath.Substring(sourceFullPath.LastIndexOf("/"));
|
|
|
- if (!Directory.Exists(targePath))//不存在就创建文件夹
|
|
|
- {
|
|
|
- Directory.CreateDirectory(targePath);
|
|
|
- }
|
|
|
- return DownLoadReName(sourceFullPath, targePath + fileName);
|
|
|
+ Uri uri = new Uri("ftp://" + serverIP + "/" + fileName);
|
|
|
+ FtpWebRequest reqFTP;
|
|
|
+ reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
|
|
|
+
|
|
|
+ reqFTP.Credentials = new NetworkCredential(userId, passWord);
|
|
|
+ reqFTP.KeepAlive = false;
|
|
|
+ reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
|
|
|
+ reqFTP.UsePassive = false;
|
|
|
+
|
|
|
+ string result = String.Empty;
|
|
|
+ FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
|
|
|
+ long size = response.ContentLength;
|
|
|
+ Stream datastream = response.GetResponseStream();
|
|
|
+ StreamReader sr = new StreamReader(datastream);
|
|
|
+ result = sr.ReadToEnd();
|
|
|
+ sr.Close();
|
|
|
+ datastream.Close();
|
|
|
+ response.Close();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- throw ex;
|
|
|
+ throw new Exception("文件删除失败!" + ex.Message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 文件上传
|
|
|
+ /// 删除文件夹
|
|
|
/// </summary>
|
|
|
- /// <param name="sourceFullPath">要上传文件的地址(G:/Ftp.txt)</param>
|
|
|
- /// <param name="targetPath">服务器端地址(temp)</param>
|
|
|
- /// <returns></returns>
|
|
|
- public bool UploadFile(string sourceFullPath, string targetPath)
|
|
|
+ /// <param name="folderName"></param>
|
|
|
+ public void RemoveDirectory(string folderName)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- string fileName = sourceFullPath.Substring(sourceFullPath.LastIndexOf("\\") + 1);
|
|
|
- //检查路径
|
|
|
- Uri uri = new Uri("ftp://" + serverIP + ":" + serverPort + "/" + targetPath + "/" + Uri.EscapeDataString(fileName));
|
|
|
- FtpWebRequest request = this.OpenRequest(uri, WebRequestMethods.Ftp.UploadFile);
|
|
|
- Stream requestStream = request.GetRequestStream();
|
|
|
- FileStream fileStream = new System.IO.FileStream(sourceFullPath, FileMode.Open);
|
|
|
- byte[] buffer = new byte[2048];
|
|
|
- int bytesRead;
|
|
|
- while (true)
|
|
|
- {
|
|
|
- bytesRead = fileStream.Read(buffer, 0, buffer.Length);
|
|
|
- if (bytesRead == 0)
|
|
|
- break;
|
|
|
- requestStream.Write(buffer, 0, bytesRead);
|
|
|
- }
|
|
|
- requestStream.Close();
|
|
|
- request.GetResponse();
|
|
|
- fileStream.Close();
|
|
|
- return true;
|
|
|
+ Uri uri = new Uri("ftp://" + serverIP + "/" + folderName);
|
|
|
+ FtpWebRequest reqFTP;
|
|
|
+ reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
|
|
|
+
|
|
|
+ reqFTP.Credentials = new NetworkCredential(userId, passWord);
|
|
|
+ reqFTP.KeepAlive = false;
|
|
|
+ reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
|
|
|
+ reqFTP.UsePassive = false;
|
|
|
+
|
|
|
+ string result = String.Empty;
|
|
|
+ FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
|
|
|
+ long size = response.ContentLength;
|
|
|
+ Stream datastream = response.GetResponseStream();
|
|
|
+ StreamReader sr = new StreamReader(datastream);
|
|
|
+ result = sr.ReadToEnd();
|
|
|
+ sr.Close();
|
|
|
+ datastream.Close();
|
|
|
+ response.Close();
|
|
|
}
|
|
|
- catch (Exception)
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- return false;
|
|
|
+ throw new Exception("文件夹删除失败!" + ex.Message);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 获取目录文件详细信息
|
|
|
+ /// 获取当前目录下明细(包含文件和文件夹)
|
|
|
/// </summary>
|
|
|
/// <param name="ServerPath">远端的地址</param>
|
|
|
/// <param name="WRMethods">
|
|
@@ -176,13 +227,21 @@ namespace MOTINOVA_Motor_Factory_Set
|
|
|
///
|
|
|
private string[] GetFileList(string ServerPath, string WRMethods)
|
|
|
{
|
|
|
- StringBuilder result = new StringBuilder();
|
|
|
+ StringBuilder result = null;
|
|
|
+ FtpWebRequest ftp = null;
|
|
|
+ WebResponse response = null;
|
|
|
+ StreamReader reader = null;
|
|
|
+
|
|
|
try
|
|
|
{
|
|
|
+ result = new StringBuilder();
|
|
|
Uri uri = new Uri("ftp://" + serverIP + "/" + ServerPath);
|
|
|
- FtpWebResponse downloadResponse = OpenResponse(uri, WRMethods);
|
|
|
- Stream responseStream = downloadResponse.GetResponseStream();
|
|
|
- StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.Default);
|
|
|
+ ftp = (FtpWebRequest)FtpWebRequest.Create(uri);
|
|
|
+ ftp.Credentials = new NetworkCredential(userId,passWord);
|
|
|
+ ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
|
|
|
+ ftp.UsePassive = false;
|
|
|
+ response = ftp.GetResponse();
|
|
|
+ reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
|
|
|
string line = reader.ReadLine();
|
|
|
while (line != null)
|
|
|
{
|
|
@@ -195,7 +254,7 @@ namespace MOTINOVA_Motor_Factory_Set
|
|
|
result.Remove(result.ToString().LastIndexOf("\n"), 1);
|
|
|
}
|
|
|
reader.Close();
|
|
|
- downloadResponse.Close();
|
|
|
+ response.Close();
|
|
|
return result.ToString().Split('\n');
|
|
|
}
|
|
|
catch (Exception ex)
|
|
@@ -241,7 +300,7 @@ namespace MOTINOVA_Motor_Factory_Set
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 获得文件明晰
|
|
|
+ /// 获取文件夹下明细
|
|
|
/// </summary>
|
|
|
/// <param name="path"></param>
|
|
|
/// <returns></returns>
|
|
@@ -306,17 +365,47 @@ namespace MOTINOVA_Motor_Factory_Set
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 判断目录下指定的文件是否存在
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="rootDir">远端目录</param>
|
|
|
+ /// <param name="fileName">指定文件</param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public bool FileExist(string rootDir, string fileName)
|
|
|
+ {
|
|
|
+ string[] fileList = GetFileNameList(rootDir);
|
|
|
+ if (fileList.Length > 0)
|
|
|
+ {
|
|
|
+ foreach (string file in fileList)
|
|
|
+ {
|
|
|
+ if (file.Substring(file.LastIndexOf("/") + 1) == fileName)
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 创建目录
|
|
|
/// </summary>
|
|
|
/// <param name="dirName"></param>
|
|
|
public void MakeDir(string dirName)
|
|
|
{
|
|
|
+ FtpWebRequest reqFTP;
|
|
|
try
|
|
|
{
|
|
|
Uri uri = new Uri("ftp://" + serverIP + "/" + dirName);
|
|
|
- FtpWebResponse downloadResponse = OpenResponse(uri, WebRequestMethods.Ftp.MakeDirectory);
|
|
|
- downloadResponse.Close();
|
|
|
+ reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
|
|
|
+ reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
|
|
|
+ reqFTP.UseBinary = true;
|
|
|
+ reqFTP.UsePassive = false;
|
|
|
+ reqFTP.Credentials = new NetworkCredential(userId,passWord);
|
|
|
+ FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
|
|
|
+ Stream ftpStream = response.GetResponseStream();
|
|
|
+ ftpStream.Close();
|
|
|
+ response.Close();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
@@ -324,7 +413,44 @@ namespace MOTINOVA_Motor_Factory_Set
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 重命名
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="currentFilename"></param>
|
|
|
+ /// <param name="newFilename"></param>
|
|
|
+ public void ReName(string currentFilename, string newFilename)
|
|
|
+ {
|
|
|
+ FtpWebRequest reqFTP;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Uri uri = new Uri("ftp://" + serverIP + "/" + currentFilename);
|
|
|
+ reqFTP = (FtpWebRequest)FtpWebRequest.Create(uri);
|
|
|
+ reqFTP.Method = WebRequestMethods.Ftp.Rename;
|
|
|
+ reqFTP.RenameTo = newFilename;
|
|
|
+ reqFTP.UseBinary = true;
|
|
|
+ reqFTP.UsePassive = false;
|
|
|
+ reqFTP.Credentials = new NetworkCredential(userId,passWord);
|
|
|
+ FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
|
|
|
+ Stream ftpStream = response.GetResponseStream();
|
|
|
+
|
|
|
+ ftpStream.Close();
|
|
|
+ response.Close();
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ throw new Exception("FtpHelper ReName Error --> " + ex.Message);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 移动文件
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="currentFilename"></param>
|
|
|
+ /// <param name="newFilename"></param>
|
|
|
+ public void MovieFile(string currentFilename, string newDirectory)
|
|
|
+ {
|
|
|
+ ReName(currentFilename, newDirectory);
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
}
|