Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I should access the output of a console application like CMD, for example when i enter the command: "ping 192.168.1.1" in the cmd, I should read the reply of cmd.

i don't want to send a command from my app to cmd. i want to users work with a console app and running their command on this, and my windows app that works on background, read the reply of the console app.

I should add that the app that i want to access it's output, is not a console app quite. that is an win app, that users can click on a button, and run the app in console environment. so i can not access the console app name, to run it

can you help me?

Thanks in advance.
Posted
Updated 26-May-15 20:20pm
v4

If you can't identify the app, then there really isn't a lot you can do - and if it's being opened by a third party app at the users request then pretty much the only way to find it is to read the active windows list and look for changes. But...that will find every application the user opens, including Solitaire, Notepad, IE..., so isolating the one you want probably won't be simple.

And I'm not sure that you can "get at" the text output of a console app anyway unless your application is the one creating the process - I'm pretty sure that you would need to divert the output stream to you app via the Process.StandardOutput[^] property of the Process object.

What are you trying to do that you think this might be a solution?
 
Share this answer
 
This is done with the class System.Diagnostics.Process used to start your application. As redirection will be needed (see below), you need to use the Start method using System.Diagnostics.ProcessStartInfo:
https://msdn.microsoft.com/en-us/library/system.diagnostics.process%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/0w4h05yb%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx[^].

You need to re-direct the StandardOutput stream (and, just in case, StandardError), to pick up the output of the application you start.

The redirection code sample can be found here: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput%28v=vs.110%29.aspx[^].

Now, a big warning for you, just in case: don't repeat one of the stupid mistakes found on this forum: don't use CMD.EXE. It's totally pointless. In your example, your constructor call ProcessStartInfo(string, string) should directly use "ping" and "192.168.1.1" for first and second parameters:
https://msdn.microsoft.com/en-us/library/b5zzxthe(v=vs.110).aspx[^].

—SA
 
Share this answer
 
Comments
DevSTDev 27-May-15 1:51am    
Thanks for your suggestion,
but I have not the console app name. users should click on a button to run that console app.

I was able to get my point?
Sergey Alexandrovich Kryukov 27-May-15 1:57am    
So what? Somebody knows the name, otherwise there is nothing to run.
Are you getting my point?
The "problem" is extremely simple, it's hard for me to imagine what you are not getting.
—SA
DevSTDev 27-May-15 2:12am    
Infact my win app should works in the background,
and it never send a command to that console app.
users are working whith their app, and my app should read the reply of that console app.

can you show an example to me?
Sergey Alexandrovich Kryukov 27-May-15 2:17am    
What, what sample you may possibly need? I already pointed out where you get the redirection sample. Just read my answer more thoroughly and follow the links.
—SA
DevSTDev 27-May-15 2:30am    
I neeeeeddd the FileName to use RedirectStandardOutput.
in that win app there is 4 buttons, by clicking on them redirect to others form that these forms like CMD. these 4 forms have one proccessName,
ANd can not run their directly by their ProcessName.

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