6 Replies - 17846 Views - Last Post: 25 June 2011 - 10:59 PM Rate Topic: -----

#1 hironakamura  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 20-September 07

Reading and writing to and from command prompt

Post icon  Posted 28 April 2008 - 11:03 AM

Hey guys, im working on a small project that lets me read and write to and from command prompt, i have this code but one line does not work
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim CMD As New Threading.Thread(AddressOf GETCMD)
		CMD.Start()
	End Sub
	Private Sub GETCMD()
		Dim CMDprocess As New Process
		Dim StartInfo As New System.Diagnostics.ProcessStartInfo
		StartInfo.FileName = "cmd" 'starts cmd window
		StartInfo.RedirectStandardInput = True
		StartInfo.RedirectStandardOutput = True
		StartInfo.UseShellExecute = False 'required to redirect
		CMDprocess.StartInfo = StartInfo
		CMDprocess.Start()
		Dim SR As System.IO.StreamReader = CMDprocess.StandardOutput
		Dim SW As System.IO.StreamWriter = CMDprocess.StandardInput
		SW.WriteLine(TextBox1.Text) 'the command you wish to run.....
		SW.WriteLine("exit") 'exits command prompt window
		TextBox2.Text = SR.ReadToEnd 'returns results of the command window
		SW.Close()
		SR.Close()
	End Sub 


but one line does not work this one :
  TextBox2.Text = SR.ReadToEnd 'returns results of the command window


id like some help on this please

thankyou

Is This A Good Question/Topic? 0
  • +

Replies To: Reading and writing to and from command prompt

#2 danny_kay1710  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 28
  • View blog
  • Posts: 358
  • Joined: 27-April 08

Re: Reading and writing to and from command prompt

Posted 28 April 2008 - 02:18 PM

Can you give some information about the error message.

You could try making sure that TextBox2 is multiline. After all most command prompt outputs do span multiple lines. Not sure if it would cause an error although it may just cause a blank text box.

Other than that would need to know how it isnt working

danny_kay1710
Was This Post Helpful? 0
  • +
  • -

#3 agupte  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-March 08

Re: Reading and writing to and from command prompt

Posted 30 April 2008 - 10:59 AM

I think Streameader returns a ByteArray, in which case you will have to convert from that to a string.

AG

View Posthironakamura, on 28 Apr, 2008 - 11:03 AM, said:

Hey guys, im working on a small project that lets me read and write to and from command prompt, i have this code but one line does not work
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim CMD As New Threading.Thread(AddressOf GETCMD)
		CMD.Start()
	End Sub
	Private Sub GETCMD()
		Dim CMDprocess As New Process
		Dim StartInfo As New System.Diagnostics.ProcessStartInfo
		StartInfo.FileName = "cmd" 'starts cmd window
		StartInfo.RedirectStandardInput = True
		StartInfo.RedirectStandardOutput = True
		StartInfo.UseShellExecute = False 'required to redirect
		CMDprocess.StartInfo = StartInfo
		CMDprocess.Start()
		Dim SR As System.IO.StreamReader = CMDprocess.StandardOutput
		Dim SW As System.IO.StreamWriter = CMDprocess.StandardInput
		SW.WriteLine(TextBox1.Text) 'the command you wish to run.....
		SW.WriteLine("exit") 'exits command prompt window
		TextBox2.Text = SR.ReadToEnd 'returns results of the command window
		SW.Close()
		SR.Close()
	End Sub 


but one line does not work this one :
  TextBox2.Text = SR.ReadToEnd 'returns results of the command window


id like some help on this please

thankyou

Was This Post Helpful? 0
  • +
  • -

#4 HighOnTek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 25-June 11

Re: Reading and writing to and from command prompt

Posted 25 June 2011 - 03:03 PM

View Posthironakamura, on 28 April 2008 - 11:03 AM, said:

