FTP的下载功能代码,一个小方法代码
Code
public void TestFTPDownload()
{
try
{
// go get the same code from edition 1
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create(
"ftp://ftp.oreilly.com/pub/examples/csharpckbk/CSharpCookbook.zip");
request.Credentials = new NetworkCredential("anonymous", "hilyard@oreilly.com");
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
Stream data = response.GetResponseStream();
string targetPath = "CSharpCookbook.zip";
if (File.Exists(targetPath))
File.Delete(targetPath);
byte[] byteBuffer = new byte[4096];
using (FileStream output = new FileStream(targetPath, FileMode.CreateNew))
{
int bytesRead = 0;
do
{
bytesRead = data.Read(byteBuffer, 0, byteBuffer.Length);
if (bytesRead > 0)
{
output.Write(byteBuffer, 0, bytesRead);
}
}
while (bytesRead > 0);
}
}
}
catch (WebException e)
{
Console.WriteLine(e);
}
}
public void TestFTPDownload()
{
try
{
// go get the same code from edition 1
FtpWebRequest request =
(FtpWebRequest)WebRequest.Create(
"ftp://ftp.oreilly.com/pub/examples/csharpckbk/CSharpCookbook.zip");
request.Credentials = new NetworkCredential("anonymous", "hilyard@oreilly.com");
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
Stream data = response.GetResponseStream();
string targetPath = "CSharpCookbook.zip";
if (File.Exists(targetPath))
File.Delete(targetPath);
byte[] byteBuffer = new byte[4096];
using (FileStream output = new FileStream(targetPath, FileMode.CreateNew))
{
int bytesRead = 0;
do
{
bytesRead = data.Read(byteBuffer, 0, byteBuffer.Length);
if (bytesRead > 0)
{
output.Write(byteBuffer, 0, bytesRead);
}
}
while (bytesRead > 0);
}
}
}
catch (WebException e)
{
Console.WriteLine(e);
}
}