0

I made a C# program that inputs ftp commands into cmd.exe, it will read the output and displays it on the console. The problem is, it gets stuck when reading the output of Console.WriteLine(srFTP.ReadToEnd()); The debugger does not show any errors. It simply gets stuck. I tried putting console.writelines on each line and it proved that it was stuck at that point. Help?

    public static void CheckKVSConnect()
    {
        Process KVSFTP = new Process();
        KVSFTP.StartInfo.FileName = "cmd.exe";
        KVSFTP.StartInfo.Arguments = "disable";
        KVSFTP.StartInfo.UseShellExecute = false;
        KVSFTP.StartInfo.RedirectStandardOutput = true;
        KVSFTP.StartInfo.RedirectStandardInput = true;
        KVSFTP.StartInfo.RedirectStandardError = true;
        KVSFTP.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        KVSFTP.Start();
        StreamWriter swFTP = KVSFTP.StandardInput;
        swFTP.WriteLine("ftp");
        swFTP.WriteLine("open aaaaaa.org");
        swFTP.WriteLine("username");
        swFTP.WriteLine("password");
        StreamReader srFTP = KVSFTP.StandardOutput;
        Console.WriteLine(srFTP.ReadToEnd());
        Console.WriteLine("DONE");
    }
Matty
  • 77
  • 1
  • 9

1 Answers1

0

ReadToEnd will keep reading until the stream is provably and permanently at the end, i.e., the app has actually closed its standard output stream (which usually only happens when the app exits). You haven't gotten to that point -- all you've done is log in.

You'll have to use ReadLine instead, and parse the lines to figure out when you've gotten to the point where you need to send more input to the app. (Or use managed code to perform your FTP operations instead of shelling out to an interactive command-line app.)

Joe White
  • 94,807
  • 60
  • 220
  • 330