20

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 value of the executed code so I don't really know how to.

This program is a service but has access to the disk no problem as I already log other information. The whole service has multiple threads so I must also make sure that when the file is written it's not already accessed.

3 Answers 3

38

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
Sign up to request clarification or add additional context in comments.

2 Comments

@DT, the link supplied in your comment is no longer valid - do you know of any other confirmations for this behavior?
but the code still works...
12

Just pipe the output to a text file?

MyCommand > "c:\file.txt"

Then read the file.

3 Comments

Actually, I did found out the solution last night before reading this and it's quite close. I will use >> instead because I want to append the results each time, but thank you anyway.
I forgot to mention you should consider MyCommand > "c:\file.txt 2>&1 if you also want to capture error reports in the file. By default error outputs are not included in the file.
@mark : i'm not the downvoter, but i can explain : i hate having to use "save-to-file" "read-file". It's slow, bad and uggly
1
    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.

2 Comments

Part of your code snipet has slipped out of the code box
Naming the batch file after an existing command will result in an endless loop. I leanrt it a hard way :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.