Saturday, November 20, 2010

Learn to get full detail using ftp connection


ftp connection

Developers are always thinking that how to download a file from the server. Here are the code to download and upload the file.

Before downloading the file we must have the name of that file for this we should have list of file. Here is the code to get the list of the file from the server.

public string[] GetFileList()

{

string[] downloadFiles;

StringBuilder result = new StringBuilder();

FtpWebRequest reqFTP;

try

{

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));

reqFTP.UseBinary = true;

reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

WebResponse response = reqFTP.GetResponse();

StreamReader reader = new StreamReader(response.GetResponseStream());

string line = reader.ReadLine();

while (line != null)

{

result.Append(line);

result.Append("\n");

line = reader.ReadLine();

}

// to remove the trailing '\n'

result.Remove(result.ToString().LastIndexOf('\n'), 1);

reader.Close();

response.Close();

return result.ToString().Split('\n');

}

catch (Exception ex)

{

System.Windows.Forms.MessageBox.Show(ex.Message);

downloadFiles = null;

return downloadFiles;

}

}


download code here


Need more visitors?
Add your website to our directory to get more visitors.

No comments: