Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 1st, 2013, 02:37 PM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default Process.Start, cmd.exe, multiple commands and echo

I am trying to open a visible Process, start cmd.exe, and then pass it arguments one at a time. In pseudo-code, this would like something like this:

1. Open "cmd.exe"
2. Send it "D:"
3. See the "D:" command echoed in the cmd.exe process window.
4. See the drive identifier change in the cmd.exe process window.
5. Send it "cd\temp"
6. See the change directory command echoed in the cmd.exe window.
7. See the cmd.exe window change the folder.

This should open a cmd.exe window that looks something like this (when done):

C:\>D: <----Sent in step 2 above.

D:\>cd\temp <----Sent in step 5 above.

d:\Temp>

Important things to note are:
1. The cmd.exe window needs to be visible.
2. The user wants to see each command echoed in the cmd.exe process window as it is sent to the process object.
3. I cannot run a batch file containing the commands. Each command must be sent individually to the cmd.exe window for procesing.
4, SendKeys cannot be used because the cmd.exe window may not always have focus.

This seems like it ought to be very simple to do yet I have been going around in circles with Process.Start and cannot seem to get it to provide all of the functionality needed above.

1. I can get it to open the Process window containing cmd.exe and even keep it open with the "/K" parm, but I have to pass it multiple commands concatenated with " && " statement. This appears to run all the commands at before returning any results so I cannot see how each command executed in the window. IOW, if I send it "D: && cd\Temp" as shown above, all I get is the command prompt now showing "D:\Temp". Neither "D:" nor "cd\temp" is ever echoed to the cmd.eexe process window, just the end result, and the end user needs to see the result of each step as it is processed.

2. I've tried sending subsequent commands using another Process.Start, like this:

Process.Start("cmd.exe, "\K d:")
Process.Start("cmd.exe, "\K cd\temp")

but that opens another cmd.exe process window. "cd\temp" won't work unless d: is executed first in the same Process window.

3. I've tried redirecting the Standard Input stream and the cmd.exe Process window opens completely black; I get no DOS prompt or anything to even indicate the window is running cmd.exe. Nothing sent to it using Process.StandardInput.WriteLine ever appears in the window and I cannot tell if it has done anything at all. RedirectStandardInput appears to be causing this, because when I comment this line out, the command window opens as expected. Possibly further complicating things are sites I've found which indicate the cmd.exe requires redirecting input AND output, which may be why the Process window is empty. But If I try to read the redirected output, it hangs forever the process.stereamreader.ReadToEnd, which leads me to believe it has not returned from performing the command sent.

I'd really appreciate a code snippet that references cmd.exe, sends two non-concatenated commands to cmd.exe (IOW, not concatenated in the arguments using "&&", and echoes both the command sent and results returned. Most on-line code samples I've Googled run something other than cmd.exe and those referencing cmd.exe only seem to work when passing concatenated commands, which does not produce teh results needed. Thanks!
Reply With Quote
  #2 (permalink)  
Old April 2nd, 2013, 03:47 AM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi Ron,

I could be wrong, but I don't think this can be done. There's a big difference between the initial arguments and later commands. Consider a super simple Command Line app like this:

Code:
static void Main(string[] args)
{
  string anotherArg = Console.ReadLine();
}
The initial arguments, passed to Process.Start end up in the args parameter of the Main method. All subsequent user input is retrieved using Console.ReadLine().

So I think - although again, I could be very wrong -, the only option is to use something like SendKeys or alternative solutions. You may want to look into something like AutoIt (http://www.autoitscript.com/site/autoit/) that has the ability to activate windows before commands are sent.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
Ron Howerton (April 2nd, 2013)
  #3 (permalink)  
Old April 2nd, 2013, 08:28 AM
Friend of Wrox
Points: 2,876, Level: 22
Points: 2,876, Level: 22 Points: 2,876, Level: 22 Points: 2,876, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 428
Thanks: 57
Thanked 2 Times in 2 Posts
Default

Thanks for your response, Imar!

I suspect you're right. An Autoit user in their forum suggests using "&&" to concatenate DOS commands to workaround the same limitation:

http://www.autoitscript.com/forum/to...s-in-one-line/

Several sites claims it's possible to redirect standard input, but none of the samples provided seem to work. Looks like I'm going to have to pass concatenated DOS commands. Perhaps I can break these old batch files down into smaller steps that can be performed in different cmd.exe windows.

Thanks again, Imar!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Publish ASP.Net Code with cmd Line using Process rhd110 General .NET 0 February 2nd, 2008 03:26 PM
Taking output from cmd commands codefishy C++ Programming 1 July 2nd, 2007 01:44 PM
how to call shell cmd to open exe on clientside? nana Javascript How-To 6 May 25th, 2006 03:23 AM
Executing an .EXE with ASP and capturing the echo Jase Etheridge Classic ASP Basics 3 October 18th, 2005 02:48 AM
Running cmd line .EXE through PHP geolast PHP How-To 2 January 6th, 2005 02:34 PM



All times are GMT -4. The time now is 09:15 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.