1

I need to get the following thing into the CMD in C#:

  1. navigate to location
  2. start ftp.exe
  3. open server
  4. user
  5. password
  6. get file
  7. close
  8. quit

How do I accomplish that?

Please mind that I can not use Net.FtpWebRequest for this particular task.

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

000000000000000000000
  • 1,467
  • 1
  • 19
  • 38
  • 2
    It is often simpler to use the appropriate .Net (or third-party) library instead of dealing with a command line application that requires complex interaction. Is there a *reason* you can't use the .Net library? – crashmstr Jun 24 '15 at 12:31

2 Answers2

2

Try calling a 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();

Call ftp.bat file

Ftp.Bat file contains...

ftp -s:commands.ftp

Then in your commands.ftp

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

Or something similar.

JustAspMe
  • 418
  • 1
  • 4
  • 18
  • Thanks for the answer, but I am programming an automated backup service for multiple devices with different user settings. That means I'd need to create a new bat file for every device first. ^^ – 000000000000000000000 Jun 24 '15 at 12:39
  • Do you think using a *Dynamic* object instead of a *Process* object would change anything regarding the problem of my first attempt to solve this? (http://stackoverflow.com/questions/31024619/ftp-process-wont-close-c?noredirect=1#comment50077686_31024619) – 000000000000000000000 Jun 24 '15 at 12:42
  • Could you generate the commands.ftp file for each user prior to doing a cmd.Run? – JustAspMe Jun 24 '15 at 12:43
  • That would be a solution. However it would also be very complicated and dirty. But thank you very much for your suggestion. – 000000000000000000000 Jun 24 '15 at 12:46
  • Not necessarily dirty, a temp file would not generate any noticeable clutter. – Machinarius Jun 24 '15 at 13:33
  • Lol, yes, definitely not pretty. I got FTP working with edited call to bat file. Confirmed the file came down. Would have to iterate and write out a different commands.ftp file for each user setting. I'm sure there is a better solution for you, however. – JustAspMe Jun 24 '15 at 13:36
  • I went with this solution, since it was the least painfull to code. I'll post my code as an answer. – 000000000000000000000 Jun 25 '15 at 07:26
1

The solution I went with:

C#:

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, which is availiable for the time it requires to download the file, contains the username and password of the server as plain text. :-/
I could append a random String to the name though to make it a little more secure.

000000000000000000000
  • 1,467
  • 1
  • 19
  • 38