git is not recognized when running a batch job in Visual Basic application - vb.net

I have a VB app that I want to pull down a repo upon a button click.
Upon button click, my project runs a .cmd file (Frontend.cmd) that I have saved in my project resources folder:
cd ..
cd ..
cd Users\Username\Documents
git clone https://gitusername:token#github.com/repofilepath.git
PAUSE
Running Frontend.cmd normally works without issue, the repo is pulled down successfully. However, when running the .cmd file through the VB application, I get the git is not recognized error.
'git' is not recognized as an internal or external command,
operable program or batch file.
I have added C:\Program Files\Git\bin\ and C:\Program Files\Git\cmd\ paths as suggested in this answer:
'git' is not recognized as an internal or external command
Is there another path I must add?
Here is my button click code if needed:
Dim output As String
output = My.Resources.Frontend
Dim programPath As String = Path.GetTempPath & “\” & program & ".cmd"
Using sw As StreamWriter = New StreamWriter(programPath)
sw.Write(output)
End Using
If (My.Computer.FileSystem.FileExists(programPath)) Then
Dim procStartInfo As New ProcessStartInfo
Dim procExecuting As New Process
With procStartInfo
.UseShellExecute = True
.FileName = programPath
.WindowStyle = ProcessWindowStyle.Normal 'use .hide to hide the process window
.Verb = "runas" 'run as admin
End With
MsgBox("Please press Yes when system ask permissions", MsgBoxStyle.Information, "myProgram")
procExecuting = Process.Start(procStartInfo)
End If

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

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

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.

Running CMD command on windows service

I have created a windows service that requires executing an EXE file with the CMD process. I have used the following code:
Str = "C:\PCounter\Staff\account.exe CHARGE " & Name & " " & Amount & " TO" & Id
Dim procStartInfo As New System.Diagnostics.ProcessStartInfo(Str)
procStartInfo.RedirectStandardOutput = True
procStartInfo.UseShellExecute = False
procStartInfo.CreateNoWindow = True
Dim proc As New System.Diagnostics.Process
proc.StartInfo = procStartInfo
proc.Start()
proc.Dispose()
However the above code will return
system cannot find the file specified
I have tried same code on the Windows form, and its works fine. To make sure the path is correct I have added a text file in the same location as EXE file, and load the content of the text file in the service. It works fine.
I can't think of anything else; I really would appreciate it if you can help me on this.
ProcessStartInfo has two properties. One for the executable to run, and the other for the arguments to pass to the executable. The symantics for the Arguments property are the exact same as the command line.
You can not include the arguments in the same property as the executable. They must be separated.
Create service:
sc create Vm-Symantec04 binPath= "\"C:\App32\VMware\VMware Workstation\vmrun.exe\" -T ws start \"D:\VM\Sym04\Sym04.vmx\" nogui" depend= "VMAuthdService/VMnetDHCP/VMUSBArbService/VMware NAT Service" start= auto
Delete service:
sc delete Vm-Symantec04