Pass Arguments to EXE - vb.net

I have a button with this code
Process.Start("start system\now.exe")
Now I'd like to pass arguments from the Texbox but I dont know how . I've tried :
Process.Start("system\now.exe -connect -ip 127.0.0.1 -PORT 910 " & "-USER " & TextBox1.Text & "-PWD " & TextBox1.Text " -serial x4r7680")
But does not seem to work. Any idea guys?

You need to pass in a ProcessStartInfo object with the arguments. See the page in the MSDN docs for info on the ProcessStartInfo class.
Based on a cursory reading of the document, your example would look like this:
Dim startInfo as ProcessStartInfo("system\now.exe")
startInfo.Arguments = "-connect -ip 127.0.0.1 -PORT 910 " & "-USER " & TextBox1.Text & "-PWD " & TextBox1.Text " -serial x4r7680"
Process.Start(startInfo)

Look at the ProcessStartInfo class. An example:
Dim startInfo As New ProcessStartInfo("IExplore.exe")
startInfo.WindowStyle = ProcessWindowStyle.Minimized
Process.Start(startInfo)
startInfo.Arguments = "www.northwindtraders.com"
Process.Start(startInfo)
but you may also be able to use another overload of the Start method to pass your command line arguments. Again an example:
' Start a Web page using a browser associated with .html and .asp files.
Process.Start("IExplore.exe", "C:\myPath\myFile.htm")
Process.Start("IExplore.exe", "C:\myPath\myFile.asp")
or in your case:
Process.Start("system\now.exe","-connect -ip 127.0.0.1 -PORT 910 " & "-USER " & TextBox1.Text & "-PWD " & TextBox1.Text " -serial x4r7680")
or
Dim startInfo As New ProcessStartInfo("system\now.exe")
Process.Start(startInfo)
startInfo.Arguments = "-connect -ip 127.0.0.1 -PORT 910 " & "-USER " & TextBox1.Text & "-PWD " & TextBox1.Text " -serial x4r7680"
Process.Start(startInfo)
you need to be careful if TextBox1.Text contains a space as (depending on how your program parses the command line options) you might end up parsing them incorrectly unless you surround them in quotes.

Ok, Im sure the problem you have are the quotes "" of the parameters.
This example is using argument parameters.
Try with this:
Process.Start("system\now.exe", """" & "-connect -ip 127.0.0.1 -PORT 910 " & "-USER " & lblSubirLlamadasTimeInterval.Text & "-PWD " & lblSubirLlamadasTimeInterval.Text & "-serial x4r7680""")

Related

How can I see the results and how can the windows Close

I have this code, and I´m calling one Java program to pass the aurguments, that I select in VB.
My issues is:
The cmd windows open, but I can't see what the program is doing, I only see on the window the arguments that are pass, and when finish, the window don't close.
enter image description here
and when program stop, the windows don't close.
enter image description here
This is the result if I run by command line
enter image description here
txbSendCommand.Text = "java -jar FACTEMICLI-2.5.16-33194-cmdClient.jar " & "-i " & TextBox1.Text & "" & " -n " & stNIF & " -p " & stPassword & " -a " & iAno & " -m " & iMes & " -op " & stvalidar & stFicheiroEscolhido & " -o c:\saft\outputfile.xml"
Dim stcaminhoexterno As String
If stErrorMessage = False Then
Dim app As New ProcessStartInfo("cmd.exe") With {.RedirectStandardInput = True, .UseShellExecute = False, .CreateNoWindow = False}
Dim myProcess As New Process
myProcess = Process.Start(app)
Dim arguments As String = txbSendCommand.Text
myProcess.StandardInput.WriteLine(arguments)
myProcess.Close()
End If
How can I see the results in the external windows, or better even in a windows or something that I can have in the form.
How Can the window close after the process is done

How to use multiple commands with path references in command prompt

