|
-
Feb 4th, 2009, 07:47 PM
#1
Thread Starter
Lively Member
[RESOLVED] cmd.exe
Hi im would like to make a gui for a console program i use. I am able to start the cmd.exe and change to the directory the program is in. My problem is after i issue a command to the console such as cd c:\myconsoleapp cmd.exe closes why is this. I would like the console to stay running so i can issure more commands to it without having to re declare streamwriter and reader and starting the process each time.
Code:
myProcess.startInfo.CreateNoWindow = false
myProcess.StartInfo.FileName = "cmd.exe"
myProcess.StartInfo.UseShellExecute = false
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.Start()
Dim myStreamWriter As system.IO.StreamWriter = myProcess.StandardInput
Dim mystreamreader As system.IO.StreamReader = myProcess.StandardOutput
myStreamWriter.WriteLine("cd C:\myconsoleapp")
myStreamWriter.Close()
Dim str As String = mystreamreader.ReadToEnd
//display console text in textbox
shelltb.Text = shelltb.Text & vbcrlf & str
-
Feb 4th, 2009, 08:08 PM
#2
Re: cmd.exe
Pass "/K" as a Process.StartInfo.Arguments property
MyProcess.StartInfo.Arguments = "/K"
-
Feb 4th, 2009, 08:10 PM
#3
Thread Starter
Lively Member
Re: cmd.exe
nope that didnt change anything. What is that for any way? maybe i should explain better. i want to start cmd.exe which i can do, then i want to run a console program that runs as a shell so once i do that i get a # symbol in the cmd.exe window from which i can issue commands such as "cd /system" or "rm filename.xxx" But whats happening is the process closes after one command is issued and then the output from the window is being displayed in my text box so i cant issue any more commands.
Last edited by chaos75; Feb 4th, 2009 at 08:22 PM.
-
Feb 4th, 2009, 08:17 PM
#4
Re: cmd.exe
That's "keep" as far as I remember. I dont have an example in front of me but Im sure that's the one, maybe it has to be "/K "
-
Feb 4th, 2009, 08:17 PM
#5
Re: cmd.exe
Well, I suggest removing the line that closes the StreamWriter.
-
Feb 4th, 2009, 08:24 PM
#6
Thread Starter
Lively Member
Re: cmd.exe
if i comment out the streamwriter.close line then the cmd.exe window stays opened but my program freezes untill i manualy close cmd.exe. I tried /K and \K neither worked.
-
Feb 4th, 2009, 08:27 PM
#7
Re: cmd.exe
I was trying to find it on MSDN and found this link to the same subject.
http://www.microsoft.com/communities...r=US&sloc=&p=1
-
Feb 4th, 2009, 08:39 PM
#8
Thread Starter
Lively Member
Re: cmd.exe
ok ill chexk that out later and post back if i got it to work thanx for the help guys
-
Feb 4th, 2009, 08:56 PM
#9
Re: cmd.exe
Sorry, I thought that's what you wanted to do... 
Maybe you could use this code, then write anytime to the window, plus I don't think your program will freeze...
'Class-level
vb Code:
Dim t As Thread = New Thread(AddressOf TheSubWhereTheProcessStarts)
Dim sWriter As StreamWriter, sReader As StreamReader
Private Sub begin()
t.Start() ' Start the thread and the process, plus initialize the streamwriter & readers
Threading.Thread.Sleep(1000) 'Wait for the other thread to declare everything
End Sub
So, you can write things to it at any time, does not freeze. If you want to close the box, you should use a class-level boolean, plus SyncLock.
Hope this is helpful!
-
Feb 5th, 2009, 10:19 AM
#10
Junior Member
Re: cmd.exe
I have recently been down this road. The thread, and solution, are here:
http://social.msdn.microsoft.com/For...d-8538061db76c
Rock
-
Feb 5th, 2009, 12:04 PM
#11
Thread Starter
Lively Member
Re: cmd.exe
So i have to embead the console into my app? there must be a better way. Why does cmd.exe close after i send anything to it dosnt matter if its a command or just random text? I have no problem sending and getting info from the cmd.exe window it just wont stay opened for more than one command and i need to be able to keep sending diffrent commands
Last edited by chaos75; Feb 5th, 2009 at 12:30 PM.
-
Feb 5th, 2009, 12:33 PM
#12
Junior Member
Re: cmd.exe
 Originally Posted by chaos75
So i have to embead the console into my app? there must be a better way. Why does cmd.exe close after i send anything to it dosnt matter if its a command or just random text? I have no problem sending and getting info from the cmd.exe window it just wont stay opened for more than one command.
No, you don't HAVE to embed the cmd console, it is just one approach. I explored three methods before I found the one that suited me best:
1. Opening then hiding a cmd window, then redirecting its input and output to a TextBox.
2. Embedding a cmd window into a Panel
3. Embedding console.exe into a Panel.
One of the problems we found with redirection was that if you typed rubbish at the prompt, then instead of cmd reporting that the command was not recognised it would just close instead. We never did find why this happened. It also suffered from a lack of a buffer, and colored text was not reproduced.
One of the problems with embedding cmd.exe into a panel was that as cmd.exe is not a UI (and hence no message loop) you could not use WaitForInputIdle, and you had to use a Thread.Sleep instead, and the value for this was arbitrary and could vary from machine to machine.
The solution that suited me was to embed console.exe (a replacement for cmd.exe) which does have a UI, and was much more well-behaved than cmd.exe. I could also switch off the MenuBar and ToolBar with just a switch in its settings file, no need for code.
I pointed you to the thread so you could see the various discussions that we had as we went down this road (there are more than one thread there, just search on my name), and so you could decide if any of the approaches better suited your requirements. As for why your approach fails I have no idea, I am no expert, I just wanted to point you to a solution that does work.
Rock
-
Feb 5th, 2009, 12:42 PM
#13
Thread Starter
Lively Member
Re: cmd.exe
Ok thanx, I didnt realize a bad command would close cmd.exe and that was what was happening. I forgot a space between cd and the directory so cmd.exe didnt recognize it and closed. So i can use cmd.exe as long as i make sure no bad commands are issued other wise it will close.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|