3

I'm trying to run CMD silently, but each time I get an error. Could someone please tell me where I'm going wrong?

Dim myProcess As Process 
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden 
myProcess.StartInfo.CreateNoWindow = True 
myProcess.StartInfo.FileName = ("cmd.exe" & CmdStr) 
myProcess.Start() 

CmdStr is already a string to do certain things that I want in the application.

StarLordBlair
  • 573
  • 1
  • 11
  • 27
  • Well on the code it says that I a null exception might occur. But I am changing someone elses code and they have an error trap that just has a message box – StarLordBlair Sep 18 '12 at 13:16

1 Answers1

10

I suppose your cmdStr is a string with parameters for CMD.
If so you need to use the Arguments property of StartInfo.
You get a Null Exception on the myProcess variable because it is never instatiated with new. You could create a ProcessStartInfo var to use with the static Process.Start method and set the UseShellExecute to False

Dim startInfo As New ProcessStartInfo("CMD.EXE")
startInfo.WindowStyle = ProcessWindowStyle.Hidden     
startInfo.CreateNoWindow = True 
startInfo.UseShellExecute = False
startInfo.Arguments = CmdStr
Process.Start(startInfo)  

or edit your code to add

myProcess = new Process() 

before using the var myProcess

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thanks Steve, The parts I was missing were the New (hadn't set the instance of the object and the time and the argument section), your unedited version worked after I added in new. Thanks Very Much – StarLordBlair Sep 18 '12 at 13:26