Running Command Line statements

A

Atley

I am looking for a way to run DOS-type commands from within a VB.net 2003
application.

I also need to be able to answer questions (ie 'Are You Sure') that the
command line may require.

Thanks,

Atley
 
A

Atley

I don't see how that allows me to execute a DOS command, I tried a simple
one like 'dir c:\windows'
and it put it in the console window, but I did not see it execute.

What am I doing wrong?
 
C

Chris Dunaway

I don't see how that allows me to execute a DOS command, I tried a simple
one like 'dir c:\windows'
and it put it in the console window, but I did not see it execute.

What am I doing wrong?

Look at the System.diagnostics.Process class. You can call the
Process.Start method to start a DOS command. You can optionally, redirect
the output of the DOS command. When you receive a certain output such as
"Are You Sure?" You can send the appropriate response. Search Google for
examples on redirecting input and you should find a link with an example.

Just FYI (and I'm sure you already know this) most DOS commands that ask
"Are You Sure?" also provide a switch /y for answering yes.

Regards,
 
A

Atley

I have been trying to use the Shell method, but I can't seem to make the
command window activate the way you can with calc or word or notepad... It
is making it impossible to use the SendKeys function, which would be
ideal... I will take a look at the suggestion you gave me, but the Shell
looks like it would be the way to go, if I could just get the focus to the
window without errors.

The code is simple and works for everything except a DOS command window.

Dim myLng As Integer = Shell("NET SHARE tshare /DELETE",
AppWinStyle.NormalFocus)
AppActivate(myLng)
SendKeys.SendWait("y")
SendKeys.SendWait("{Enter}")

That should work and does if you substitute notepad, word, etc instead of
any DOS command, for some reason Windows cannot find the Command Window to
set focus.

I am using NET SHARE to unshare a directory on a server so that some file
maintence can be done at night, even though some users still leave their
computers attached to the share. I cannot find a /y to answer affermative
and ECHO y| doesn't work for it either, or I would be using a simple batch
file to do this.
 
C

Chris Dunaway

H

Herfried K. Wagner [MVP]

* Chris Dunaway said:
I'm not sure that cmd.exe has a window you can find. You may have noticed
that the cmd.exe window (when run on XP) does not have the XP look and
feel. I think that is because it is not a standard window, but I could be
wrong.

It will have a window, but I am not sure if the according 'Process'
object has a 'MainWindowHandle' (I seriously doubt it has). Maybe
playing around with Spy++ (which comes with VS.NET) helps.

:)

This will redirect the console output into a Windows Forms application.
I am not sure if that's what the OP wants to do.
 
C

Chris Dunaway

This will redirect the console output into a Windows Forms application.
I am not sure if that's what the OP wants to do.

The OP said he wants to "run DOS-type commands from within a VB.net 2003
application." and "answer questions (ie 'Are You Sure') that the
command line may require."

If he redirects the DOS programs output to a stream, shouldn't he be able
to scan the stream for the prompt (i.e. 'Are You Sure?') and send an
appropriate answer back to the DOS programs standard input? I'll have to
try that myself and see if it can be done.
 
H

Herfried K. Wagner [MVP]

Chris,

* Chris Dunaway said:
The OP said he wants to "run DOS-type commands from within a VB.net 2003
application." and "answer questions (ie 'Are You Sure') that the
command line may require."

If he redirects the DOS programs output to a stream, shouldn't he be able
to scan the stream for the prompt (i.e. 'Are You Sure?') and send an
appropriate answer back to the DOS programs standard input? I'll have to
try that myself and see if it can be done.

Maybe it will work. If would be great if you post your experiences
here.
 
A

Atley

Answer:
Set commands in a batch file, this will give the window a title you can rely
on and send responses too... At least it is working for me.

Thanks for all of your help.
Atley
 

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