Use ftp.exe from C #

I need to do the following in CMD in C #:

  • go to location
  • run ftp.exe
  • open server
  • User
  • password
  • get the file
  • close
  • log off

How to do it?

Please note that I cannot use Net.FtpWebRequest for this specific task.

Is there a way to log in on one line like ftp user: password @host?

0


source to share


2 answers


Solution I went with:

FROM#:



String ftpCmnds = "open " + folder.server + "\r\n" + folder.cred.user + "\r\n" + folder.cred.password + "\r\nget " + file + "\r\nclose\r\nquit";

//This is a custom method that I wrote:
Output.writeFile(basePath + "\\" + Util.getDateFormated(reverseDate) + "\\" + parentFolder + "\\" + folder.server + "\\", "tmp.txt", ftpCmnds);
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.WindowStyle = ProcessWindowStyle.Hidden;
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;

p.StartInfo = info;
p.Start();

using (StreamWriter sw = p.StandardInput)
{
   if (sw.BaseStream.CanWrite)
   {
      Console.WriteLine("Forcing Download from " + folder.server + folder.path + " of " + file + "\n"); log += "\r\n\r\n\t\t- Forcing Download from " + folder.server + folder.path + file + "\tto\t" + basePath + "\\" + Util.getDateFormated(reverseDate) + "\\" + parentFolder + "\\" + folder.server + "\\" + file;
      sw.WriteLine("cd " + basePath + "\\" + Util.getDateFormated(reverseDate) + "\\" + parentFolder + "\\" + folder.server + "\\");
      sw.WriteLine("ftp -s:tmp.txt");
      sw.WriteLine("del tmp.txt");
      p.Close();
   }
}

      

The only "bad" thing is that the tmp.txt file available for the time it takes to download the file contains the server username and password as plain text .: - /
I could add a random string to the name, but make it more secure.

+1


source


Try calling your bat file?

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"/c e:\test\ftp.bat";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.Start();

      

Calling the ftp.bat file

Ftp.Bat file contains ...



ftp -s:commands.ftp

      

Then in your .ftp commands

open <server_address>
<userid>
<password>
recv <source_file> <dest_file>
bye

      

Or something similar.

+2


source







All Articles