Click here to Skip to main content
15,905,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have a problem that I can't get any further. Even after researching numerous similar cases, I have not been able to find a solution to my problem, hence the question.
It's about the topic 'String within String' or quotation marks within strings in VB.NET.
I want to start a *.vbs with relatively extensive command line parameters in a VB.NET code.
Since the group policy on the target computer always resets the standard program for *.vbs files to Notepad, I additionally have to programmatically 'force' the execution of the file with WScript.exe.

The entire string - which works fine when run via the Windows 'Run' command line utility! - looks like this:

"C:\Windows\SysWOW64\wscript.exe" "C:\Temp\AnyProcess.vbs" "Cmd1Par1=AnyVal1 Cmd1Par2=AnyVal2 Cmd1Par3=AnyVal3 Cmd1Par4=AnyVal4" "Cmd2Par1 Cmd2Par2" "Cmd3Par1" "Cmd4Par1"

Now I try in vain to map this 'construct' in VB.NET and then to execute via

Dim myproc As New Process
start.FileName = .....
start.Arguments = .....
myproc.Start()

I am failing at creating the string constructs and assigning them to .Filename and .Arguments.

(PS: using the 'Process' class (rather than simple SHELL Run) is important because I want to control the process inside the VB.NET application (.WaitForExit))

thanks for help.
greets catrice

What I have tried:

Dim xylauncher = Application.StartupPath & "\" & filename_vbs & " "
Dim Cmd1 = "Cmd1Par1 Cmd1Par2 Cmd3Par3 Cmd1Par4"
Dim Cmd2 = "AnyString2"
Dim Cmd3 = "AnyString3"
Dim Cmd4 = "AnyString4"
xycmdline = """" & Cmd1 & """" & " " & """" & Cmd2 & """" & " " & """" & Cmd2 & """" & " " & """" & Cmd4 & """"

Dim myproc As New Process
start.FileName = xylauncher
start.Arguments = xycmdline
myproc.Start()
Posted
Updated 10-Apr-23 0:03am

To duplicate your original command is simple:
VB
Dim cmdApp As String = """C:\Windows\SysWOW64\wscript.exe"""
Dim cmdPar As String = """C:\Temp\AnyProcess.vbs"" ""Cmd1Par1=AnyVal1 Cmd1Par2=AnyVal2 Cmd1Par3=AnyVal3 Cmd1Par4=AnyVal4"" ""Cmd2Par1 Cmd2Par2"" ""Cmd3Par1"" ""Cmd4Par1"""
Dim myproc As New Process
start.FileName = cmdApp
start.Arguments = cmsPar
myproc.Start()
So start from there, and it should be obvious what you need to do.
 
Share this answer
 
NB As mentioned by Richard Deeming below, this is only valid with .NET Core.

Most of those extra quote characters are unnecessary. Try this:
VB
Dim myproc As New Process
Dim start As New myproc.StartInfo
start.FileName = "C:\Windows\SysWOW64\wscript.exe"
start.ArgumentList.Add("C:\Temp\AnyProcess.vbs")
start.ArgumentList.Add("Cmd1Par1=AnyVal")
start.ArgumentList.Add("Cmd1Par2=AnyVal2")
start.ArgumentList.Add("Cmd1Par3=AnyVal3")
start.ArgumentList.Add("Cmd1Par4=AnyVal4")
start.ArgumentList.Add("Cmd1Par1")
start.ArgumentList.Add("Cmd1Par2")
start.ArgumentList.Add("Cmd1Par3")
start.ArgumentList.Add("Cmd1Par4")
myproc.Start()
 
Share this answer
 
v3
Comments
Richard Deeming 11-Apr-23 6:02am    
NB: The ProcessStartInfo.ArgumentList property[^] was added in .NET Core 2.1; if you're still using .NET Framework, that property won't be available.

You can see the code that converts that to the arguments string on GitHub:
runtime/ProcessStartInfo.cs at 17ed09116b8750f73d4b96f67fa875659edc98b4 · dotnet/runtime · GitHub[^]
runtime/PasteArguments.cs at 17ed09116b8750f73d4b96f67fa875659edc98b4 · dotnet/runtime · GitHub[^]
Richard MacCutchan 11-Apr-23 6:04am    
Thanks Richard. Unfortunately I forget that whenever I read a MSDN .NET entry it defaults to .NET core.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900