Curl Command With FTP And SFTP In C#

What is Curl

Curl is a command line tool and library. It is open source and run on various OS. Basically it is used to transferring data from a server to another server. It supports many types of Protocol like FTP, SFTP, POP3 SMB, SMTP, SMTPS, DICT, FILE, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3S, RTMP, RTSP, SCP. Curl also supports SSL certificate. In today's world, most of the equipment uses curl. Latest version of curl is 7.44.0 that released on  August 12, 2015.

Download

You can download curl as per your Operating system compatible. It can be x86 or x64 as per your requirement. You can download it from here.

There are various protocols where we can use curl. But today I will explain you how to work with FTP and SFTP using curl.

Curl with FTP

FTP, means “File Transfer Protocol, in short we use FTP, is a standard network protocol that is used to transfer the data from one host to another host over a TCP based network like Internet.

You can use the following command with FTP.

Login usign curl on FTP

curl -P - --insecure "ftp://82.45.34.23:21/" --user "testuser:testpassword"

Upload using curl on FTP

curl -p - --insecure "ftp://82.45.34.23:21/CurlPutTest/" --user "testuser:testpassword" -T "C:\test\testfile.xml" --ftp-create-dirs

Download using curl on FTP

curl -p - --insecure "ftp://82.45.34.23:21/CurlPutTest/testfile.xml" --user "testuser:testpassword" -o "C:\test\testfile.xml" --ftp-create-dirs

Rename using curl on FTP

curl -p - --insecure "ftp://82.45.34.23:21/CurlPutTest/" --user "testuser:testpassword" -Q "-RNFR /CurlPutTest/testfile.xml" -Q "-RNTO /CurlPutTest/testfile.xml.tmp" --ftp-create-dirs

Delete using curl on FTP

curl -p - --insecure "ftp://82.45.34.23:21/CurlPutTest/testfile.xml" --user "testuser:testpassword" -Q "–DELE /CurlPutTest/testfile.xml" --ftp-create-dirs

Make directory using curl on FTP

Curl -p - --insecure "ftp://82.45.34.23:21/CurlPutTest/test" --user "testuser:testpassword" -Q "-MKD /CurlPutTest/test" --ftp-create-dirs

Remove directory using curl on FTP

Curl -p - --insecure "ftp://82.45.34.23:21/CurlPutTest/test" --user "testuser:testpassword" -Q "-RMD /CurlPutTest/test" --ftp-create-dirs

Curl with SFTP

SFTP, means “SSH File Transfer Protocol" or “Secure File Transfer Protocol”, in short we use SFTP, is a standard network protocol that is used to transfer the data from one host to another host over a secure connection.

You can use the following command with SFTP.

Login using curl on SFTP

curl -k "sftp://83.46.38.23:22/" --user "testuser:testpassword"

Upload using curl on SFTP

curl -k "sftp://83.46.38.23:22/CurlPutTest/" --user "testuser:testpassword" -T "C:\test\testfile.xml" --ftp-create-dirs

Download using curl on SFTP

curl -k "sftp://83.46.38.23:22/CurlPutTest/testfile.xml" --user "testuser:testpassword" -o "C:\test\testfile.xml" --ftp-create-dirs

Rename using curl on SFTP

curl -k "sftp://83.46.38.23:22/CurlPutTest/" --user "testuser:testpassword" -Q "-RENAME
‘/CurlPutTest/testfile.xml’ ‘/CurlPutTest/testfile.xml.tmp’" --ftp-create-dirs


Delete using curl on SFTP

curl -k "sftp://83.46.38.23:22/CurlPutTest/ " --user "testuser:testpassword" -Q "–RM /CurlPutTest/testfile.xml" --ftp-create-dirs

Make directory using curl on SFTP

curl -k "sftp://83.46.38.23:22/CurlPutTest/test " --user "testuser:testpassword" -Q "–MKDIR /CurlPutTest/Test" --ftp-create-dirs

Remove directory using curl on SFTP

curl -k "sftp://83.46.38.23:22/CurlPutTest/test " --user "testuser:testpassword" -Q "–RMDIR /CurlPutTest/Test" --ftp-create-dirs

