Execute DOS command within console application VB - vb.net

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

Related

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

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?

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 - execute vbscript using process with 'run as administrator'

I have a vbscript file that I want to run in my vb.net application. In the application, the script 'must' use a process. The reason is that it's actually being called from a windows process. In order for me to execute the vbscript manually, I have to right-click on the shortcut and select 'run as administrator'.
How can I emulate this using vb.net? Currently the execution works because I tested it with it only creating a text file. Also, I want to assume that the user is in the administrators group and don't want them to have to login each time because it will execute every minute.
My code:
Dim foo As New System.Diagnostics.Process
foo.StartInfo.WorkingDirectory = "c:\"
foo.StartInfo.RedirectStandardOutput = True
foo.StartInfo.FileName = "cmd.exe"
foo.StartInfo.Arguments = "%comspec% /C cscript.exe //B //Nologo C:\aaa\test.vbs"
foo.StartInfo.UseShellExecute = False
foo.StartInfo.CreateNoWindow = True
foo.Start()
foo.WaitForExit()
foo.Dispose()
Thanks.
The class ProcessStartInfo has two properties that can be used to define the user name that will run your script
ProcessStartInfo.UserName
ProcessStartInfo.Password
Please note as from MSDN:
The WorkingDirectory property must be set if UserName and Password are provided. If the property is not set, the default working directory is %SYSTEMROOT%\system32.
The Password property is of type SecureString. This class need a special initialization code like this:
' Of course doing this will render the secure string totally 'insecure'
Dim pass As String = "Password"
Dim passString As SecureString = New SecureString()
For Each c As Char In pass
passString.AppendChar(ch)
Next
So your code could be changed in this way
Dim foo As New System.Diagnostics.Process
foo.StartInfo.WorkingDirectory = "c:\"
foo.StartInfo.RedirectStandardOutput = True
foo.StartInfo.FileName = "cmd.exe"
foo.StartInfo.Arguments = "%comspec% /C cscript.exe //B //Nologo C:\aaa\test.vbs"
foo.StartInfo.UseShellExecute = False
foo.StartInfo.CreateNoWindow = True
foo.StartInfo.UserName = "administrator"
foo.StartInfo.Password = passString
foo.Start()
foo.WaitForExit()
foo.Dispose()