How to properly hide process? - vb.net

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.

Related

VB.Net Process.Start - Verb

I want my vb.net Program to launch notepad and elevate the credentials however there is no prompt and the program just opens.
Dim process As System.Diagnostics.Process = Nothing
Dim processStartInfo As System.Diagnostics.ProcessStartInfo
processStartInfo = New System.Diagnostics.ProcessStartInfo()
processStartInfo.FileName = "notepad.exe"
processStartInfo.Verb = "runas"
processStartInfo.Arguments = ""
processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
processStartInfo.UseShellExecute = True
process = System.Diagnostics.Process.Start(processStartInfo)
There's a lot of unnecessary code there. You're using two lines to declare and set your two variables when you only need one each time. You're setting the FileName property explicitly when you can pass an argument to the constructor and you're also setting three properties to their default values. I just stripped it down to the bare minimum:
Dim psi As New ProcessStartInfo("notepad.exe") With {.Verb = "runas"}
Dim proc = Process.Start(psi)
When I ran that code from a new WinForms app with just a Button on the form and I got a UAC prompt as expected.

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

Get output of external console app to RichTextBox

I used the VB.Net Shell() command to start a console app that I haven't created. I want to get the lines from that console to a RichTextBox in my form. It should be possible because I have seen many apps that do this. Please provide some information or any program that might help me. I tried to see if the external app creates log files, but it does not.
Here's how I started. What should I add, and where, to return the output?
Try
writer.WriteLine(RichTextBox1.Text)
writer.Close()
Shell(CurDir() & "\yelloeye.exe .\new.demo")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Here's an example using the RedirectStandardOutput property as mentioned by #Plutonix which will ping SO and display the results in a RichTextBox. Just replace the .exe name and arguments (if any) to suit your needs.
Private Sub ShellToRTB()
Dim p As New Process()
p.StartInfo.FileName = "ping.exe"
p.StartInfo.Arguments = "www.stackoverflow.com"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.Start()
RichTextBox1.Text = p.StandardOutput.ReadToEnd()
p.WaitForExit()
End Sub
Result:

Kill all processes in a listBox? (VB.NET)

I'm trying to create a small productivity program to keep myself focused when programming; specifically, to close any processes that might distract me from doing my job. I'm writing it in VB.NET for simplicity.
What is the easiest way to kill all processes listed in a listBox? I already know how to add the processes to my listBox with this code:
Dim newProc As New OpenFileDialog
'// Settings for the open file dialog. (I like how I use ' to start the comment, but // so I recognize it! :)
newProc.Filter = "Executable files (*.exe)|*.exe"
newProc.FileName = "..choose a file.."
newProc.Multiselect = True
newProc.CheckFileExists = True
newProc.CheckPathExists = True
newProc.AutoUpgradeEnabled = True
newProc.AddExtension = True
If (newProc.ShowDialog = Windows.Forms.DialogResult.OK) Then
ListBox1.Items.AddRange(newProc.SafeFileNames)
End If
This adds the processes to the listBox very neatly and all, exactly how I want it. I have a timer that gets enabled with the press of a button that should close all processes in the listBox, but I'm unsure what I should use. Can I get some help? :(
Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("notepad")
For Each p As Process In pProcess
p.Kill()
Next
You can try the above. Please view this link for further infomation.

VB.NET read standard output

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.