The NET
DOS command is an executable - net.exe . Therefore, it can be invoked as an executable via Process class. Here is a wrapper that returns a string containing the result of the command. Example usage:
retorno = ExecutaComando("net.exe", "start Audiosrv")
Here's the function in VisualBasic.NET:
Private Shared Function ExecutaComando(pComando As String, pParametros As String) As String
Dim _ret As String = ""
Dim procShell As New Process()
'Seu comando vai aqui.
procShell.StartInfo.FileName = pComando
'Os argumentos, aqui.
procShell.StartInfo.Arguments = pParametros
procShell.StartInfo.CreateNoWindow = True
procShell.StartInfo.RedirectStandardOutput = True
procShell.StartInfo.UseShellExecute = False
procShell.StartInfo.RedirectStandardError = True
procShell.Start()
Dim streamReader As New StreamReader(procShell.StandardOutput.BaseStream, procShell.StandardOutput.CurrentEncoding)
Do
Dim _line As String = streamReader.ReadLine()
If (IsNothing(_line)) Then Exit Do
_ret = _ret + _line + vbCrLf
Loop
streamReader.Close()
Return _ret
End Function
For example, use the following:
ExecutaComando("net.exe", "stop w3svc")
Returns the following value on a Windows 8.1 machine with IIS installed:
The World Wide Web Publishing Service service is stopping.
The World Wide Web Publishing Service service was stopped successfully.
You can use the same method with CMD. Example:
Process.Start("cmd", "/c del C:.txt")