Running DOS Commands from Code

Posted on 2006-05-25
Last Modified: 2012-06-21
Hi all,

Im using this line of code:
to run an application called BUTIL.exe through a DOS window, however, before I use the DOS command above I need to be able to specify which directory the DOS promt should go to. Can someone tell me how to write more than 1 line of DOS code using Shell form VB.NET

Thanks :o)
Question by:kiranboi
    LVL 9

    Accepted Solution

    Hi kiranboi,
    use "&"

    LVL 69

    Expert Comment

    by:Éric Moreau
    Hi kiranboi,

    In VB.Net, you should use Process.Start instead of Shell

    LVL 9

    Expert Comment

    for example

    ipconfig release & ipconfig renew

    you can also loop it as "cd c:\test\program & butil -clone mtaslin.8 mtaslin.b"

         Shell("CMD /K cd c:\testing\abc & BUTIL -CLONE MTASLIN.8 MTASLIN.B")
    LVL 96

    Expert Comment

    by:Bob Learned
    I agree with emoreau, the Process class has much better control over running DOS commands.

    Here is a class to highlight:

    Imports System.Diagnostics

    Public Class DOSCommander

      Public Shared Function RunCommand(ByVal command As String, ByVal arguments As String, ByVal createWindow As Boolean, ByVal timeOut As Integer, ByRef output As String) As Integer

        Dim proc As New Process

        proc.StartInfo.Arguments = "/c " & command & " " & arguments
        proc.StartInfo.CreateNoWindow = Not createWindow
        proc.StartInfo.ErrorDialog = True
        proc.StartInfo.FileName = "cmd.exe"
        proc.StartInfo.RedirectStandardError = False
        proc.StartInfo.RedirectStandardInput = False
        proc.StartInfo.RedirectStandardOutput = True
        proc.StartInfo.UseShellExecute = False

        If createWindow Then
          proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
        End If


        If timeOut = -1 Then
        End If

        output = proc.StandardOutput.ReadToEnd()

        Return proc.ExitCode

      End Function

    End Class

    Sample usage:
      Dim output As String = ""
      DOSCommander.RunCommand("dir", "c:\windows", False, 3000, output)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now