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.
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
Related
Hey i am programming in Visual Basic (VB.NET) and i am trying to run cmd commands without showing the cmd screen on the computer, I have the following code but i cant get it to hide it.. :(
Code:
Dim CMD As New Process
CMD.StartInfo.FileName = "cmd.exe"
CMD.StartInfo.UseShellExecute = False
CMD.StartInfo.RedirectStandardInput = True
CMD.StartInfo.RedirectStandardOutput = True
CMD.StartInfo.CreateNoWindow = True
CMD.Start()
Dim SW As System.IO.StreamWriter = CMD.StandardInput
Dim SR As System.IO.StreamReader = CMD.StandardOutput
SW.WriteLine("exit")
Process.Start("Cmd.exe", "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt")
Thread.Sleep(5000)
Ugly code aside, you can basically do it like this:
Dim CMD As New Process
CMD.StartInfo.FileName = "cmd.exe"
CMD.StartInfo.UseShellExecute = False
CMD.StartInfo.RedirectStandardInput = True
CMD.StartInfo.RedirectStandardOutput = True
CMD.StartInfo.CreateNoWindow = True
CMD.Start()
Dim SW As System.IO.StreamWriter = CMD.StandardInput
Dim SR As System.IO.StreamReader = CMD.StandardOutput
SW.WriteLine("exit")
Dim p2 As New Process
p2.StartInfo.FileName = "Cmd.exe"
p2.StartInfo.Arguments = "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt"
p2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p2.Start()
Thread.Sleep(5000)
Process.Start is a static method with a limited set of parameters for modifying its behavior.
So where you give the name of the .exe and the parameters via the static method
Process.Start("Cmd.exe", "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt")
, is similar to:
Dim p2 As New Process
p2.StartInfo.FileName = "Cmd.exe"
p2.StartInfo.Arguments = "/C systeminfo > C:\Users\" & Environment.UserName & "\Pictures\hello.txt",
p2.Start()
But by doing it like that you have many more options available, including the option to set the WindowStyle to hidden:
p2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
I'm trying to hide cmd windows with vb.net without success.
Dim oProcess As New Process()
Dim oStartInfo As New ProcessStartInfo("cmd.exe", " /c cscript ""%windir%\system32\slmgr.vbs"" /xpr | findstr ""The machine""")
oStartInfo.WindowStyle = ProcessWindowStyle.Minimized
oStartInfo.WindowStyle = ProcessWindowStyle.Hidden
oStartInfo.UseShellExecute = False
oStartInfo.RedirectStandardOutput = True
oProcess.StartInfo = oStartInfo
oProcess.Start()
Dim sOutput As String
Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
sOutput = oStreamReader.ReadToEnd()
End Using
TextBox4.Text = sOutput
any help please what is the mistake in my code ?
You must set the CreateNoWindow property as well.
oStartInfo.CreateNoWindow = True
Also, this is just redundant:
oStartInfo.WindowStyle = ProcessWindowStyle.Minimized '<-- Remove this line.
oStartInfo.WindowStyle = ProcessWindowStyle.Hidden
Setting WindowStyle to minimized won't affect anything since you change it to Hidden right after. When you use the = operator you replace a variable's or property's current value with a new one.
I'm encountering an issue with the Process.start() method in VB.Net.
I'm trying to execute an ipconfig /all and set a variable with the StandardOutput.ReadToEnd() method.
It works well if I execute the application locally, but when I place the EXE on a network share, my variable is not set with the result of the command.
It seems it cannot retrieve the output of the command.
Can you please help me?
Thank you in advance.
Here is my code:
Dim p As New Process
Dim psi As New ProcessStartInfo
psi.FileName = "cmd.exe"
psi.Arguments = "/c ipconfig /all"
psi.UseShellExecute = False
psi.RedirectStandardOutput = True
p.StartInfo = psi
p.Start()
Dim output As String = p.StandardOutput.ReadToEnd
p.WaitForExit()
MsgBox(output)
i've been created launcher application with vb.net, but when the target application began, there was an error "Can not Read Start.ini file".
I tried with this code
Dim psi As New ProcessStartInfo()
With psi
.FileName = sb.ToString
.UseShellExecute = True
End With
Process.Start(psi)
how to fix it ?
Daniel probably has it right. Try setting the WorkingDirectory property based off your FileName:
Dim psi As New ProcessStartInfo()
With psi
.FileName = sb.ToString
.WorkingDirectory = System.IO.Path.GetDirectoryName(.FileName)
.UseShellExecute = True
End With
Process.Start(psi)
I need to know, if you could help me, how to insert commands in vb then they run in cmd and i get the output.
I need to do "net localgroup Administradores a58465 /add" and get the error message if there is one.
Solution: `Dim myProcess As Process = New Process
Dim s As String
myProcess.StartInfo.FileName = "c:\windows\system32\cmd.exe"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.CreateNoWindow = True
myProcess.StartInfo.RedirectStandardInput = True
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.Start()
Dim sIn As System.IO.StreamWriter = myProcess.StandardInput
Dim sOut As System.IO.StreamReader = myProcess.StandardOutput
Dim sErr As System.IO.StreamReader = myProcess.StandardError
'sIn.AutoFlush = True
sIn.Write("cls" & System.Environment.NewLine)
sIn.Write("net user" & System.Environment.NewLine)
sIn.Write("exit" & System.Environment.NewLine)
s = sOut.ReadToEnd()
If Not myProcess.HasExited Then
myProcess.Kill()
End If
LB1.Text = s
LB1.Visible = True
sIn.Close()
sOut.Close()
sErr.Close()
myProcess.Close()`
Check out Process.Start. http://msdn.microsoft.com/en-us/library/0w4h05yb(v=vs.110).aspx
Also look for the ProcessStartInfo class, which will give you options on how to kick off an external process.
Console input and output can be made available to your program through ProcessStartInfo.