Click here to Skip to main content
15,881,742 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Is it possible to perform the following steps below using C# 2008:

1. open and hide the command prompt
2. using C# textbox to send command ("c:\") to the hidden command prompt
3. Display output from the command prompt to richtextbox
4. send another command ("dir") to command prompt
5. Display output from the command prompt to richtextbox but append to it.
6. send another command ("cd \windows") to command prompt
7. Display output from the command prompt to richtextbox but append to it.
8. and so on...until I decided to quit

If all these can be done...please provide a sample code. Thanks.
Posted
Updated 3-Aug-20 2:28am

1 solution

It's all possible, only makes no sense with the set of commands mentioned, because it's much easier to get the effect if those command immediately from Windows API using the types and methods from the namespace System.IO.

Anyway, using command interpreter CMD.EXE programmatically is quite possible through using the class System.Diagnostics.Process, please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

The trick is using redirection of the streams StandardInput and StandardOutput (and, if required, also StandardError). The stream StandardInput is used to feed the command to the command interpreter, and the stream StandardOutput — for collecting of the output text.

For the code sample with redirected input please see here:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardinput.aspx[^].

For the code sample with redirected output — here:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

Combining of those code sample would be quite enough to implement the functionality you want. I want to repeat that it does not make a lot of sense: implementation of required functionality at the level of OP APIs is much more natural ans simple.

—SA
 
Share this answer
 
Comments
Dean Oliver 22-Mar-12 1:35am    
nice. +5
Sergey Alexandrovich Kryukov 22-Mar-12 2:02am    
Thank you, Dean.
--SA
wgha168 22-Mar-12 22:42pm    
Thanks for links, but unfortunately I'm a beginner in C# so I still have alot to learn. I'm don't know how to combine them to get it to work.

Can you provide me a sample that you mention on the very top comment?

"It's all possible, only makes no sense with the set of commands mentioned, because it's much easier to get the effect if those command immediately from Windows API using the types and methods from the namespace System.IO."
Sergey Alexandrovich Kryukov 22-Mar-12 22:59pm    
If you are a beginner, I suggest you begin. What tells you you cannot combine the samples? Did you try it? You only need to understand each line. Please feel free to ask further questions if you do not.

As to System.IO, here is what I meant was pretty trivial:

1) Getting files in directory like with "dir" command: System.IO.Directory.GetFiles and System.IO.Directory.GetDirectories, also System.IO.Directory.GetFileSystemEntries.
See:
http://msdn.microsoft.com/en-us/library/system.io.directory.aspx

2) Setting current (working) directory like in "cd" command:
System.IO.Directory.SetCurrentDirectory, see:
http://msdn.microsoft.com/en-us/library/system.io.directory.setcurrentdirectory.aspx

And stuff like that. Use System.IO.Path, System.IO.DirectoryInfo, System.IO.FileInfo, etc.
See:
http://msdn.microsoft.com/en-us/library/29kt2zfk.aspx

Is it clear now?
--SA
TECNO14 3-Aug-20 7:28am    
how to get the effect if those command immediately from Windows API ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900