Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Novice question

Status
Not open for further replies.

tg2003

IS-IT--Management
Feb 6, 2003
270
0
0
IL
Hi,

I'm VB6 veteran, but VB.NET rookie.

What is considered the best to run some system command or 3rd party command from within my .NET application?

In VB6 I used
Code:
CreateObject("wscript.shell") and cmd /c

I found somewhere about
Code:
System.Diagnostics.Process.Start("CMD.exe",strCmdLine);

Is it true? Or may something else?

Thanks in advance!
 
I personally use process as you can easily code to wait for your launched process to finish before you continue running the rest of your code.




Hope this helps.

Matt
[rockband]
 
How can I use the same "cmd" windows for all the duration of the running app?
I ask it since my app executes "cmd" actions a few times.

And how can I avoid of opening the black cmd screen in the background?

Thanks in advance!
 
To have better control (i mean not use the default settings) create an object or drag 'n drop a "Process" component (Components tab | Process).

If you do not use the component, you can write code:

Code:
Dim p As New System.Diagnostics.Process
'OR Dim p As New System.Diagnostics.ProcessStartInfo
p.CreateNoWindow = False
' p. ...
p.Start(...)

The Process 'thing' has been mentioned many times in here. Have a search too.
 
As for the "same cmd window"... i dont know if it is possible.

You can play a little with the 'p.RedirectStandardOutput = True' and the 'p.StandardOutput.ReadToEnd' streamReader's String.

You should not show any more the cmd window (p.CreateNoWindow = True) and write the streamReader Stream to a textbox. It isnt difficult. ;)
 
With a lot of help from this and other VB support areas, I use this function to execute DOS commands, and wait until they are done:

Code:
Public Function RunDos(ByVal strWorkingDir As String, ByVal strCommand As String, ByVal strArgs As String) As String
        prcDosProcess.StartInfo.WorkingDirectory = strWorkingDir
        prcDosProcess.StartInfo.FileName = strCommand
        prcDosProcess.StartInfo.Arguments = strArgs
        prcDosProcess.Start()
        prcDosProcess.PriorityClass = ProcessPriorityClass.High
        RunDos = prcDosProcess.StandardOutput.ReadToEnd
        prcDosProcess.WaitForExit()
        prcDosProcess.Close()
    End Function

This requires that you drag a System.Diagnostics.Process component (listed as Process under Components) onto your form, and name it prcDosProcess. From the properties sheet you can configure whether or not a DOS window is opened, among many other configurable options.

You would then call the function something like this:

Code:
RunDos("c:\Directory","copy","source target")

or

Code:
strDosOutput = RunDos("c:\Directory","copy","source target")

which would both execute 'copy source target' in the directory 'c:\directory'. The second method should return the dos output as the string strDosOutput, but I don't think it actually works.

You can see that I still like to prefix names with an idea of what they are, just to help me keep track of what I'm up to. I'm only an intermittent programmer, so I need to remind myself what I was up to last time I opened the code.

Hope this helps.
--
JB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top