Search code examples
vb.netshellcommand-linecmdhidden

hide cmd in vb.net not working


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 ?


Solution

  • 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.