Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am unable to find any solution for FTP validation in online. Please let me know is there any easy approach to find out the proper ftp validation in C#.
Posted

1 solution

Hi Srinivas,
Hope the below method is useful. :)

public static bool IsValidFTPPath(string directory,string ftpUser, string ftpPassword)
{

try
{
try
{
FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(directory));
requestDir.Method = WebRequestMethods.Ftp.MakeDirectory;
requestDir.Credentials = new NetworkCredential(ftpUser, ftpPassword);
requestDir.UsePassive = true;
requestDir.UseBinary = true;
requestDir.KeepAlive = false;
FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();

}
catch (Exception)
{

}
try
{
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(directory));
reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPassword);
FtpWebResponse responseRemDir = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStreamRemoveDir = responseRemDir.GetResponseStream();
ftpStreamRemoveDir.Close();
responseRemDir.Close();
}
catch (Exception)
{
throw;
}
return true;
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
response.Close();
return true;
}
else
{
response.Close();
return false;
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900