• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 407
  • Last Modified:

Running DOS Commands from Code

Hi all,

Im using this line of code:
     Shell("CMD /K BUTIL -CLONE MTASLIN.8 MTASLIN.B")
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)
0
kiranboi
Asked:
kiranboi
  • 2
1 Solution
 
imnajamCommented:
Hi kiranboi,
>      Shell("CMD /K BUTIL -CLONE MTASLIN.8 MTASLIN.B")
use "&"

Cheers!
0
 
Éric MoreauSenior .Net ConsultantCommented:
Hi kiranboi,

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

Cheers!
0
 
imnajamCommented:
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")
0
 
Bob LearnedCommented:
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

    proc.Start()

    If timeOut = -1 Then
      proc.WaitForExit()
    Else
      proc.WaitForExit(timeOut)
    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)

Bob
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now