try
{
string FileNameToDownload = "filename.txt"; //檔名
string DownloadFilePath = @"D://"; //存檔路徑
FileStream outputStream = new FileStream(DownloadFilePath + "\\" + FileNameToDownload, FileMode.Create);
string FTPip = "192.168.100.100"; //FTP Server IP
FtpWebRequest repFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://"+FTPip+"/path/"+FileNameToDownload));
repFTP.Method = WebRequestMethods.Ftp.DownloadFile;
repFTP.UseBinary = true;
repFTP.Proxy = null;
string ftpUserID = "user";
string ftpPassword = "password";
repFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)repFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int buffersize = 2048;
int readCount;
byte[] buffer = new byte[buffersize];
readCount = ftpStream.Read(buffer, 0, buffersize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, buffersize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
MessageBox.Show("Complete!");
}
catch (WebException ex)
{
String status = ((FtpWebResponse)ex.Response).StatusDescription;
MessageBox.Show(status);
}
沒有留言:
張貼留言