Run DOS commands in Visual Studio

2

I need to know which command in Visual Studio 2013 runs the command to stop a Windows system service. In DOS I do this:

net stop Audiosrv
netstart Audiosrv
exit

The other question is how to execute the DOS commands inside Visual Studio 2013 - Visual Basic. Calling or not SHELL cmd.exe

I tried to do so but it is not working because it is not calling the commands DOS .

Private Sub ExecutarComando()

 Dim comando As String = "/del C:.txt"

Shell ("cmd.exe /c " & comando)

 End Sub
    
asked by anonymous 20.07.2014 / 13:57

2 answers

0

Problem solved, I solved it like this:

To start

Shell("net start WSearch") 'Windows Search Start'

To Stop

Shell("net stop Wsearch")'Windows Search Stop'
    
28.09.2014 / 14:54
3

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")
    
20.07.2014 / 16:05