VB icon

RunDosCommand Function

Email
Submitted on: 1/4/2015 10:39:00 PM
By: Igor Krupitsky (from psc cd)  
Level: Beginner
User Rating: By 2 Users
Compatibility: VB.NET
Views: 1604
 
     Run MS DOS Command and get results back in VB.NET
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: RunDosCommand Function
// Description:Run MS DOS Command and get results back in VB.NET
// By: Igor Krupitsky (from psc cd)
//**************************************

'Example Of Use 
'sRet = RunDosCommand("CScript.exe C:\MyScript.vbs", 10)
Function RunDosCommand(ByVal sCommandText As String, Optional ByVal iTimeOutSec As Integer = 1) As String
	Dim iPos As Integer = sCommandText.IndexOf(" ")
	Dim sFileName As String
	Dim sArguments As String = ""
	If iPos = -1 Then
		sFileName = sCommandText
	Else
		sFileName = sCommandText.Substring(0, iPos)
		sArguments = sCommandText.Substring(iPos + 1)
	End If
	Dim sRet As String
	Dim oProcess As Process = New Process
	oProcess.StartInfo.UseShellExecute = False
	oProcess.StartInfo.RedirectStandardOutput = True
	oProcess.StartInfo.FileName = sFileName
	oProcess.StartInfo.Arguments = sArguments
	oProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
	oProcess.StartInfo.CreateNoWindow = True
	oProcess.Start()
	oProcess.WaitForExit(1000 * iTimeOutSec)
	If Not oProcess.HasExited Then
		oProcess.Kill()
		Return "Timeout"
	End If
	sRet = oProcess.StandardOutput.ReadToEnd()
	If oProcess.ExitCode <> 0 And sRet = "" Then
		sRet = "ExitCode: " & oProcess.ExitCode
	End If
	oProcess.Close()
	Return sRet
End Function


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.