public class FtpManager
{
/// <summary>
/// 主機名
/// </summary>
string ftpServerIP;
/// <summary>
/// 登錄名
/// </summary>
string ftpUserID;
/// <summary>
/// 登錄密碼
/// </summary>
string ftpPassword;
FtpWebRequest ftpRequest;
public FtpManager(string ftpServerIP, string ftpUserID, string ftpPassword)
{
this.ftpServerIP = ftpServerIP;
this.ftpUserID = ftpUserID;
this.ftpPassword = ftpPassword;
}
#region 連接ftp
/// <summary>
/// 連接ftp
/// </summary>
/// <param name="path"></param>
public void Connect(String path)
{
// 根據uri創建FtpWebRequest對象
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "//" + path));
//設置文件傳輸類型
ftpRequest.UseBinary = true;
//設置登錄FTP帳號和密碼
ftpRequest.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
}
#endregion
#region FTP獲取文件列表
/// <summary>
/// FTP獲取文件列表
/// </summary>
/// <param name="ftpServerIP"></param>
/// <param name="ftpUserID"></param>
/// <param name="ftpPassword"></param>
/// <returns></returns>
public string[] FTPGetFileList(string filePath)
{
//響應結果
StringBuilder result = new StringBuilder();
//FTP響應
WebResponse ftpResponse = null;
//FTP響應流
StreamReader ftpResponsStream = null;
try
{
Connect(filePath);
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
//生成FTP響應
ftpResponse = ftpRequest.GetResponse();
//FTP響應流
ftpResponsStream = new StreamReader(ftpResponse.GetResponseStream());
string line = ftpResponsStream.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = ftpResponsStream.ReadLine();
}
//去掉結果列表中最後一個換行
result.Remove(result.ToString().LastIndexOf('\n'), 1);
//返回結果
return result.ToString().Split('\n');
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return (null);
}
finally
{
if (ftpResponsStream != null)
{
ftpResponsStream.Close();
}
if (ftpResponse != null)
{
ftpResponse.Close();
}
}
}
#endregion
#region FTP下載文件
/// <summary>
/// FTP下載文件
/// </summary>
/// <param name="ftpServerIP">FTP服務器IP</param>
/// <param name="ftpUserID">FTP登錄帳號</param>
/// <param name="ftpPassword">FTP登錄密碼</param>
/// <param name="saveFilePath">保存文件路徑</param>
/// <param name="saveFileName">保存文件名</param>
/// <param name="downloadFileName">下載文件名</param>
public void FTPDownloadFile(string path, string saveFileName, string downloadFileName)
{
//定義FTP響應對象
FtpWebResponse ftpResponse = null;
//存儲流
FileStream saveStream = null;
//FTP數據流
Stream ftpStream = null;
try
{
Connect(downloadFileName);
//生成下載文件
saveStream = new FileStream(path + "\\" + saveFileName, FileMode.Create);
//設置下載文件方法
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
//生成FTP響應對象
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
//獲取FTP響應流對象
ftpStream = ftpResponse.GetResponseStream();
//響應數據長度
long cl = ftpResponse.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
//接收FTP文件流
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
saveStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (ftpStream != null)
{
ftpStream.Close();
}
if (saveStream != null)
{
saveStream.Close();
}
if (ftpResponse != null)
{
ftpResponse.Close();
}
}
}
#endregion
#region 從ftp服務器上獲得文件列表
/// <summary>
/// 從ftp服務器上獲得文件列表
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string[] GetFileList(string path)
{
return FTPGetFileList(path);
}
#endregion
#region 文件存在檢查
/// <summary>
/// 文件存在檢查
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public bool fileCheckExist(string fileName)
{
bool success = false;
FtpWebResponse response = null;
StreamReader reader = null;
try
{
Connect(fileName);
response = (FtpWebResponse)ftpRequest.GetResponse();
reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
if (line != null)
{
success = true;
}
}
catch (Exception)
{
success = false;
}
finally
{
if (reader != null)
{
reader.Close();
}
if (response != null)
{
response.Close();
}
}
return success;
}
#endregion
#region 獲取文件大小
/// <summary>
/// 獲取文件大小
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public long GetFileSize(string filepath)
{
long fileSize = 0;
try
{
Connect(filepath);//連接
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
fileSize = response.ContentLength;
response.Close();
}
catch (Exception ex)
{
throw ex;
}
return fileSize;
}
#endregion
}
調用方法:
FtpManager client = new FtpManager("192.168.1.101", "wangjun01", "qwer1234");
string parentUrl = "B";
client.FTPDownloadFile("D:", fileList[i], parentUrl);
//string[] fileList = client.FTPGetFileList(parentUrl);
//for (int i = 0; i < fileList.Length; i++)
//{
// client.FTPDownloadFile("D:", fileList[i], parentUrl);
//}