By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,169 Members | 1,661 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,169 IT Pros & Developers. It's quick & easy.

Process question

P: n/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.
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com

"ask" <a@creepREM.dk> wrote in message
news:3f*********************@news.dk.uu.net...
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.

Nov 15 '05 #2

P: n/a
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

"ask" <a@creepREM.dk> wrote in message
news:3f*********************@news.dk.uu.net...
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.

Nov 15 '05 #3

P: n/a
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

"ask" <a@creepREM.dk> wrote in message
news:3f*********************@news.dk.uu.net...
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.

Nov 15 '05 #4

P: n/a
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]

"ask" <a@creepREM.dk> wrote in message
news:3f*********************@news.dk.uu.net...
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.

Nov 15 '05 #5

P: n/a
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]

"ask" <a@creepREM.dk> wrote in message
news:3f*********************@news.dk.uu.net...
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.

Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.