Run batch file in vb.net? - 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")

Related

Calling PowerShell Script from VB.net. Script can't find WinSCPnet.dll when it's in System32 directory Windows 10 VS2019

VB.NET Process Code I am using to invoke
Dim p As Process = New Process()
p.StartInfo.FileName = "PowerShell.exe"
p.StartInfo.Arguments = "C:\Users\dbashore\Documents\SFTP2.ps1"
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
p.StartInfo.UseShellExecute = False
p.StartInfo.CreateNoWindow = True
p.StartInfo.RedirectStandardError = True
p.Start()
p.WaitForExit()
Dim sStdErr_psRename As String = p.StandardError.ReadToEnd()
Console.WriteLine("Exit code : {0}", p.ExitCode)
Console.WriteLine("StdErr : {0}", sStdErr_psRename)
Here is the PowerShell Line that is failing
Add-Type -Path "C:\Windows\System32\WinSCPnet.dll"
Error says no such file exists.
When I copy the DLL to Mydocuments and change the path to point there the script runs.
Why can't it use .DLL's directly from system32. I'm sure it related to elevated permisions but i can't find anything about what property needs to be added/changed
The script runs fine from PoweshellISE
Thanks
Dean

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.

Invoking the .bat file by passing arguement from vb.net application

From Vb.Net Application, i am calling .bat file by passing SourceFile , DestinationFile. The .Bat file transfers source file to the destination folder. If i call .bat file directly from command prompt by passing arguements, file is getting transfered.
My code fails to transfer the file. I am not able to find the error where the code fails to execute the .bat file.
Dim strBatchFile As String = String.Empty
strBatchFile = AppDomain.CurrentDomain.BaseDirectory
strBatchFile = strBatchFile.Replace("\bin\Debug", "\ShellScript")
strBatchFile = strBatchFile & "callsfxcl.bat"
Dim proc As New System.Diagnostics.Process()
proc.StartInfo.UseShellExecute = False
proc.StartInfo.FileName = strBatchFile
proc.StartInfo.Arguments = String.Format("{0},{1}", strSourceFile, sSFTP)
proc.StartInfo.CreateNoWindow = True
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
proc.Start()
Batch files are run by cmd.exe. cmd /c batfile.bat. If you had of set UseShellExecute to true Windows would have done it for you. I suspect you didn't read the error.
From .NET help fror filename property.
The set of file types available to you depends in part on the value of the UseShellExecute property. If UseShellExecute is true, you are able to start any document and perform operations on the file, such as printing, with the Process component. When UseShellExecute is false, you are able to start only executables with the Process component.
Batch files are run by cmd.exe.
cmd /c batfile.bat.
So cmd is your process and /c c:\path\batch.bat is your arguments.

Run already opened process in 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.

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.