public void Download(string ftpPath, string filePath, string fileName)
{
filePath = "D:\\TestFTP\\";
fileName = "test.jpg";
ftpPath = "ftp://192.168.1.5/SAW1422.jpg";
string sRet = "下載成功!";
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(filePath + fileName, FileMode.Create);
// 根據uri創建FtpWebRequest對象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));
// 指定執行什麼命令
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
// 指定數據傳輸類型
reqFTP.UseBinary = true;
reqFTP.UsePassive = false;
// ftp用戶名和密碼
//reqFTP.Credentials = new NetworkCredential();
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
// 把下載的文件寫入流
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
// 緩衝大小設置爲2kb
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
// 每次讀文件流的2kb
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
// 把內容從文件流寫入
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
//關閉兩個流和ftp連接
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
sRet = ex.Message;
}
}