Hey guys, im working on a small project that lets me read and write to and from command prompt, i have this code but one line does not work
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim CMD As New Threading.Thread(AddressOf GETCMD)
		CMD.Start()
	End Sub
	Private Sub GETCMD()
		Dim CMDprocess As New Process
		Dim StartInfo As New System.Diagnostics.ProcessStartInfo
		StartInfo.FileName = "cmd" 'starts cmd window
		StartInfo.RedirectStandardInput = True
		StartInfo.RedirectStandardOutput = True
		StartInfo.UseShellExecute = False 'required to redirect
		CMDprocess.StartInfo = StartInfo
		CMDprocess.Start()
		Dim SR As System.IO.StreamReader = CMDprocess.StandardOutput
		Dim SW As System.IO.StreamWriter = CMDprocess.StandardInput
		SW.WriteLine(TextBox1.Text) 'the command you wish to run.....
		SW.WriteLine("exit") 'exits command prompt window
		TextBox2.Text = SR.ReadToEnd 'returns results of the command window
		SW.Close()
		SR.Close()
	End Sub 


but one line does not work this one :
  TextBox2.Text = SR.ReadToEnd 'returns results of the command window


id like some help on this please

thankyou


Try this...

Private Results As String

'The "Delegate" is used to correct the threading issue (Can't update control directly in VB.net 08/10), and invokes the needed text update.
Private Delegate Sub delUpdate()
Private Finished As New delUpdate(AddressOf UpdateText)

Private Sub UpdateText()
resultsTextBox.Text = Results
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CMDThread.Start()
End Sub

Private Sub CMDAutomate()

Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo

'Starts the CMD Prompt
StartInfo.FileName = "cmd.exe"
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True

'Required to redirect
StartInfo.UseShellExecute = False

'Disables the creation of a CMD Prompt outside application.
StartInfo.CreateNoWindow = True


myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput

'Runs the command you entered...
SW.WriteLine(TextBox1.Text)

'Exits CMD Prompt 
SW.WriteLine("exit")

'Displayes the results...
Results = SR.ReadToEnd
SW.Close()
SR.Close()

'Invokes Finished delegate, which updates textbox with the results text
Invoke(Finished)
End Sub

This post has been edited by HighOnTek: 25 June 2011 - 03:04 PM

Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Reading and writing to and from command prompt

Posted 25 June 2011 - 03:31 PM

I think he has got the solution since April 2008. No need to wake up dead threads :)
Was This Post Helpful? 0
  • +
  • -

#6 HighOnTek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 25-June 11

Re: Reading and writing to and from command prompt

Posted 25 June 2011 - 04:04 PM

View Postsmohd, on 25 June 2011 - 03:31 PM, said:

I think he has got the solution since April 2008. No need to wake up dead threads :)


Your probably right. But I stumbled onto this thread through google search. It's good information, but incomplete.
Personally, I think the date of this posting is irrelevant. It's the information that lives on. No?

I'm sure others will one day google cmd prompt information for VB and stumble upon this thread and see that someone took the time to answer this guys questions.
Being a forum administrator myself, for a few forums... I too am plagued with members who think time has boundaries you can't cross. If you see a post, a question that's unanswered... Answer the damn thing for the next guy. ;)

Anyways, who really cares... Later. :)
Was This Post Helpful? 0
  • +
  • -

#7 kmxs  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 34
  • Joined: 22-June 11

Re: Reading and writing to and from command prompt

Posted 25 June 2011 - 10:59 PM

View PostHighOnTek, on 25 June 2011 - 04:04 PM, said:

View Postsmohd, on 25 June 2011 - 03:31 PM, said:

I think he has got the solution since April 2008. No need to wake up dead threads :)


Your probably right. But I stumbled onto this thread through google search. It's good information, but incomplete.
Personally, I think the date of this posting is irrelevant. It's the information that lives on. No?

I'm sure others will one day google cmd prompt information for VB and stumble upon this thread and see that someone took the time to answer this guys questions.
Being a forum administrator myself, for a few forums... I too am plagued with members who think time has boundaries you can't cross. If you see a post, a question that's unanswered... Answer the damn thing for the next guy. ;)

Anyways, who really cares... Later. :)


Norm?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1