Uri u =
new
Uri(
"ftp://localhost/test.txt"
);
string
downFile =
"C:\\test.txt"
;
System.Net.FtpWebRequest ftpReq = (System.Net.FtpWebRequest)
System.Net.WebRequest.Create(u);
ftpReq.Credentials =
new
System.Net.NetworkCredential(
"username"
,
"password"
);
ftpReq.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
ftpReq.KeepAlive =
false
;
ftpReq.UseBinary =
false
;
ftpReq.UsePassive =
false
;
System.IO.FileStream fs;
if
(System.IO.File.Exists(downFile))
{
ftpReq.ContentOffset = (
new
System.IO.FileInfo(downFile)).Length;
fs =
new
System.IO.FileStream(
downFile, System.IO.FileMode.Append, System.IO.FileAccess.Write);
}
else
{
fs =
new
System.IO.FileStream(
downFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
}
System.Net.FtpWebResponse ftpRes =
(System.Net.FtpWebResponse)ftpReq.GetResponse();
System.IO.Stream resStrm = ftpRes.GetResponseStream();
byte
[] buffer =
new
byte
[1024];
while
(
true
)
{
int
readSize = resStrm.Read(buffer, 0, buffer.Length);
if
(readSize == 0)
break
;
fs.Write(buffer, 0, readSize);
}
fs.Close();
resStrm.Close();
Console.WriteLine(
"{0}: {1}"
, ftpRes.StatusCode, ftpRes.StatusDescription);
ftpRes.Close();