0

I am trying to build a list of all files on a ftp server.

// Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(remote);
            request.Credentials = new NetworkCredential(userName, passWord);
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            StreamReader streamReader = new StreamReader(response.GetResponseStream());
            List<string> files = new List<string>();

            // Build List of all files
            string line = streamReader.ReadLine();
            while (!string.IsNullOrWhiteSpace(line))
            {
                files.Add(line);
                line = streamReader.ReadLine();
            }
            streamReader.Close();

Now, the problem is, whe i use this list with the broken/incomplete file to download all the files with a Webclient, the code breaks when trying to download the incomplete file...

The remote server returned an error: (550) File unavailable (e.g., file not found, no access)

How can i skip these files when building the list?

Nomistake
  • 893
  • 2
  • 17
  • 32
  • 1
    Not sure on whether you can "skip" bad files, however you can try the code here http://stackoverflow.com/questions/17471745/ftp-getresponse-error-550-file-unavailable to get a more exact error message. From there you can trouble shoot with the FTP sites administrator to clear up the issue. – Bearcat9425 Nov 24 '15 at 13:57
  • @Bearcat9425 Thank you. The files ar created in the ftp folder automaticly, but i want to skip the files that are not fully copied/created – Nomistake Nov 24 '15 at 13:59
  • I don't think there is a way to do this, at least wise I am not aware of. I hope someone can say I am wrong and provide a solution but I don't think there is an out of the box one for this. I would work with your FTP administrator to find out why an automated process is generating incomplete/bad files. I would start there and work my way back. – Bearcat9425 Nov 24 '15 at 14:11
  • 1
    What happens if you try checking the filesize (WebRequestMethods.Ftp.GetFileSize) as you create the list - if it returns 0 or throws an exception - you could use that to limit the list of files. – PaulF Nov 24 '15 at 14:17
  • See also [How to detect that a file is being uploaded over FTP](http://stackoverflow.com/q/29238153/850848). – Martin Prikryl Nov 24 '15 at 14:27
  • Doing some modifications and testing with GetFileSize... I'll get back on this... – Nomistake Nov 24 '15 at 15:22

1 Answers1

0

I the catch of the exeption i just added a "ignore" when the exeptions was "The remote server returned an error: (550) File unavailable (e.g., file not found, no access)" This way i dont get the error and the file gets downloaded just fine when its complete on the ftp server.

Nomistake
  • 893
  • 2
  • 17
  • 32