How to call a Windows shell command using VB.NET

1 Answer

0 votes
Module WindowsShellCommand

    Sub Main()
        Dim p As New Process()
        p.StartInfo.FileName = "cmd.exe"
        p.StartInfo.Arguments = "/c dir"
        p.StartInfo.RedirectStandardOutput = True
        p.StartInfo.RedirectStandardError = True
        p.StartInfo.UseShellExecute = False
        p.StartInfo.CreateNoWindow = True

        p.Start()

        Dim output As String = p.StandardOutput.ReadToEnd()
        Dim err As String = p.StandardError.ReadToEnd()

        p.WaitForExit()

        Console.WriteLine("OUTPUT:")
        Console.WriteLine(output)

        Console.WriteLine("ERRORS:")
        Console.WriteLine(err)


    End Sub

End Module



' run:
'
' OUTPUT:
' Volume in drive C has no label.
' Volume Serial Number Is 81DD-BCS1

' Directory of C:\The3DPlatform\VB_ConsoleApp1\bin\Debug\net7.0

' 01/08/2026  10:42 AM    <DIR>          .
' 01/08/2026  10:42 AM    <DIR>          ..
' 04/22/2025  04:41 PM               434 VB_ConsoleApp1.deps.json
' 01/08/2026  10:42 AM             5,120 VB_ConsoleApp1.dll
' 01/08/2026  10:42 AM           154,624 VB_ConsoleApp1.exe
' 01/08/2026  10:42 AM            10,980 VB_ConsoleApp1.pdb
' 04/22/2025  04:41 PM               147 VB_ConsoleApp1.runtimeconfig.json
'              5 File(s)        171,305 bytes
'              2 Dir(s)  325,944,745,984 bytes free
'
' ERRORS:
'

 



answered Jan 8 by avibootz

Related questions

...