Run DOS commands in Visual Studio

Asked

Viewed 2,983 times

2

I need to know which command on Visual Studio 2013 executes the command to stop a Windows system service. No DOS I do like this:

net stop Audiosrv
netstart Audiosrv
exit

The other question is how to execute the commands DOS 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:\1.txt"

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

 End Sub

2 answers

3

THE COMMAND OF NET is an executable - net.exe. Thus, it can be invoked as an executable via class Process. Here is a wrapper that returns a string containing the result of the command. Example of use:

retorno = ExecutaComando("net.exe", "start Audiosrv")

Here is 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, the following use:

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:\1.txt")
  • Looking here I think you don’t understand . Net start is a DOS command , Audiosrv is the audio service as example put .

  • @Mauricioperroni I understood, yes. Several DOS commands are actually executable as in the case of NET. Feel free to test the above code.

  • Jewel I’ll test now .

  • Gave error , look at the error that gave ....

  • Link to error . http://imgur.com/AEJPmtc

  • It is a function, not a complete class; you need to insert this function into a class/module.

  • That’s exactly what you sent me ... .

  • I tried to put in a module class it is returning me an error ; I will post to you and to the Administrator. link to the class/module error: http://imgur.com/C9nxphT @Onosendai

  • Like I settled with SHELL

  • Private Sub Button1_click(Sender As Object, and As Eventargs) Handles Button1.Click Shell("net stop Teamviewer9") End Sub .

  • Ready , when runs the solution in my VB it is simple to just run with the right button , administrator permissions and goes smoothly .

Show 6 more comments

0


Problem solved , I resolved so :

To start

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

To Stop

Shell("net stop Wsearch")'Windows Search Stop'
  • Mauricio, if this is the answer that worked, mark it as correct by clicking on the icon on the left, below the answer score.

  • I can’t get my friend, I don’t have score yet ;

  • Help me there ; mark as answer please do this kindness to me; Great day for you . @bfavaretto

  • You should be able to. The question is yours, only you can mark the answer. Even I who am moderator can’t do it for others.

  • @bfavaretto because my topic is negative ? .

  • I don’t know what led someone to vote against, they might have found it confusing, that research was lacking... I have no way of knowing. For accepting the reply, see instructions here: http://meta.pt.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta. If this does not work, let me know what message the system has given.

  • @bfavaretto I don’t know what to do , just wanted to help the community and mark as done ; because it worked here and solved a problem that was giving me a headache . I didn’t want other users to go through what I went through .

  • I appreciate you wanting to help, that’s the spirit. It’s always good to record the answer that worked. Don’t call the -1 up there, it’s part of it, everybody takes those vows once in a while.

  • @bfavaretto so it is ; I’m already 32 years old . I’m halfway through life , I’m a pacifist.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.