Running Dos Commands from VB.net

Associate
Joined
8 Mar 2007
Posts
2,176
Location
between here and there
hey all,

I need to run loads of dos commands from within vb.net and display each line as it's run to a listbox.

I would also like a progress bar as well, and since i know the amount of dos commands it needs to run that shouldn't be so hard. (but I'll get to that later)

I'm very new to vb.net and I'd like to write it my self (a nice little project:p)

If someone could point me in the direction of loading a dos prompt and running the "net use \\ipaddress /user:username password" command which wait untill it has finished to display the output to my listbox that would be very helpful.

cheers guys!
 
Do you want to display it ? or would you rather it run in the background silently? if silently..

im sure .net groupies would rather you didnt but personally

Shell('a really interesting command');

does the job imo
 
tbh, I'd rather it display each line as it runs, that way the user can see that it is doing something and not just hanging.

is the whole, loading of a "command prompt" and running commands on it that I'm having trouble with.

cheers
 
Last edited:
Hang on sorry i didnt read your post :P i just replyed to title + 1st line. my bad

so where do you want the output to be ? you want a command promt window to pop up? in that case can still use shell statement. just use something like

Shell(cmd, AppWinStyle.NormalFocus)

instead


*edit* please ignore the ; at the end of shell command in my first post. been doing PASCAL all day.. :p

*edit2*

if however you want to output to something on your form you might be better of using process.start class

Code:
Dim args As String = your string \\ip and all that
Dim process As New Process()
Dim FileName As String = "net use"
Dim Arguments As String = args
process.StartInfo.UseShellExecute = False
process.StartInfo.RedirectStandardOutput = True
process.StartInfo.RedirectStandardError = True
process.StartInfo.CreateNoWindow = False
process.StartInfo.FileName = FileName
process.StartInfo.Arguments = Arguments
process.StartInfo.WorkingDirectory = WorkingDirectory
process.Start()
Dim output As String = process.StandardOutput.ReadToEnd()

im fairly new to all this as well so i cant promise that code will work but you get the idea
 
Last edited:
umm, no probably better if the prompt window doesn't display, (it will only confuse the poor end user!!):rolleyes:


is it something similar "Running Dos Commands from VB.net" ??

and how would get the command you wanted to run in there?
 
Last edited:
Okay that example i made might not have been so great

maybe this source code (working) might explain it a bit better

Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim args As String = "/all"
        Dim process As New Process()
        Dim WorkingDirectory As String = "C:\"
        Dim FileName As String = "ipconfig"
        Dim Arguments As String = args
        process.StartInfo.UseShellExecute = False
        process.StartInfo.RedirectStandardOutput = True
        process.StartInfo.RedirectStandardError = True
        process.StartInfo.CreateNoWindow = True
        process.StartInfo.FileName = FileName
        process.StartInfo.Arguments = Arguments
        process.StartInfo.WorkingDirectory = WorkingDirectory
        process.Start()
        Dim output As String = process.StandardOutput.ReadToEnd()
        Label1.Text = output

    End Sub
End Class
 
nice,

ok, seem to work but it displays on one line,

screen.jpg


i have tried cr and lf, but it has made no difference.

cheers again for your time!
 
Last edited:
Im just looking into that myself - i never use listbox controls so im not quite sure what to do. either need to find a way to split it over all lines or somehow use

Dim output As String = process.StandardOutput.ReadLine()

but im not really sure.

how about using a simlpe lable to display data? just a thought. either way i dunno what to do about that one line issue! really sorry! right i gotta go to bed anyway - if you work it out post here im curious myself :)

good luck need any help just gimme a shout (not that im actualy any good)
 
Assign the process.StandardOutput to a StreamReader. This was you can keep reading 1 line at a time untill you reach the end of the stream. You will need to add a new line youself i.e.
Code:
Console.Writer(StreamReader.ReadLine() & vBCrLf)
There is another thread where someone asked the same thing. More examples were posted in there.

EDIT: Getting the progress of the batch files will be difficult as you are not in control of them. You could only really show progress of how many batch files have been executed.

TrUz
 
