hide cmd in vb.net not working - vb.net

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.

Related

How to run CMD commands and hide it

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

use the cmd command result in case statment

im trying to get the windows license status from cmd by running this code
Dim oProcess As New Process()
Dim oStartInfo As New ProcessStartInfo("cmd.exe", " /c cscript ""%windir%\system32\slmgr.vbs"" /xpr | findstr ""The machine""")
oStartInfo.CreateNoWindow = True
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
Select Case sOutput
Case Is = "The machine is permanently activated."
TextBox4.Text = "activated"
End Select
the code is working okay but the result that appears on my textbox4 is : The machine is permanently activated. i need to take this result to case statement
Select Case sOutput
Case Is = "The machine is permanently activated."
TextBox4.Text = "activated"
all i need if i get The machine is permanently activated. status from cmd then show in textbox4 word ( activated )
It could be that the output contains a space or a line break at the end. Call Trim() on the string to remove any leading or trailing spaces and/or line breaks.
Select Case sOutput.Trim()

Using cmd in VB, using commands and receiving output

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.

How to Copy CMD Results into Textbox on VB.Net Project

I'm working on a Project by VB.net and i'm using CMD to excute commands i want to Know how to copy the Results of the CMD into a textbox on my Main Form
Take a look at the accepted answer here: Get the output of a shell Command in VB.net. That is probably what you need.
Also, here is a version of the code that puts the result into the textbox:
Dim oProcess As New Process()
Dim oStartInfo As New ProcessStartInfo("ApplicationName.exe", "arguments")
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
txtOutput.Text = sOutput 'txtOutput being the output textbox.
I hope this helps.
Dim proc As New Process
proc.StartInfo.FileName = "C:\ipconfig.bat"
proc.StartInfo.UseShellExecute = False
proc.StartInfo.RedirectStandardOutput = True
proc.Start()
proc.WaitForExit()
Dim output() As String = proc.StandardOutput.ReadToEnd.Split(CChar(vbLf))
For Each ln As String In output
RichTextBox1.AppendText(ln & vbNewLine)
lstScan.Items.Add(ln & vbNewLine)
Next
'Created a file in batch with 2 lines as shown below:
echo off
ipconfig
' save this file as ipconfig.bat or whatever name u want.
' if you didn't want that you could use any command on there like this:
echo off
dir/s
or
echo off
cd\
dir/s
pause

How to format drive in FAT 16 format using VB.NET without user interaction

How to format drive in FAT 16 format using VB.NET without user interaction
That would be the VB.NET translation of this C# answer to a similar question :
Dim allDrives As DriveInfo() = DriveInfo.GetDrives()
For Each d As DriveInfo In allDrives
If d.IsReady AndAlso (d.DriveType = DriveType.Removable) Then
Dim startInfo As New ProcessStartInfo()
startInfo.FileName = "format.com"
startInfo.Arguments = "/fs:FAT /v:MyVolume /q " & d.Name.Remove(2)
startInfo.UseShellExecute = False
startInfo.CreateNoWindow = True
startInfo.RedirectStandardOutput = True
startInfo.RedirectStandardInput = True
Dim p As Process = Process.Start(startInfo)
Dim processInputStream As StreamWriter = p.StandardInput
processInputStream.Write(vbCr & vbLf)
p.WaitForExit()
End If
Next