Code Heaven For C# Developer

Friday, July 19, 2013

Directory operations on ftp in c#


First of all i am going to write down the code for checking the existance of the directory on the given FTP server.
public static bool FtpDirectoryExists(string directoryPath, string ftpUser, string ftpPassword)
 {
 bool IsExists = true;
 try
 { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(directoryPath); request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
 }
 catch (WebException ex)
 { IsExists = false;
 } return IsExists;
 }

 public static void CreateDirectory(string directoryPath, string ftpUser, string ftpPassword)
{
 try
{WebRequest request = WebRequest.Create(directoryPath);
 request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
 request.Method = WebRequestMethods.Ftp.MakeDirectory;
 using (var resp = (FtpWebResponse)request.GetResponse())
 { Console.WriteLine(resp.StatusCode); }
 }
 catch (Exception ex)
{ Console.WriteLine(ex.Message); }
 }

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]



<< Home