1

I have written a winform app in C# which modifies FTP commands in a script file (server address etc..) and then executes a batch file that is supposed to start FTP from command prompt and download files according to script.

When i run the Batch file external to app (from command prompt or double click), the files are being downloaded fine, but when executing the batch from the app nothing happens...

I also noticed following difference: When running batch externally I see "ftp> open 10.1.1.1" in cmd window. When running batch from the app I see "open 10.1.1.1" in cmd window,(missing the "ftp>").

My guess is that I'm not using System.Diagnostics.Process() correctly...

C# relevant part:

    private void button1_Click(object sender, EventArgs e)
    {
            var cmd = new System.Diagnostics.Process();
            cmd.StartInfo.FileName = "runFTP.bat";
            cmd.StartInfo.UseShellExecute = false;
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.WorkingDirectory = path;
            cmd.Start();
            cmd.WaitForExit();
            cmd.Close();
        }

    }

Batch file (runFTP.bat):

echo off

@echo Downloading files...
REM ==Start FTP with script==

ftp -i -s:ftpCmd.txt

del ftpCmd.txt

@echo Done!
@echo Exiting...

FTP script file (ftpCmd.txt):

open 10.1.1.1
user
password
bin
cd /rootFolder/new
lcd C:\Downloads
mget *.*
bye
1
  • Can you redirect ftp output to a file in both scenarios and share the output here? Commented Mar 9, 2015 at 10:24

1 Answer 1

1

I found a solution to my problem.. works fine now :)

By adding and changing in C# the following System.Diagnostics.Process() StartInfo parameters to false, I was able to download the files and get the missing "ftp>" on command prompt.

Modified C# code looks like this

            var cmd = new System.Diagnostics.Process();
            cmd.StartInfo.FileName = "runFTP.bat";
            cmd.StartInfo.UseShellExecute = false;
            cmd.StartInfo.RedirectStandardInput = false;
            cmd.StartInfo.RedirectStandardOutput = false;
            cmd.StartInfo.WorkingDirectory = path;
            cmd.Start();
            cmd.WaitForExit();
            cmd.Close();

Thanks to anyone spent time on this.. hope this will contribute to someone else.

Sign up to request clarification or add additional context in comments.

1 Comment

can we use the same for uploading files to the server using put?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.