Process question

  • Thread starter Thread starter ask
  • Start date Start date
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.
 
The reason it is hanging is that it is probably waiting for input of
some kind (username probably).

However, one has to ask, why not use a third party library to FTP, or
the WinInet functions for FTP through the P/Invoke layer? It would be a
much cleaner solution and give you much more control over the process.

Hope this helps.
 
Hi,

I've tried this with the FTP command myself using VBScripting, but never got
it to work, so I don't think it's C# problem, but might be something with
the FTP tool. What I ended up doing was to write a script that generated an
FTP script, and then used the -s option of the ftp tools to read that script
instead.


Arild
 
This is a classic you will see if you use Process class incorrectly.
You can read the document about Process class to understand why this
happens.
Always read the document :)
In our next release, you will be able to get the output asynchrously.

Gang Peng
[MS]
 
Back
Top