// Get ftp file content
//public StreamReader FtpGet()
public bool FtpGet()
{
FileStream fileStream = null;
Stream responseStream = null;
if (this.InputValidation())
{
try
{
Uri UriFtpGet = new Uri("ftp://" + serverhost + ":" + serverport + dirpath + remotefile);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(UriFtpGet);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
responseStream = response.GetResponseStream();
returnmsg = response.BannerMessage;
returncode = response.StatusCode.ToString();
// Write into file
if (localfile == null)
localfile = Path.GetFileName(request.RequestUri.AbsolutePath);
fileStream = File.Create(localPath + @"\" + localfile);
byte[] buffer = new byte[1024];
int bytesRead;
while (true)
{
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
fileStream.Write(buffer, 0, bytesRead);
}
responseStream.Close();
fileStream.Close();
return true;
}
catch (Exception ex)
{
returnmsg = ex.ToString();
returncode = "0";
return false;
}
}
else
{
throw new ArgumentException(returnmsg);
}
}
沒有留言:
張貼留言