Sunday, 18 March 2018

How to check file exists in FTP server in C#

Description : In this post when you try to delete file from FTp but get error 550, File Unavailable to solve this first check file is exists or not in FTP.
here create one method for check file in FTP

private bool FileCheckInFTP(string fileName)   
{   
    var request = (FtpWebRequest)WebRequest.Create("ftp://www.server.com/" + fileName);   
    request.Credentials = new NetworkCredential("username", "password");   
    request.Method = WebRequestMethods.Ftp.GetFileSize;   
       
    try   
    {   
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();   
        return true;   
    }   
    catch (WebException ex)   
    {   
        FtpWebResponse response = (FtpWebResponse)ex.Response;   
        if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)   
            return false;   
    }   
    return false;   
}

- if the response get from FTP like "ActionNotTakenFileUnavailable" it means file is unavailble.

No comments:

Post a Comment