Problem with Shell() function in vb.net with complex command string

M

micahstrasser

I have been trying for days to send a command to the command prompt
through the shell() function in vb.net. For some reason it is not
working. Here is the code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim command As String

command = "systeminfo > C:\temp\sysinfo.txt"

Shell(command, AppWinStyle.MaximizedFocus)
'Process.Start(command)

End Sub

As you can tell I have tried it with Shell() and process.start(). I
have also tried to use double quotes throughout different parts of the
string. When the code executes the command window flashes but the file
is not created. When I paste the command in the command window
everything works fine. Can anyone help?
 
D

David Browne

I have been trying for days to send a command to the command prompt
through the shell() function in vb.net. For some reason it is not
working. Here is the code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim command As String

command = "systeminfo > C:\temp\sysinfo.txt"

Shell(command, AppWinStyle.MaximizedFocus)
'Process.Start(command)

End Sub

As you can tell I have tried it with Shell() and process.start(). I
have also tried to use double quotes throughout different parts of the
string. When the code executes the command window flashes but the file
is not created. When I paste the command in the command window
everything works fine. Can anyone help?

This is a meta-ness problem. The ">" is not an argument to systeminfo, but
rather to the command interpreter. Anyway, to make this work, invoke the
command shell explicitly, telling it to execute the command "systeminfo >
C:\temp\sysinfo.txt" and then exit:

Sub Main()
Dim command As String
command = "cmd /c systeminfo > C:\temp\sysinfo.txt"
Shell(command, AppWinStyle.MaximizedFocus)
'Process.Start(command)
End Sub

You could also just redirect standard output for the program, and skip the
file.

David
 

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