0

I create a windows form to download files from ftp from particular folder.

The user put the ftp details with username and password and folder name from which file will be download all the files. This will set by user one time and the all file from ftp describe folder will download everyday.
Example on FTP Folder Name is MyFolder where a.docx, b.docx etc it will download a.docx, b.docx everyday not other folder data need to download.

For download and list of file I use below function. Can you please tell me what I am doing mistake or how can I do this .

 private void downloadFileFromFTP()
 {
    try
    {
        string[] files = GetFileList();
        foreach (string file in files)
        {
            Download(file);
        }
    }
    catch (Exception ex)
    {
    }
}

For Get The List of file

public string[] GetFileList()
{
    string[] downloadFiles;
    StringBuilder result = new StringBuilder();
    WebResponse response = null;
    StreamReader reader = null;
    try
    {
        FtpWebRequest reqFTP;
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri( "ftp://" + txtftpAddress.Text + "/")); //txtFtpAddress.Text + "/" + txtFTPFolderName + "/" + file
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential("UserNm", "passwd");
        reqFTP.Method = WebRequestMethods.Ftp .ListDirectory;
        reqFTP.Proxy = null;
        reqFTP.KeepAlive = false;
        reqFTP.UsePassive = false;
        response = reqFTP.GetResponse();
        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);
        return result.ToString().Split('\n');
    }
    catch (Exception ex)
    {
        if (reader != null)
        {
            reader.Close();
        }
        if (response != null)
        {
            response.Close();
        }
        downloadFiles = null;
        return downloadFiles;
    }
}

download the file form the folder

private void Download(string file)
{                       
    try
    {                             
        string uri = "ftp://" + txtFtpAddress.Text.Trim() + "/" + "txtlodername.Text" + "/" + file;

        Uri serverUri = new Uri(uri);
        if (serverUri.Scheme != Uri.UriSchemeFtp)
        {
            return;
        }       
        FtpWebRequest reqFTP;                
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(txtFtpAddress.Text + "/" + txtFTPFolderName + "/" + file));                                
        reqFTP.Credentials = new NetworkCredential("UserName", "mypass");                
        reqFTP.KeepAlive = false;                
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;                                
        reqFTP.UseBinary = true;
        reqFTP.Proxy = null;                 
        reqFTP.UsePassive = false;
        FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
        Stream responseStream = response.GetResponseStream();
        FileStream writeStream = new FileStream("D\\Temp"  + file, 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);
        }                
        writeStream.Close();
        response.Close(); 
    }
    catch (WebException wEx)
    {
        MessageBox.Show(wEx.Message, "Download Error");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Download Error");
    }
}
rene
  • 41,474
  • 78
  • 114
  • 152
A.Goutam
  • 3,422
  • 9
  • 42
  • 90

1 Answers1

2

I think 3 lines of your Download method have to be corrected as follows:

1.

string uri = "ftp://" + txtFtpAddress.Text.Trim() + "/" + "txtlodername.Text" + "/" + file;

should be:

string uri = "ftp://" + txtFtpAddress.Text.Trim() + "/" + txtFTPFolderName.Text.Trim() + "/" + file;


2.

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(txtFtpAddress.Text + "/" + txtFTPFolderName + "/" + file));

should be:

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));


3.

FileStream writeStream = new FileStream("D\\Temp"  + file, FileMode.Create);  

should be:

FileStream writeStream = new FileStream("D:\\Temp\\"  + file, FileMode.Create);  
jhmt
  • 1,401
  • 1
  • 11
  • 15
  • Thanks for your quick response i am doing these changes my problem is that how to first list the all file in the given directory and second i want to download only file of txtFTPFolderName in ftp that is describe by user – A.Goutam Oct 31 '15 at 09:43
  • i want to download the file of particular folder . So first how can i read all file from particular folder `example MyFolder contains a.docx ` i need to download only `a.docx` from ftp not other file – A.Goutam Oct 31 '15 at 09:50
  • @A.Goutam Your `GetFileList` method looks good but `Uri` has to be `new Uri( "ftp://" + txtftpAddress.Text + "/" + "MyFolder" + "/")`. Then let users choose the listed files. `ListBox` control should be the best option to choose. – jhmt Oct 31 '15 at 09:53
  • @jtmt i tried this my GetFileList return files list i want that i can get list of file that is in my `MyFolder` directory . Current code return `The remote server returned an error: (550) File unavailable (e.g., file not found, no access).` – A.Goutam Oct 31 '15 at 10:03
  • @jtmt the GetFileList return the List of all file on my ftp . Here is going puzzle – A.Goutam Oct 31 '15 at 10:04
  • @A.Goutam As for the error, this question (http://stackoverflow.com/questions/17471745/ftp-getresponse-error-550-file-unavailable) is helpful for you.You might want to change the permission of `MyFolder`. – jhmt Oct 31 '15 at 10:13