using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace WindowsFormsApplication1
{
class Class1
{
public static String pathName;
public static String getpathName()
{
return pathName;
}
public static void CombineBlocksIntoLibrary(string pathName)
{
string activeDir = @"c:\Temp";
//Create a new subfolder under the current active folder
string newPath = System.IO.Path.Combine(activeDir, "test");
// Create the subfolder
System.IO.Directory.CreateDirectory(newPath);
//File Download,Write Local Copy, from FTP Location
string localPath = @"C:\Temp\test\";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(pathName);
request.Credentials = new NetworkCredential("LOGIn", "PASSWORD");
request.Method = WebRequestMethods.Ftp.ListDirectory;
StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
request = null;
string fileName = streamReader.ReadLine();
while (fileName != null)
{
FtpWebRequest requestFileDownload = null;
FtpWebResponse responseFileDownload = null;
try
{
Console.WriteLine(fileName);
requestFileDownload = (FtpWebRequest)WebRequest.Create(pathName + fileName);
requestFileDownload.Credentials = new NetworkCredential("LOGIN", "PASSWORD");
requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();
Stream responseStream = responseFileDownload.GetResponseStream();
FileStream writeStream = new FileStream(localPath + fileName, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
Int64 iRunningByteTotal = 0;
iRunningByteTotal += bytesRead;
// calculate the progress out of a base "100"
double dIndex = (double)(iRunningByteTotal);
double dTotal = (double)buffer.Length;
double dProgressPercentage = (dIndex / dTotal);
int iProgressPercentage = (int)(dProgressPercentage * 100);
writeStream.Close();
Console.WriteLine("Download Done");
}
catch (System.Exception exceptionObj)
{
Console.WriteLine(exceptionObj.Message.ToString());
}
finally
{
requestFileDownload = null;
responseFileDownload = null;
}
fileName = streamReader.ReadLine();
}
request = null;
streamReader = null;
}
}
}