473,394 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Process question

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
5 3630
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Michael J. Moore | last post by:
Is it the listener process, or some other Oracle process. Also, on a UNIX system, when you do "ps -ef" to see your processes, the PPID points back to a process named "init". Why does the PPID not...
2
by: mwazir | last post by:
Hi all, I have reposted this question from dotnet.general as I have been advised that this is a more appropriate forum for this question. Apologies for the repost. I have a process thats...
1
by: Doug Wyatt | last post by:
So I'll preface this with the fact that I'm a UNIX developer by training and have just recently gotten in to C# development on Windows. I'm basically running in to a problem whereby I suspect...
6
by: A | last post by:
Hi all, This is sort of a newbie question. I have a method that creates a process which in turn installs MSDE. Basically, after I start the process I need to wait until it is complete and then...
2
by: ca___t | last post by:
Hi there : I want to start a consoneApplication using process.start method at web application server side.I have a question at this.Why the consoneApplication's process is start but not run...
3
by: Nikolay Petrov | last post by:
I have a class which starts a process and redirects it's output and input. My class have a method which starts the process and another which stops it. How can I check if the process have been...
22
by: Zen | last post by:
Hi, My production machine has 2G of memory, when aspnet_wp.exe goes up to about ~1.2G of memory usage, I start get out-of-memory exception. Other processes don't use as much memory and I added...
2
by: tony.newsgrps | last post by:
Hi there, I'm trying to understand the impact of killing a process that owns a system mutex (used to ensure there is only 1 instance of my program running) Here is my code pretty much: try...
10
by: Susan | last post by:
I have a process that takes a while to run so I have it running asynchronously so that the user can continue working. My question is that I want to killl the process if the user changes the search...
9
by: SeC | last post by:
Hi. Is there any way to detect if application is being killed by 'End Process' via Task Manager ?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.