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

user Emulator to debug Windows Mobile 6 in Vista

open Windows Mobile Device center Mobile Device Setting -> Connection Setting -> Allow connections to one of the following: Change to DMA in vs 2008, Go to Tools -> Device Emulator Manager select one from list, for example: windows mobile 6 professional emulator right on to cradle waiting for popup to establish the relationship (be sure get internet connection from Emulator)

android textsize sp, px, dp

android:textSize Since:  API Level Size of the text. Recommended dimension type for text is "sp" for scaled-pixels (example: 15sp). Must be a dimension value, which is a floating point number appended with a unit such as " 14.5sp ". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters). This may also be a reference to a resource (in the form " @[ package :] type : name ") or theme attribute (in the form " ?[ package :][ type :] name ") containing a value of this type. This corresponds to the global attribute resource symbol  textSize .

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