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!
 
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:
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:
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:
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??
 
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!;)
 
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?
 
cheers dude.

could i add another argument in there to bring in the ip address from a text box???

i was thinking something like,

Code:
Private Sub RUN_PROCESS(ByVal fileName As String, ByVal arguments As String, ByVal arguments2 As String)


with,

Code:
Me.RUN_PROCESS("cmd", "/c net use \\", TextBoxcleanIP.Text + " /user:mine spottydog")
 
i have used the following code,

dos.dos2("cmd", "/c copy c:\", Test.Text + " c:\temp\nice2.txt")

(i also moved the sub's to a moudal page for ease of use ;))

but it seems to display (below) when it should say "1 file(s) copied"

blap.jpg


these files listed are on my c:\ but there is a lot more there than that, so it's not like it's doing "dir" instead.

Most other commands that i've tested seem to be working ok and display just how i'd like them to, but "copy" is the most important command i'm going to use.

any ideas as I'm stumped!:confused:

edit; i've used "dos.dos1("cmd", "/c copy c:\test.txt c:\temp\nice2.txt") and that works, must be something to do with bringing in the file name from the textbox.
 
Last edited:
here it is;

Code:
Imports System.io
Module dos
    Public Sub dos1(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)
                Formstatus.statuslist.Items.Add(String.Format("{0}", reader.ReadLine))
                Formstatus.statuslist.HorizontalScrollbar = True
                Formstatus.Show()
            End While

            .WaitForExit()
        End With
    End Sub

cheers dude ;)
 
umm,

dos.dos2("cmd", "/c copy c:\" + Test.Text + " c:\temp\nice2.txt")

has to be put in like,

dos.dos2("cmd", "/c copy c:\" +Test.Text + " c:\temp\nice2.txt") - (vb just changes it)

but that just make is hang.
 
with the "+test.text +" it throws an double exception error.

seems odd that it works when the file is written in but when pulled though from the text box, it fails. I did think that it had something to do with the spaces in the line but I've checked that and the syntax of the dos command should be right.

god i love this programming lark!!!
 
TrUz,

you where right,

dos.dos2("cmd", "/c copy c:\" + Test.Text + " c:\temp\nice2.txt")

does work i had;

dos.dos2("cmd", "/c copy c:\", +Test.Text + " c:\temp\nice2.txt") - and that was never gonna work.

a fresh set of eyes an' all.

thanks again for your time.
 
Back
Top Bottom