VB.NET read standard output - vb.net

From my application I need to run a command and parse the output. I can do this with no problem but I don't want the command to be displayed. I hoped WindowStyle = ProcessWindowStyle.Hidden would work but it doesn't. Take the sample code below for example. It works fine but the command window still visibly opens and closes very quickly and I need it to never show its ugly face. How can I fix this?
Dim myprocess As New Process
Dim lines As String = ""
With myprocess
.StartInfo.FileName = "C:\Windows\System32\cmd.exe"
.StartInfo.Arguments = "/c ipconfig"
.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
.StartInfo.RedirectStandardOutput = True
.StartInfo.UseShellExecute = False
.Start()
End With
lines = myprocess.StandardOutput.ReadToEnd
MsgBox(lines)

Try setting CreateNoWindow to True too.
If what you are trying to achieve is to find the IP address(es) of the local machine, there are more direct ways of doing it.

Include
.StartInfo.CreateNoWindow = True

Try these settings in tandem:
.CreateNoWindow = True
.UseShellExecute = False
See http://blogs.msdn.com/b/jmstall/archive/2006/09/28/createnowindow.aspx for more details.

Related

How to get full output from Python script inside VB.net application?

I've successfully coded it to run the Python script and return the output. But unfortunately, it only returns the last line. I was wondering if there was a way to get the full output from the script?
If anyone is interested, here is the code to run the script in the background and return the final line of output.
Dim proc As Process = New Process
proc.StartInfo.FileName = "C:\Program Files\Python38\python.exe" 'Default Python Installation
proc.StartInfo.Arguments = "RunImageChecker.py arg1 arg2" 'Python script and arguments
proc.StartInfo.UseShellExecute = False 'required for redirect.
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
proc.StartInfo.CreateNoWindow = True
proc.StartInfo.RedirectStandardOutput = True 'captures output from commandprompt.
proc.Start()
AddHandler proc.OutputDataReceived, AddressOf proccess_OutputDataReceived
proc.BeginOutputReadLine()
proc.WaitForExit()
MsgBox(Value)
TextBox1.Text = Value
Note: The python script has to be in the same folder as your program or it won't work. I wish there was a way around this, but I'm unable to find one.

How to properly hide process?

How to properly hide process?
I m using these five lines of code, for another process, but somehow it is still not hidden, or to say it is still visible on screen. Do you know what line of code am i missing?
What I have tried:
Dim myprocess As Process = New Process()
myprocess.StartInfo.FileName = "SW.exe"
myprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
myprocess.StartInfo.UseShellExecute = True
myprocess.StartInfo.CreateNoWindow = True
myprocess.Start()
myprocess.WaitForExit()
The documentation for the CreateNoWindow property has this to say:
If the UseShellExecute property is true or the UserName and Password
properties are not null, the CreateNoWindow property value is ignored
and a new window is created.
The answer to your question is exactly where you should have expected it to be. ALWAYS read the documentation.

VB.net has process finished running

I have this code which prints files in vb.net:
' Create object, passing in text
Dim psi As New ProcessStartInfo
psi.UseShellExecute = True
psi.Verb = "print"
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.FileName = fi.FullName
Process.Start(psi)
i want to be able to run more code once the printing process has completed, how can i see if it has completed?
I think you most likely want to wait for the process to finish. Try this:
Dim p = Process.Start("calc.exe")
p.WaitForExit()
If you really don't want to wait but just check for completion try this:
If p.HasExited() Then
' do something
End If

How to obtain ip address automatically programmatically

Dim args As String
args="netsh wlan interface ip set address """+adptrname+""" dhcp"
Dim proc As New System.Diagnostics.Process()
proc.StartInfo.FileName = "netsh"
proc.StartInfo.Verb="RunAs"
proc.StartInfo.CreateNoWindow = true
proc.StartInfo.RedirectStandardOutput = True
proc.StartInfo.Arguments = args
proc.StartInfo.UseShellExecute = False
proc.Start()
My.Computer.FileSystem.WriteAllText(MainForm.filePath,proc.StandardOutput.ReadToEnd(), True)
proc.Close()
MsgBox(args)
i wrote the above piece of code to change my wlan adapter to obtain ip address automatically but it still remains on the old ip configuration inspite of the output saying that dhcp is enabled. Can someone please tell where I am going wrong.
OK i tried to do this on the comments, but i kept on finding errors... so here a corrected version of your code:
'Where does your adptrname come from?
Dim adptrname As String = "wlan0" 'i.E.
Dim args As String
' Note that i removed 'netsh' from args
' And the notation to add the addapter name (in VB we use & instead of +)
args="netsh int ipv4 set address name=""" & adptrname & """ source=dhcp"
Dim proc As New System.Diagnostics.Process()
proc.StartInfo.FileName = "netsh"
proc.StartInfo.Verb="RunAs"
proc.StartInfo.CreateNoWindow = true
proc.StartInfo.RedirectStandardOutput = True
proc.StartInfo.RedirectStandardError = True ' maybe you want to read this, too
proc.StartInfo.Arguments = args
proc.StartInfo.UseShellExecute = False
proc.Start()
' This is still bad, but we keep it for now
My.Computer.FileSystem.WriteAllText(MainForm.filePath, proc.StandardOutput.ReadToEnd(), True)
My.Computer.FileSystem.WriteAllText(MainForm.filePath, proc.StandardError.ReadToEnd(), True)
' Normally not necessery, but no way we do proc.close() here
proc.WaitForExit()
' ok, this shoud have shown you until now that it didn't work...
MsgBox(args)
Read the comments, try the corrections on your own code and see if it runs.
Edit on the Parameters
Also i am thinking it won't work with your arguments...
Try it like this instead:
netsh int ipv4 set address name="Wireless Connection" source=dhcp
I built this from the help and it works on my machine (Win7x64-en_US)

Run batch file in vb.net?

How can I run a batch from from within vb.net?
You can use the Process class to run a batch file
Dim psi As New ProcessStartInfo("Path TO Batch File")
psi.RedirectStandardError = True
psi.RedirectStandardOutput = True
psi.CreateNoWindow = False
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.UseShellExecute = False
Dim process As Process = Process.Start(psi)
Here is a simple and straight forward method:
System.Diagnostics.Process.Start("c:\batch.bat")
The best way is to use the Process.Start and pass the path to the batch file
Process.Start(pathToBatchFile)
The easiest way if you know the exact location of the file is
System.Diagnostics.Process.Start("c:\test\file.bat")
In Visual Studio the file must exist in the /bin/debug or /bin/release depending on your current build configuration
System.Diagnostics.Process.Start("test.bat")