Run already opened process in vb.net - vb.net

I have a windows form that, on the click of a button, runs a process (MATLAB) and executes a file.
Dim myProcesses() As Process
myProcesses = Process.GetProcessesByName("Matlab")
If myProcesses.Count > 0 Then
'~~~~ what goes here? ~~~~
Else
Dim startInfo As New ProcessStartInfo
startInfo.FileName = "C:\Program Files\MATLAB\R2011b\bin\matlab.exe"
startInfo.WorkingDirectory = MatlabDir 'MatlabDir is defined elsewhere
startInfo.Arguments = "matlab.exe -r test_plot2"
Process.Start(startInfo)
End If
The above code opens MATLAB and executes the script "test_plot2.m" if MATLAB isn't already open. But what do I write in the first IF statement, if MATLAB is already open, and all I want to do is run the file?
Thanks in advance.

Its supposed to be the same. I mean, it doesn't matter if its opened or not, unless the application (Matlab) manages something different, then you have to guess how. Have you tried just using the same code?
Example:
Dim startInfo As New ProcessStartInfo
startInfo.FileName = "notepad.exe"
startInfo.Arguments = "C:\temp\test.txt"
Process.Start(startInfo)
It doesn't matter if you have Notepad already opened or not.

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.

Writing to Command Line Not Working

An application I need to use (USB capture utility) has a .cmd version that I can call from my Visual Basic code. I am able to launch the application and put it in "command line mode" like this:
Public Class MyClass
Dim StreamWriteUtility As System.IO.StreamWriter
Dim StreamReadUtility As System.IO.StringReader
Dim ProcessInfo As ProcessStartInfo
Dim Process As Process
Public Sub StartUSBCapture(ByVal DataStorageLocation As String)
Dim ProcessInfo As ProcessStartInfo
Dim Process As New Process
ProcessInfo = New ProcessStartInfo("C:\FW_Qualification_Suite\data-center-windows\data-center\bin\datacenter.cmd", "-c ")
ProcessInfo.CreateNoWindow = True
ProcessInfo.UseShellExecute = False 'Must be changed if redirect set to True
ProcessInfo.RedirectStandardInput = True
Process = Process.Start(ProcessInfo)
SWUtility = Process.StandardInput
While True
SWUtility.WriteLine("run") 'Looping for test to ensure this isn't a timing issue
End While
End Sub
End Class
This launches the application and opens a separate command line window that should accept further commands (i.e., capture, run, stop, etc). However, I am having trouble getting those subsequent commands to show up in the command line window. I've tried redirecting the standard input of the process, but still nothing shows up in the console window.
Can anyone tell how I'm supposed to actually get these commands from my Visual Basic program into this application?

Execute DOS command within console application VB

I need to be able to execute a DOS command, such as 'ipconfig', using a command line application in Visual Basic. I can simply use start.process("CMD", "ipconfig"), but that opens a new instance of CMD. I want to be able to run a command like I would with CMD, using a console application, without opening another CMD window. Thanks!
You can use this to run the ipconfig command in hidden console window and redirect the output to local variable. From here you can manipulate it as needed:
Dim cmdProcess As New Process
With cmdProcess
.StartInfo = New ProcessStartInfo("cmd.exe", "/C ipconfig")
With .StartInfo
.CreateNoWindow = True
.UseShellExecute = False
.RedirectStandardOutput = True
End With
.Start()
.WaitForExit()
End With
' Read output to a string variable.
Dim ipconfigOutput As String = cmdProcess.StandardOutput.ReadToEnd

VB.Net Printing PDF Fails (But equivalent works from PowerShell)

I'm trying to write a small desktop app that just sits around and prints files as they get downloaded.
Sending a PDF file to the printer with PowerShell works fine and it prints:
.\AcroRd32.exe /N /T C:\Path\to\201402124_label.pdf "Brother QL-700"
However, doing the same in Visual Studio 2012 does not work. The Adobe Reader window opens with the label, and closes, but the file never show up at the printer to be printed. This doesn't make sense because this same code is currently working to send larger PDFs to a duplex printer in the same manner (just using a different printer saved in My.Settings):
For Each file As IO.FileInfo In files
If file.CreationTime > My.Settings.LastRunDate Then
MsgBox(file.Name)
Dim startInfo As New ProcessStartInfo
Dim proc As Process = New Process
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.Verb = "print"
startInfo.Arguments = My.Settings.LabelPrinterSettings.PrinterName.ToString
startInfo.UseShellExecute = True
startInfo.CreateNoWindow = True
startInfo.FileName = file.FullName
proc.StartInfo = startInfo
proc.Start()
proc.WaitForInputIdle()
proc.CloseMainWindow()
End If
Next
I can't figure out why doing this over the CLI/PowerShell works, but not inside VB.net
Why not simply use Process.Start(String, String)? Much more straight forward and clean. You can then use the returned Diagnostics.Process to run your other commands like WaitForInputIdle().
You would probably use something like this:
Dim proc As Process = Process.Start("AcroRd32.exe", _
String.Format("/N /T {0} ""{1}""", _
"C:\Path\to\201402124_label.pdf", "Brother QL-700")

How to start up an exe with parameters

I am new to Visual Basic. I am using VB Premium 2012.
Is there a way I can open/start up an exe file (not my app) with params. I know we can do it in batch coding using echo and stuff. Can it be done in vb?
I want to open up "app.exe" with params as "-login usernamehere passwordhere"
Try this
Dim pHelp As New ProcessStartInfo
pHelp.FileName = "YourApplication.exe"
pHelp.Arguments = "parameter1,parameter2"
pHelp.UseShellExecute = True
pHelp.WindowStyle = ProcessWindowStyle.Normal
Dim proc As Process = Process.Start(pHelp)
I hope this helps...