Skip to main content

make FTP client with FtpWebRequest in C sharp



Get Remote File List

private ArrayList GetRemoteFiles()
{
ArrayList al = new ArrayList();


FtpWebRequest fwr = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpIP));
fwr.Credentials = new NetworkCredential(ftpUser, ftpPass);
fwr.Method = WebRequestMethods.Ftp.ListDirectory;
fwr.UsePassive = false;

StreamReader sr = new StreamReader(fwr.GetResponse().GetResponseStream());
string str = sr.ReadLine();
while (str != null)
{
al.Add(str);
str = sr.ReadLine();
}

sr.Close();
sr = null;

fwr = null;

return al;
}



Download File to Local

private void DownloadFile(string remoteFile, string localFile)
{
try
{

string uri = @"ftp://" + ftpIP + @"/" + remoteFile;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.UsePassive = false;
request.Credentials = new NetworkCredential(ftpUser, ftpPass);
request.UseBinary = true;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Stream responseStream = response.GetResponseStream();

FileStream writeStream = new FileStream(localFile, FileMode.Create, FileAccess.Write);

ReadWriteStream(responseStream, writeStream);
response.Close();
}
catch (Exception e)
{
throw new Exception(e.Message);
}


}

Comments

Anonymous said…
very good!

Popular posts from this blog

Web synchronization option for merge replication

http://msdn2.microsoft.com/en-us/library/ms151319.aspx SQL Server 2005 Books Online Implementing Replication over the Internet Replicating data over the Internet allows remote, disconnected users to access data when they need it using a connection to the Internet. Replicate data over the Internet using: A Virtual Private Network (VPN). For more information, see Publishing Data over the Internet Using VPN . The Web synchronization option for merge replication. For more information, see Web Synchronization for Merge Replication . All types of Microsoft SQL Server replication can replicate data over a VPN, but you should consider Web synchronization if you are using merge replication.

Request Signatures in Blackberry

Request Signatures in Blackberry need copy sigtool.db, sigtool.csk to SDK/component/Bin folder for example V 4.2.1 ../eclipse\plugins\net.rim.eide.componentpack4.2.1_4.2.1.17\components\bin