TF.exe doesn't prompt for checkin confirmation when executed directly, without using the operating system shell to start the process - vb.net

I'm using TF.exe command-line for some simple TFS commands such as getting the latest version of the files (tf.exe vc get), checking out some files (tf.exe vc checkout) and then checking them in at the end of use (tf.exe vc checkin).
After some tests I've noticed that when I run the checkin command through Windows Command Prompt (cmd.exe), the prompt window is displayed asking for confirmation before the check-in command is done:
C:\Program Files\Microsoft Visual Studio\2022\Community
\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer>
tf.exe vc checkin "C:\TfsTestProject\test.txt" /comment:"Checked via command-line."
For my project that was a good thing, displaying the prompt window for check-in confirmation.
However, when I started the TF command through code I found a problem: I have to start the TF.exe process redirecting the standard output, so I can display what's happening when the command is executed, and for that I have to set process.StartInfo.UseShellExecute = False. But when I do that, the prompt window is no longer displayed, as if I was using the /noprompt parameter (and I'm not using it).
So, when I use the code below the prompt window is stil displayed, as in the picture, but I cannot receive the standard output stream feedback:
Dim p = New Process()
' I removed the full path for clarity, but that's:
' "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\TF.exe"
p.StartInfo.FileName = "TF.exe"
p.StartInfo.Arguments = $"vc checkin ""{testFilePath}"" /comment:""Checked via code."""
p.StartInfo.UseShellExecute = True
p.StartInfo.CreateNoWindow = True
p.Start()
p.WaitForExit()
But, when I use this other code, the one that I need, the prompt window for check-in confirmation is no longer displayed, even though I'm not using /noprompt parameter:
Dim p = New Process()
' I removed the full path for clarity, but that's:
' "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\TF.exe"
p.StartInfo.FileName = "TF.exe"
p.StartInfo.Arguments = $"vc checkin ""{testFilePath}"" /comment:""Checked via code."""
p.StartInfo.UseShellExecute = False ' <- That's the problem, apparently.
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.CreateNoWindow = True
p.EnableRaisingEvents = True
AddHandler p.OutputDataReceived, Sub(s, e) Console.WriteLine(e.Data)
p.Start()
p.BeginOutputReadLine()
p.WaitForExit()
I couldn't find any information about that behaviour, maybe it's just a bug?

Shame on me, I've done some research before posting the question and found nothing, but, just after posting it I found this:
c# - How can I capture tf.exe stderr without stopping its dialog prompts? - Stack Overflow
This problem is old, since that question is from 10 years ago, facing the same "bug". Apparently there is an undocumented parameter /prompt that makes the prompt window to be displayed even though the TF command is not executed through shell.
So this code (with undocumented /prompt parameter) worked:
Dim p = New Process()
' I removed the full path for clarity, but that's:
' "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\TF.exe"
p.StartInfo.FileName = "TF.exe"
' With undocumented /prompt parameter the prompt window is
' displayed even when the command is not executed through shell.
p.StartInfo.Arguments = $"vc checkin ""{testFilePath}"" /prompt /comment:""Checked via code."""
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.CreateNoWindow = True
p.EnableRaisingEvents = True
AddHandler p.OutputDataReceived, Sub(s, e) Console.WriteLine(e.Data)
p.Start()
p.BeginOutputReadLine()
p.WaitForExit()
And after that question and answer from long time ago I've tested a little more and found that I was wrong, the problem is not about not starting the command from shell, the problem really happens when the streams (output or error) are redirected. When you just set UseShellExecute = False, without redirecting, the prompt window is still displayed.

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

git is not recognized when running a batch job in Visual Basic application

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

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

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.

Console Application output is not shown

I have read several questions in StackOverflow about redirecting output/errors
from application but It could not help me.
I have developed an executable in C# code. It recovers software from system registry
and call a webservice to save it to our database.
Inside the executable we are printing messages with
Console.WriteLine("Message from Executable");
On the other hand we have a CRM application and I have written two win forms
that call psexec.exe with the desired parameters and this .exe is remotely
copied and launched on target machine.
If you use direct psexec call with that parameters in cmd.exe, we can see the psexec
execution banners and OUR executable output
Something like:
PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010
Mark Russinovich Sysinternals - www.sysinternals.com
Enterprise software # 2012
Message from application
Starting task in machine "XXXXXXX"
Recovering software list from registry
Program.exe exited on MACHINE with error code 0.
When I make the psexec call with .NET, I cant recover my application output, It
just shows:
PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010
Mark Russinovich Sysinternals - www.sysinternals.com
Program.exe exited on MACHINE with error code 0.
I am assigning to Process class a delegate who is recovering async
data from process, but this information, is not there.
I paste some code to see If you can find the reason my C# Console application
output does not appear:
The command I launch is:
Dim CmdExe As String = "\\#MAQUINA# -u #USUARIO# -p #CLAVE# -s -c -f """ + executionPath + """"
Parametes -s (Run as system) -c (Copy local file to remote system) -f (force overwrite)
Private Sub LanzarProceso()
CheckForIllegalCrossThreadCalls = False
Dim Proceso As New Process
AddHandler Proceso.OutputDataReceived, AddressOf CallbackProcesoAsync
AddHandler Proceso.ErrorDataReceived, AddressOf ErrorDataReceivedAsync
Dim startInfo As New ProcessStartInfo
startInfo.FileName = execFile
startInfo.Arguments = CmdExe.Replace("#MAQUINA#", txtFiltroMaquina.Text).Replace _
("#USUARIO#", txtUsuario.Text.Trim).Replace _
("#CLAVE#", txtClave.Text.Trim)
Proceso.EnableRaisingEvents = True
startInfo.UseShellExecute = False
startInfo.RedirectStandardOutput = True
startInfo.RedirectStandardError = True
startInfo.CreateNoWindow = False
Proceso.StartInfo = startInfo
Proceso.Start()
Proceso.BeginOutputReadLine()
Proceso.BeginErrorReadLine()
Proceso.WaitForExit()
End Sub
Delegates handling data:
Private Sub CallbackProcesoAsync(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)
If Not args.Data Is Nothing AndAlso Not String.IsNullOrEmpty(args.Data) Then
If Not listtask.InvokeRequired Then
listtask.Items.Add(args.Data.ToString)
Else
Dim d As New TextToControl(AddressOf AddToControl)
listtask.Invoke(d, args.Data.ToString)
End If
End If
End Sub
Private Sub ErrorDataReceivedAsync(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)
If Not args.Data Is Nothing AndAlso Not String.IsNullOrEmpty(args.Data) Then
If Not listtask.InvokeRequired Then
listtask.Items.Add(args.Data.ToString)
Else
Dim d As New TextToControl(AddressOf AddToControl)
listtask.Invoke(d, args.Data.ToString)
End If
End If
End Sub
I checked the program is finishing correctly. THe c# executable has
Enviroment.Exit(0);
at the end of the execution
Ok, the best workaround I found to get all the output between psexec and my c# application
after execution is:
Dim CmdExe As String ="\#MAQUINA# -u #USUARIO# -p #CLAVE# -s -c
-f """ + executionPath + """ > outputFileCode.txt"
This allow me to parse the standard output without having to change anything or calling
my program directly, because I can`t do it.
What you see in the console window is a mix of output sent to console by both psexec as well as the child process. You can intercept the output from psexec, but to do the same for the child would require the interception code in the psexec, which apparently it does not have
Try to remove the intermediary - call your executable directly