Execute Curl By C# Code

  1. public bool ExecuteCommand(string curlExePath, string commandLineArguments, bool isReturn = true)  
  2. {  
  3.     bool result = true;  
  4.     try  
  5.     {  
  6.         commandProcess = new Process();  
  7.         commandProcess.StartInfo.UseShellExecute = false;  
  8.         commandProcess.StartInfo.FileName = curlExePath;  
  9.         commandProcess.StartInfo.Arguments = commandLineArguments;  
  10.         commandProcess.StartInfo.CreateNoWindow = true;  
  11.         commandProcess.StartInfo.RedirectStandardInput = true;  
  12.         commandProcess.StartInfo.RedirectStandardOutput = true;  
  13.         commandProcess.StartInfo.RedirectStandardError = true;  
  14.         commandProcess.Start();  
  15.         var reader = new ProcessOutputReader(commandProcess);  
  16.         reader.ReadProcessOutput();  
  17.         commandProcess.WaitForExit();  
  18.         string output = reader.StandardOutput;  
  19.         lastStandardOutput = output;  
  20.         string error = reader.StandardError;  
  21.         commandProcess.Close();  
  22.         if (isCheck == true)  
  23.         {  
  24.             if (ComponentType.Equals("1"))  
  25.             {  
  26.                 if (!string.IsNullOrEmpty(FtpType) && FtpType.ToUpper().Equals("FTP"))  
  27.                 {  
  28.                     if (lastStandardOutput.ToLower().Contains("content-length") && lastStandardOutput.ToLower().Contains("last-modified"))  
  29.                     {  
  30.                         return true;  
  31.                     }  
  32.                     else  
  33.                     {  
  34.                         return false;  
  35.                     }  
  36.                 }  
  37.                 else if (!string.IsNullOrEmpty(FtpType) && FtpType.ToUpper().Equals("SFTP"))  
  38.                 {  
  39.                     if (error.ToLower().Contains("could not open directory for reading: no such file or directory") || error.ToLower().Contains(" could not open remote file for reading: no such file or directory"))  
  40.                     {  
  41.                         return false;  
  42.                     }  
  43.                     else  
  44.                     {  
  45.                         return true;  
  46.                     }  
  47.                 }  
  48.                 else if (!string.IsNullOrEmpty(FtpType) && FtpType.ToUpper().Equals("FTPS"))  
  49.                 {  
  50.                     if (lastStandardOutput.ToLower().Contains("content-length") && lastStandardOutput.ToLower().Contains("last-modified"))  
  51.                     {  
  52.                         return true;  
  53.                     }  
  54.                     else  
  55.                     {  
  56.                         return false;  
  57.                     }  
  58.                 }  
  59.             }  
  60.             else if (ComponentType.Equals("2"))  
  61.             {  
  62.                 if (!string.IsNullOrEmpty(FtpType) && FtpType.ToUpper().Equals("FTP"))  
  63.                 {  
  64.                     if (error.ToLower().Contains("server denied you to change to the given directory"))  
  65.                     {  
  66.                         return false;  
  67.                     }  
  68.                     else  
  69.                     {  
  70.                         return true;  
  71.                     }  
  72.                 }  
  73.                 else if (!string.IsNullOrEmpty(FtpType) && FtpType.ToUpper().Equals("SFTP"))  
  74.                 {  
  75.                     if (error.ToLower().Contains("could not open directory for reading: no such file or directory"))  
  76.                     {  
  77.                         return false;  
  78.                     }  
  79.                     else  
  80.                     {  
  81.                         return true;  
  82.                     }  
  83.                 }  
  84.                 else if (!string.IsNullOrEmpty(FtpType) && FtpType.ToUpper().Equals("FTPS"))  
  85.                 {  
  86.                     if (error.ToLower().Contains("server denied you to change to the given directory"))  
  87.                     {  
  88.                         return false;  
  89.                     }  
  90.                     else  
  91.                     {  
  92.                         return true;  
  93.                     }  
  94.                 }  
  95.             }  
  96.         }  
  97.         else  
  98.         {  
  99.             if (error.ToLower().Contains("curl"))  
  100.             {  
  101.                 LogError = error.Substring(error.LastIndexOf("curl"), 10).ToString();  
  102.             }  
  103.         }  
  104.         if (isReturn) result = LoggedError(error);  
  105.         return result;  
  106.     }  
  107.     catch (Exception ex)  
  108.     {  
  109.         _errorMessage = ex.ToString();  
  110.         Program.writeLogFile("|LauncherService|ERROR |" + _errorMessage, false);  
  111.         result = false;  
  112.     }  
  113.     return result;  
  114. }  
References

 

Thanks for reading this article, hope you enjoyed it.


Similar Articles