CoomandLine Automation

fadi

Junior Contributor
Joined
Jul 4, 2003
Messages
209
Location
Lebanon
Hello everybody,
i wanted to ask how can i automate a commandline prompt. As an example, i want to be able to execute the command 'cd windows' or 'copy file1 file2' using vb.net rather than writing it directly. i know i can use the classes in IO namespace but i need to write dos commands exactly.
thanks
 
You can redirect the standard input and output of console applications (such as cmd.exe) when you start the process. Check out the RedirectStandardInput property of the ProcessStartInfo class.

Once you have started a process with this property set to true, you can use myProcess.StandardInput.WriteLine("cd blah") etc to control it.
 
Hello,
i tried to do what u said but i received the following error:
" StandardIn has not been redirected."
here is my code:
Dim myProcessStartInfo As New System.Diagnostics.ProcessStartInfo()

myProcessStartInfo.RedirectStandardInput = True

Dim myProcess As New System.Diagnostics.Process()
myProcess.StartInfo = myProcessStartInfo
myProcess.Start("cmd")
myProcess.StandardInput.WriteLine("cd windows")

and the error is raised on the last line.
thanks
 
The following works for me:

Visual Basic:
        Dim ps As New ProcessStartInfo("cmd.exe")
        ps.RedirectStandardInput = True
        ps.UseShellExecute = False

        Dim process As Process = process.Start(ps)

        process.StandardInput.WriteLine("dir")
 
fair enough,it works for me too. i needed not to show the command prompt at all but i managed to do that by setting the property CreateNoWindow to true.
thanks alot anyways....
 
Back
Top