A
ask
Hi NG
I'm a bit new to programming c# and have a question regarding ftp by the
command prompt. As far as I can see it should be possible to start a process
and pipe command streams into it. But I cant make it work, it always stalls
when I try to read the output:
/// code 1
Process ftpProcess = new Process();
ftpProcess.StartInfo.FileName = "cmd";
ftpProcess.StartInfo.CreateNoWindow = true;
ftpProcess.StartInfo.RedirectStandardInput = true;
ftpProcess.StartInfo.RedirectStandardOutput = true;
ftpProcess.StartInfo.UseShellExecute = false;
ftpProcess.Start();
string output = ftpProcess.StandardOutput.ReadToEnd();
string error = ftpProcess.StandardError.ReadToEnd();
ftpProcess.StandardInput.WriteLine("ftp localhost");
ftpProcess.StandardInput.WriteLine();
output = ftpProcess.StandardOutput.ReadToEnd();
error = ftpProcess.StandardError.ReadToEnd();
ftpProcess.StandardInput.WriteLine("username");
ftpProcess.StandardInput.WriteLine();
//... further ftp action - but I never get this far....
output = ftpProcess.StandardOutput.ReadToEnd();
error = ftpProcess.StandardError.ReadToEnd();
ftpProcess.WaitForExit();
///////////
I also tried:
/// code2
char [] output = new char [1000];
string all = "";
Process ftpProcess = new Process();
ftpProcess.StartInfo.FileName = "cmd";
ftpProcess.StartInfo.CreateNoWindow = true;
ftpProcess.StartInfo.RedirectStandardInput = true;
ftpProcess.StartInfo.RedirectStandardOutput = true;
ftpProcess.StartInfo.UseShellExecute = false;
ftpProcess.Start();
int i = ftpProcess.StandardOutput.Read(output, 0, 1000);
ftpProcess.StandardInput.WriteLine("ftp localhost");
i = ftpProcess.StandardOutput.Read(output, i, 1000-i);
ftpProcess.StandardInput.WriteLine("username");
ftpProcess.StandardInput.WriteLine("password");
ftpProcess.StandardInput.WriteLine("mkdir test");
ftpProcess.StandardInput.WriteLine("ls");
ftpProcess.StandardInput.WriteLine("bye");
all = ftpProcess.StandardOutput.ReadToEnd();
thanx
ps. And yes - I did connect manually by the command prompt so it should
work.
I'm a bit new to programming c# and have a question regarding ftp by the
command prompt. As far as I can see it should be possible to start a process
and pipe command streams into it. But I cant make it work, it always stalls
when I try to read the output:
/// code 1
Process ftpProcess = new Process();
ftpProcess.StartInfo.FileName = "cmd";
ftpProcess.StartInfo.CreateNoWindow = true;
ftpProcess.StartInfo.RedirectStandardInput = true;
ftpProcess.StartInfo.RedirectStandardOutput = true;
ftpProcess.StartInfo.UseShellExecute = false;
ftpProcess.Start();
string output = ftpProcess.StandardOutput.ReadToEnd();
string error = ftpProcess.StandardError.ReadToEnd();
ftpProcess.StandardInput.WriteLine("ftp localhost");
ftpProcess.StandardInput.WriteLine();
output = ftpProcess.StandardOutput.ReadToEnd();
error = ftpProcess.StandardError.ReadToEnd();
ftpProcess.StandardInput.WriteLine("username");
ftpProcess.StandardInput.WriteLine();
//... further ftp action - but I never get this far....
output = ftpProcess.StandardOutput.ReadToEnd();
error = ftpProcess.StandardError.ReadToEnd();
ftpProcess.WaitForExit();
///////////
I also tried:
/// code2
char [] output = new char [1000];
string all = "";
Process ftpProcess = new Process();
ftpProcess.StartInfo.FileName = "cmd";
ftpProcess.StartInfo.CreateNoWindow = true;
ftpProcess.StartInfo.RedirectStandardInput = true;
ftpProcess.StartInfo.RedirectStandardOutput = true;
ftpProcess.StartInfo.UseShellExecute = false;
ftpProcess.Start();
int i = ftpProcess.StandardOutput.Read(output, 0, 1000);
ftpProcess.StandardInput.WriteLine("ftp localhost");
i = ftpProcess.StandardOutput.Read(output, i, 1000-i);
ftpProcess.StandardInput.WriteLine("username");
ftpProcess.StandardInput.WriteLine("password");
ftpProcess.StandardInput.WriteLine("mkdir test");
ftpProcess.StandardInput.WriteLine("ls");
ftpProcess.StandardInput.WriteLine("bye");
all = ftpProcess.StandardOutput.ReadToEnd();
thanx
ps. And yes - I did connect manually by the command prompt so it should
work.