Get the output of a shell Command in VB.net

后端 未结 3 442
闹比i
闹比i 2020-12-05 07:51

I have a VB.net program in which I call the Shell function. I would like to get the text output that is produced from this code in a file. However, this is not the return va

相关标签:
3条回答
  • 2020-12-05 08:20
        Dim proc As New Process
    
        proc.StartInfo.FileName = "C:\ipconfig.bat"   
        proc.StartInfo.UseShellExecute = False
        proc.StartInfo.RedirectStandardOutput = True
        proc.Start()
        proc.WaitForExit()
    
        Dim output() As String = proc.StandardOutput.ReadToEnd.Split(CChar(vbLf))
        For Each ln As String In output
            RichTextBox1.AppendText(ln & vbNewLine)
            lstScan.Items.Add(ln & vbNewLine)
        Next
    

    ======================================================================= create a batch file in two lines as shown below:

        echo off
        ipconfig
    

    ' make sure you save this batch file as ipconfig.bat or whatever name u decide to pick but make sure u put dot bat at the end of it.

    0 讨论(0)
  • 2020-12-05 08:23

    You won't be able to capture the output from Shell.

    You will need to change this to a process and you will need to capture the the Standard Output (and possibly Error) streams from the process.

    Here is an example:

            Dim oProcess As New Process()
            Dim oStartInfo As New ProcessStartInfo("ApplicationName.exe", "arguments")
            oStartInfo.UseShellExecute = False
            oStartInfo.RedirectStandardOutput = True
            oProcess.StartInfo = oStartInfo
            oProcess.Start()
    
            Dim sOutput As String
            Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
                sOutput = oStreamReader.ReadToEnd()
            End Using
            Console.WriteLine(sOutput)
    

    To get the standard error:

    'Add this next to standard output redirect
     oStartInfo.RedirectStandardError = True
    
    'Add this below
    Using oStreamReader As System.IO.StreamReader = checkOut.StandardError
            sOutput = oStreamReader.ReadToEnd()
    End Using
    
    0 讨论(0)
  • 2020-12-05 08:31

    Just pipe the output to a text file?

    MyCommand > "c:\file.txt"
    

    Then read the file.

    0 讨论(0)
提交回复
热议问题