Trouble with "Process's" please help!

M

Matt

Ok so I'm trying to run a simple dos command shell through a VB.NET
2005 program. The oddest thing is happening given the following piece
of code:


'============================================================
'=== VARIABLE DECLARATION AND INITIALIZATION ================
'============================================================
'---General Variables------------------------------------
Dim CMDProcess As Process = New Process
'---End General Variables--------------------------------
'---XLNT Shell Variables---------------------------------
CMDProcess.StartInfo.FileName = "cmd.exe"
CMDProcess.StartInfo.Arguments = "\c md C:\temp\temp"
'---End XLNT Shell Variables-----------------------------
'============================================================
'=== END VARIABLE DECLARATION AND INITIALIZATION ============
'============================================================

'============================================================
'=== EXECUTE JOB AND KEEP TRACK OF TIME =====================
'============================================================
CMDProcess.Start() <--- Does not work
Process.Start("cmd.exe", "/c md C:\temp\temp") <---- Works


If I create a new instance of the "Process" object and then try to
start it, the command will not work. But if I just simply call
Process.start the command works!

I need to be able to use the created object to do this job. If anyone
could offer any thoughts I would be very appreciative.

Thanks
Matt
 
G

Guest

When you say the command doesn't work...what is the behavior? Do you get any
errors, is there a message box, does an exception get thrown? And if
so...what is the message?

I’m wondering if the CMDProcess object is able to actually find cmd.exe.

Kim Greenlee
 
M

Miro

I checked my process start program and the only difference I an see that I
have and you have is the
Dim of the Processes in the begining. This works for me... dont know if you
want to see if it works for u.

my code is as follows:
Dim StartInfo As New ProcessStartInfo()
Dim myNewApp As New Process()

myNewApp.StartInfo.FileName = "C:\FILENAME.EXE"
myNewApp.StartInfo.Arguments = "/Q"
myNewApp.StartInfo.WorkingDirectory = "C:\Temp"
myNewApp.StartInfo.WindowStyle = ProcessWindowStyle.Normal

myNewApp.Start()


Miro
 
P

Phill W.

Matt said:
Dim CMDProcess As Process = New Process
CMDProcess.StartInfo.FileName = "cmd.exe"
CMDProcess.StartInfo.Arguments = "\c md C:\temp\temp"
CMDProcess.Start() <--- Does not work
Process.Start("cmd.exe", "/c md C:\temp\temp") <---- Works

Have a play with the StartInfo UseShellExecute property.
IIRC, that has some bearing on how things work.

Failing that, try "%COMSPEC%" instead of "cmd.exe"

Or, better still, looking at what you're actually doing:

System.IO.Directory.CreateDirectory( "C:\temp\temp" )

HTH,
Phill W.
 
M

Matt

I'm not trying to create a directory, that was just something I tossed
in there to see if it would work. What I'm ultimately trying to do is
to run dos commands from a VB.NET app.

I was under the impression given the following code.

Dim StartInfo As New ProcessStartInfo()
Dim myNewApp As New Process()


myNewApp.StartInfo.FileName = "cmd.exe"
myNewApp.StartInfo.Arguments = "\c md C:\temp\temp"
myNewApp.StartInfo.WorkingDirectory = "C:\Temp"
myNewApp.StartInfo.WindowStyle = ProcessWindowStyle.Normal
myNewApp.Start()

That the "Arguments" part of it would indicate what dos command I
wanted to execute. When I run the following code I get a dos window
showing up but it doesn't make a directory. How do I get it to execute
it?

The reason I want to do things this way is so that I can measure when
the process ends, whereas if I just did it like this...

Process.Start("cmd.exe", "/c " & Command)

I can't tell.
 
P

Phill W.

Matt said:
I'm not trying to create a directory, that was just something I tossed
in there to see if it would work.

Fair enough.
myNewApp.StartInfo.FileName = "cmd.exe"
myNewApp.StartInfo.Arguments = "\c md C:\temp\temp"

Shouldn't that be "/c md C:\temp\temp" ?
The reason I want to do things this way is so that I can measure when
the process ends, whereas if I just did it like this...

Process.Start("cmd.exe", "/c " & Command)

I can't tell.

That's because when you kick off a /Windows/ process from /DOS/, DOS
doesn't wait for it to finish /unless/ you use the START command with
the "/WAIT" flag, effectively

START /WAIT winprog1.exe a b c d
IF ERRORLEVEL 1 GOTO Boom

The VB.Net way to start and wait for a Process would be more like this:

With myNewApp.StartInfo
.FileName = "program1.exe"
.Arguments = "a b c"
.Start()
.WaitForExit( ... ' can't remember the arguments; sorry
End With

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top