0

I am trying to access command prompt from c# code and want to do FTP to get and put files form the ftp server. by using the following code I am able to connect to the FTP server. However, the put command is not working. Please assist.

        var process = new Process {StartInfo = startInfo};
        process.Start();
        process.StandardInput.WriteLine(@"cd "+localPath);
        process.StandardInput.WriteLine(@"dir");
        process.StandardInput.WriteLine(@"ftp "+serverURL);
        process.StandardInput.WriteLine(@""+username);
        process.StandardInput.WriteLine(@""+password);
        process.StandardInput.WriteLine(@"cd "+serverPath);
        process.StandardInput.WriteLine(@"put "+fileName);
        process.StandardInput.WriteLine(@"bye");
        process.StandardInput.WriteLine(@"exit");

        process.WaitForExit();
  • 1
    What is the output you get from the process after each line you sent? – Progman May 16 '19 at 19:36
  • 1
    You seem to be hammering the standard input as fast as you can without waiting for a response. Maybe try adding some delays or checking the standard out to see what response you get. – Ron Beyer May 16 '19 at 19:37
  • All the writelines seems overly complicated. You should be able to build a string and do it all at once: https://stackoverflow.com/questions/1469764/run-command-prompt-commands – Brad May 16 '19 at 19:37
  • @Ron Beyer, I tired putting System.Threading.Thread.Sleep(2300); to know where exactly its failing and the code seems to hang at the put +filename command – user11511891 May 16 '19 at 19:40
  • Have you you tried running the commands manually from the command prompt to ensure this isn't e.g. a networking issue? – cronburg May 16 '19 at 19:48
  • @cronburg, yes it works fine manually... – user11511891 May 16 '19 at 19:50
  • Perhaps the `fileName` variable is a null string / empty when you do it from C#? Try printing that variable to stdout. – cronburg May 16 '19 at 19:52
  • 1
    A 0 byte file with the same file name is created on the mentioned location, but the process is not getting completed. – user11511891 May 16 '19 at 19:54
  • 1
    Try executing ftp with `ftp -dv` to get verbose / debugging info. – cronburg May 16 '19 at 20:05
  • @cronburg, following is what i get, but the request timesout after this 230 User glads logged in. ---> CWD /interfaces/R3D/ediout/RPABOTS/outbound/ 250 CWD command successful. ---> XPWD 257 "/interfaces/R3D/ediout/RPABOTS/outbound" is current directory. ---> PORT 19,39,44,158,242,182 200 PORT command successful. ---> STOR birthdaylist.xlsx – user11511891 May 16 '19 at 22:50

1 Answers1

0

Two possibilities come to mind:

  1. Do you have write permission in serverPath? (Being able to connect does not mean that you do.)

  2. Immediately before the put, set the transfer mode

    process.StandardInput.WriteLine(@"binary");

Otherwise please describe system responses... "is not working" could mean "does not respond (times out)", "returns error XXX", "finishes, appearing to succeed but file is not transferred" etc.

AlanK
  • 1,827
  • 13
  • 16
  • I have the permissions, tried using Binary did not work, and by saying not working ...i wanted to say the request is timing out, but it works manually when i am doing the same.Apologies for the incomplete info – user11511891 May 16 '19 at 22:34