public bool Download(string
filePath, string fileName)
{
FtpWebRequest
reqFTP;
try
{
string
ftpServerIP =”ftpservernane”;
//filePath
= <>,
//fileName
= <>
FileStream
outputStream = new FileStream(filePath
+ "\\" + fileName, FileMode.Create);
string
uri = "ftp://" + ftpServerIP + "/" + fileName;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new
Uri("ftp://"
+ ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential("", "");
FtpWebResponse
response = (FtpWebResponse)reqFTP.GetResponse();
Stream
ftpStream = response.GetResponseStream();
long
cl = response.ContentLength;
int
bufferSize = 2048;
int
readCount;
byte[]
buffer = new byte[bufferSize];
if
(ftpFileExist(uri))
{
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();
return
true;
}
else
{ return
false; }
}
catch (Exception ex)
{
return
false; //MessageBox.Show(ex.Message);
}
}
public bool ftpFileExist(string
fileName)
{
WebClient
wc = new WebClient();
try
{
wc.Credentials = new NetworkCredential("", "");
byte[]
fData = wc.DownloadData(fileName);
if
(fData.Length > -1)
{
return
true;
}
else
{
return
false;
}
}
catch (Exception)
{
// Debug
here?
}
return false;
}
No comments :
Post a Comment