VB.NET Process.Start Arguments not passing to CMD - vb.net

This is my first post here, so please go easy, I also have a good knowledge of VB.
I am making an app that is to create a WiFi hotspot at a click of a button so that I can use my laptop as a WiFi extender for my devices like my phone, however, I am doing this with command prompt. This is my code so far:
Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Dim startInfo As New ProcessStartInfo("cmd")
startInfo.WindowStyle = ProcessWindowStyle.Minimized
startInfo.Arguments = "netsh show wlan drivers"
Process.Start(startInfo)
End Sub
The problem is, it's not passing the arguments. Cmd launches fine but receives nothing.
Things I have tried:
1. Using process.start and ProcessStartInfo
2. Change working directory
3. Send arguments after cmd has launched (after process.start)
4. Change target framework
5. Run in x86 and x64
6. Run as administrator
7. Trying other commands such as "color 2f". Failed.
Any help would be greatly appreciated!
Edit: Not even the WindowStyle argument was passed.

You need to add
startInfo.Arguments = "/C netsh wlan show drivers"
without that flag (/C) the CMD command exits immediately and nothing is executed
In any case your command is wrong. The correct syntax is
netsh wlan show drivers
You could change your code to capture the output of the command in this way
Dim startInfo As New ProcessStartInfo("cmd")
startInfo.WindowStyle = ProcessWindowStyle.Minimized
startInfo.Arguments = "/C netsh wlan show drivers"
startInfo.RedirectStandardOutput = true
startInfo.UseShellExecute = false
startInfo.CreateNoWindow = true
Dim p = Process.Start(startInfo)
Dim result = p.StandardOutput.ReadToEnd()
p.Close()

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.

In .net vb is there a way to know if Yes or No was chosen in a SHELL command when User Account control appears

I am using .net vb ; I usually shell a program to run an update of my software; the problem I am facing is I don't know whether the user has chosen Yes or No when prompted to run the program in an alleviated mode i.e When the User Account control box appears.
How can I know whether Yes or No was chosen after the shell command was issued. .
This is my current code - I use runas for a higher windows then XP
Public Shared Sub UpdateElevated()
Try
'Shell(C_Drive & "\POS-UPD\pos-upd.exe", AppWinStyle.NormalFocus, False)
Dim osInfo As OperatingSystem = Environment.OSVersion
Dim startInfo As ProcessStartInfo = New ProcessStartInfo()
startInfo.UseShellExecute = True
startInfo.WorkingDirectory = C_Drive & "\POS-UPD"
startInfo.FileName = C_Drive & "\POS-UPD\pos-upd.exe"
If osInfo.Version.Major >= 6 Then startInfo.Verb = "runas"
startInfo.WindowStyle = ProcessWindowStyle.Normal
Process.Start(startInfo)
Catch ex As Exception
End Try
End Sub

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")

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.