Bored at work so here you go. :)
Code:
        Dim p As New Process()

        With p
            .StartInfo.Arguments = "/all"
            .StartInfo.CreateNoWindow = True
            .StartInfo.FileName = "ipconfig"
            .StartInfo.RedirectStandardOutput = True
            .StartInfo.UseShellExecute = False
            .Start()

            Dim reader As StreamReader = .StandardOutput

            While (Not reader.EndOfStream)
                Me.TextBox1.AppendText(String.Format("{0}", reader.ReadLine() & vbCrLf))
            End While

            .WaitForExit()
        End With
TrUz
 
hey truz, thanks.

That other thread was started by me, but it got a dit off topic and i had to change the way i was thinking about it so i started this one instead.

I have managed to get it to work with the code you provided the other day

Code:
Imports System.io


Public Class Formtestme
   
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles testmerun.Click
        Dim args As String = "/all"
        Dim process As New Process()
        Dim WorkingDirectory As String = "C:\"
        Dim FileName As String = "ipconfig"
        Dim Arguments As String = args
        process.StartInfo.UseShellExecute = False
        process.StartInfo.RedirectStandardOutput = True
        process.StartInfo.RedirectStandardError = True
        process.StartInfo.CreateNoWindow = True
        process.StartInfo.FileName = FileName
        process.StartInfo.Arguments = Arguments
        process.StartInfo.WorkingDirectory = WorkingDirectory
        process.Start()
        Dim std_out As StreamReader = process.StandardOutput()
        While (Not std_out.EndOfStream)
            Dim temp As String = std_out.ReadLine()
            Formstatus.statuslist.HorizontalScrollbar = True
            Formstatus.statuslist.Items.Add(temp)
            Formprogress.Close()
            Formstatus.Show()
        End While
    End Sub
End Class

nice!!

Since i have about 15 functions (button) in my program, and since each button used to call a batch file that i'm now going to write in to vb.net, (each bacth file has about 15 lines of code)

Is there a way of making a function so that i can save on the amount of typing??

then just "call dos" withe the set parameters??
 
create a new sub:
Code:
    Private Sub RUN_PROCESS(ByVal fileName As String, ByVal arguments As String)
        Dim p As New Process()

        With p
            .StartInfo.Arguments = arguments
            .StartInfo.CreateNoWindow = True
            .StartInfo.FileName = fileName
            .StartInfo.RedirectStandardOutput = True
            .StartInfo.UseShellExecute = False
            .Start()

            Dim reader As StreamReader = .StandardOutput

            While (Not reader.EndOfStream)
                Me.TextBox1.AppendText(String.Format("{0}", reader.ReadLine() & vbCrLf))
            End While

            .WaitForExit()
        End With
    End Sub
Then with each of you buttons call it like so:
Code:
Me.RUN_PROCESS("ipconfig", "/all")
TrUz
 
hey cool, thats well nice!!

only thing is, if i change the command from "ipconfig" to say "dir" or "net use" and either take out the arguments or use them it errors on the ".start()" saying that the file can't be found.

any ideas?
 
you'd of thought so, but no.

i get "file not found" error on the .start() line.

I've tried servel different dos commands in there but the only one that works id ipconfig, weired, no???
 
why is that every thing you suggest works, :) but anything i add or change doesn't!!!!:mad:

that dir doesn't now work, but i need this to run, net use, copy, start pstools, if - else and other commands as well.

I tried
Code:
  ("cmd", "net use \\ipaddress /user:user password")
but got "M" displayed. i then tried copy c:\test.txt c:\temp\nice.txt but got "microsift xp version 5.1.2600, c:\documents\administrator....the path to vb.net debug."

????? GRRRRRRRR:confused:

cheers for all your help by the way i wouldn't have enev got this far without it!;)
 
Do not forget /c. ;)
Code:
Me.RUN_PROCESS("cmd", "/c net use M: \\Alpha\Development /PERSISTENT:NO")
TrUz
 
sweet,

that seems to have done it.

what does that "/c" do?? it's not a dos command is it?

I reakon I should be able to write in my dos commands now.

Oh, while i think of it, will the dos working directroy stay the same between commands or will it change back to the working directroy of my vb program?
 
open a DOS command and type cmd /? you will get a full list of available switches and explanations. :)

The working directory will probably revert so you might want to set it each time just to be sure.

TrUz
 
Back
Top Bottom