fileNames = GetFileNames(ftpURI, ftpUserID, ftpPassword);
for (int i = 0; i < fileNames.Count;i++ )
{
DownloadFtp(fileNames[i].ToString().Trim());
string path = @"D:\test\" + fileNames[i].ToString().Trim();
if (File.Exists(path.ToString()))
{
//File.Delete(path);
DeleteFtp(fileNames[i].ToString().Trim());
}
}
public static ArrayList GetFileNames(string ftpDir, string userName, string passWord)
{ var fileNames = new ArrayList();
var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(ftpDir));
reqFtp.UsePassive = false;
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(userName, passWord);
reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
var response = (FtpWebResponse)reqFtp.GetResponse();
var reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string fileName = reader.ReadLine();
while (fileName != null)
{ fileNames.Add(Path.GetFileName(fileName));
fileName = reader.ReadLine();
}
reader.Close();
response.Close();
//var fileNames = reader.ReadToEnd();
reader.Close();
response.Close();
return fileNames;
}
public static int DownloadFtp(string filename)
{
FtpWebRequest reqFTP;
string serverIP;
string userName;
string password;
string url;
try
{
serverIP = "ftp://ftp1.qsicn.com//test"; ;
userName = "user14"; ;
password = "f17gg59@";
url = serverIP + "//" + Path.GetFileName(filename);
string path = @"D:\test\" + filename;
//FileStream outputStream = new FileStream(filename, FileMode.Create);
FileStream outputStream = new FileStream(path, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(userName, password);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
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 0;
}
catch (Exception ex)
{
SystemLog.logger(ex.InnerException.Message);
return -2;
}
}
public void DeleteFtp(string fileName)
{
try
{
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI +"//" +fileName));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
reqFTP.KeepAlive = false;
string result = String.Empty;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
long size = response.ContentLength;
Stream datastream = response.GetResponseStream();
StreamReader sr = new StreamReader(datastream);
result = sr.ReadToEnd();
sr.Close();
datastream.Close();
response.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
for (int i = 0; i < fileNames.Count;i++ )
{
DownloadFtp(fileNames[i].ToString().Trim());
string path = @"D:\test\" + fileNames[i].ToString().Trim();
if (File.Exists(path.ToString()))
{
//File.Delete(path);
DeleteFtp(fileNames[i].ToString().Trim());
}
}
public static ArrayList GetFileNames(string ftpDir, string userName, string passWord)
{ var fileNames = new ArrayList();
var reqFtp = (FtpWebRequest)WebRequest.Create(new Uri(ftpDir));
reqFtp.UsePassive = false;
reqFtp.UseBinary = true;
reqFtp.Credentials = new NetworkCredential(userName, passWord);
reqFtp.Method = WebRequestMethods.Ftp.ListDirectory;
var response = (FtpWebResponse)reqFtp.GetResponse();
var reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string fileName = reader.ReadLine();
while (fileName != null)
{ fileNames.Add(Path.GetFileName(fileName));
fileName = reader.ReadLine();
}
reader.Close();
response.Close();
//var fileNames = reader.ReadToEnd();
reader.Close();
response.Close();
return fileNames;
}
public static int DownloadFtp(string filename)
{
FtpWebRequest reqFTP;
string serverIP;
string userName;
string password;
string url;
try
{
serverIP = "ftp://ftp1.qsicn.com//test"; ;
userName = "user14"; ;
password = "f17gg59@";
url = serverIP + "//" + Path.GetFileName(filename);
string path = @"D:\test\" + filename;
//FileStream outputStream = new FileStream(filename, FileMode.Create);
FileStream outputStream = new FileStream(path, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(userName, password);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
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 0;
}
catch (Exception ex)
{
SystemLog.logger(ex.InnerException.Message);
return -2;
}
}
public void DeleteFtp(string fileName)
{
try
{
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI +"//" +fileName));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
reqFTP.KeepAlive = false;
string result = String.Empty;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
long size = response.ContentLength;
Stream datastream = response.GetResponseStream();
StreamReader sr = new StreamReader(datastream);
result = sr.ReadToEnd();
sr.Close();
datastream.Close();
response.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}