I'm new to this forum, so please correct me if I'm asking this the wrong way or not specific enough..
While coding in VB.NET I'm trying to pass multiple commands, and an argument containing a reference to a path:
Dim p As New Process
Dim pi As New ProcessStartInfo
pi.Arguments = " " & "/K """ & "C:\program files\gdal\gdalshell.bat" & """ & " & "cd C:\program files\gdal" & _
" & " + "gdal_translate" + " -of Jpeg -outsize 2000 2000 """ & "D:\box sync\my box (907070)\RIS_RHDHV_Overgang\GDAL\test2.xml" & """ "
pi.FileName = "C:\windows\syswow64\cmd.exe"
p.StartInfo = pi
p.Start()
The command prompt returns:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
I did some research on the matter and found:
vb.net How to pass a string with spaces to the command line
This however still doesn't seem to solve the problem. When I execute the following code, it runs without issues:
Dim p As New Process
Dim pi As New ProcessStartInfo
pi.Arguments = " " & "/K """ & "C:\program files\gdal\gdalshell.bat" & """ & " & "cd C:\program files\gdal" & _
" & " + "gdal_translate"
pi.FileName = "C:\windows\syswow64\cmd.exe"
p.StartInfo = pi
p.Start()
To me it looks like the problem is caused by the path reference inside an argument. I have read and used the different answers for using multiple commands, without any luck.
It would be great if someone could help me on this topic.
Kind regards,
Stuart
First of all, as a little note you don't need to add a space in the beginning of the arguments. That's only done when you write the entire command (including the executable) in one line.
Now, the correct way to pass a path as an argument is to surround it with quotes. So something like this should do:
(please note that I have also shortened some unnecessary concatenations, etc.)
pi.Arguments = "/K """"C:\program files\gdal\gdalshell.bat"" & cd ""C:\program files\gdal"" & " & _
"gdal_translate -of Jpeg -outsize 2000 2000 ""D:\box sync\my box (907070)\RIS_RHDHV_Overgang\GDAL\test2.xml"""""
EDIT:
I found that you also have to put the entire text after /K in quotes for it to work.

Excel VBA shell command to search Chrome

I have the following VBA code to open Chrome and navigate to a specific page:
Sub ExampleSub()
Dim chromePath As String
chromePath = """C:\Program Files\Google\Chrome\Application\chrome.exe"""
Shell (chromePath & " -url http:google.ca")
End Sub
What I can't find is the command to use the search engine to run a Google search for a string I pass it, like this:
Search_String = "Where to find pizza in Tibet"
Shell (chromePath & " -url " & Search_String)
or
Shell(chromePath & " -search " & Search_String)
or
Shell(chromePath & " " & Search_String)
I'd be running several searches, so the search criteria needs to be dynamic.
You are formatting your URL string incorrectly. Please consider the following solution:
Sub ExampleSub()
Dim chromePath As String
Dim search_string As String
search_string = "Where to find pizza in Tibet"
search_string = Replace(search_string, " ", "+")
chromePath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
' Uncomment the following line and comment out previous for Windows 64 versions
' chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Shell (chromePath & " -url http://google.ca/#q=" & search_string)
End Sub
If you run a search from google.com and review the resulting URL you will see that search terms are seperated by +. This is why I replace all white spaces for + and then add it to the URL. Regards,

Argument issue when using vb.net shell ()

I was trying to use shell function in vb.net to run a program and then write/export the result else where , it works on win8 but not XP !! The command line prints this error
'C:\Documents' is not recognized as an internal or external command,
operable program or batch file.
Press any key to continue . . .
Dim save as String="C:\exported.txt"
Dim command As String = tempPath & "app.exe -f " & IO.Path.GetTempPath & " -o " & save & " & pause"
shell("cmd /c " & command, AppWinStyle.NormalFocus, True)
Process.Start(IO.Path.Combine(tempPath, "app.exe"), "-f """ & IO.Path.GetTempPath & """ -o """ & Save() & """ & pause")
You hace a folder in the command with a space in it (probably the documents and Settings folder). cmd.exe has no way of telling whether the space is in the file name or the end of the file name, unless you put the file name in "" - like "C:\doxuments and Settings..."
Dim command As String = tempPath & "app.exe -f \"" & IO.Path.GetTempPath & "\" -o \"" & save & "\" & pause"
(\" is intended to escape a " so that it translates to embedding a " in the string. I'm not sure what the VB syntax is.)
EDIT:
I think #Roy van der Velde below has the 'escaping correct. The final part not checked is tempPath. See my comment on the question.

Write a visual basic script in a vb.net application.

In my application I have created I need it to write a visual basic script to change the ip address
The script is as follows...
Dim strIPAddress
Dim strSubnetMask
Dim strGateway
Dim intGatewayMetric
Dim strDns1
Dim strDns2
strIPAddress = "192.168.1.211"
strSubnetMask = "255.255.255.0"
strGateway = "192.168.0.11"
intGatewayMetric = 1
strDns1 = "8.8.8.8"
strDns2 = "4.4.4.4"
Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.Run "netsh interface ip set address name=""Local Area Connection"" static " & strIPAddress & " " & strSubnetMask & " " & strGateway & " " & intGatewayMetric, 0, True
objShell.Run "netsh interface ip set dns name=""Local Area Connection"" static "& strDns1, 0, True
objShell.Run "netsh interface ip add dns name=""Local Area Connection"" addr="& strDns2, 0, True
Set objShell = Nothing
WScript.Quit
The way I am trying to write this is like the following..
Directory.CreateDirectory("C:\SpecMee\IPChanger\")
Dim objwriter As New System.IO.StreamWriter("C:\SpecMee\IPChanger\IpChanger.vbs")
objwriter.WriteLine("Dim strIPAddress" & Environment.NewLine & "Dim strSubnetMask" & Environment.NewLine & "Dim strGateWay" & Environment.NewLine & "Dim intGatewayMetric" & Environment.NewLine & "Dim strDns1" & Environment.NewLine & "Dim strDns2" & Environment.NewLine & "strIPAddress = """ & Environment.NewLine & TB_IPAddress & Environment.NewLine &)
objwriter.Close()
MessageBox.Show("Created")
The problem I am having is one, it is taking ages and two, how would i include the "" in the script.
I dont mind spending time on this, I just dont know if im going about this the right way.
any help would be appreciated.
Thanks
Chris
Add the script as a text file resource to your project. Replace all instances of the IP Address in the script with some known value, then use string.replace:
Const ipPlaceHolder As String = "##IP##"
Dim scriptContent As String = My.Resources.script.Replace(ipPlaceHolder, myTextBox.Text)
IO.File.WriteAllText("C:\SpecMee\IPChanger\IpChanger.vbs", scriptContent)
Where myTextBox contains user input and the text file resource is named "Script"
Two quotation marks in a row makes one in the output.
"And he said ""Thar she blows!"""
( You have two questions in one. That is not considered good customs on Stackoverflow. )