Creating a complex string construct with enclosed quotation marks in VB.NET

Scheytt, Michael AVL/DE 20 Reputation points
2023-04-10T09:00:53.8566667+00:00

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

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,472 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,610 questions
0 comments No comments
{count} votes

Accepted answer
  1. LesHay 7,126 Reputation points
    2023-04-10T12:36:56.88+00:00

    Hi Here is an example that may help. The Form1 has TextBox1 and TextBox2 - (both multiline with WorWrap on) to display results. Sub One just concatenates and produces exactly as per your question. Sub Two illustrates evaluation and substitution if indeed the 'AnyVal' parameters are derived from variables (only show for the 4 'AnyVal' parts.) Anyway, here is my attempt. (Only the resultant string - no Process invoked)

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim s1() As String = {"C:\Windows\SysWOW64\wscript.exe", "C:\Temp\AnyProcess.vbs", "Cmd1Par1=AnyVal1 Cmd1Par2=AnyVal2 Cmd1Par3=AnyVal3 Cmd1Par4=AnyVal4", "Cmd2Par1 Cmd2Par2", "Cmd3Par1", "Cmd4Par1"}
    
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		' verbatim
    		TextBox1.Text = One()
    
    		' evaluate and substitute
    		Dim AnyVal1 As Integer = 1234
    		Dim AnyVal2 As String = "String1 Value1"
    		Dim AnyVal3 As Double = 987.654
    		Dim AnyVal4 As String = IO.Path.Combine(Application.StartupPath, "Data", "String2 Value2")
    
    		TextBox2.Text = Two(AnyVal1, AnyVal2, AnyVal3, AnyVal4)
    
    	End Sub
    	Function One() As String
    		Dim part As String = String.Empty
    		For Each s As String In s1
    			part &= Chr(34) & s & Chr(34) & Chr(32)
    		Next
    		Return part
    	End Function
    	Function Two(v1 As Integer, v2 As String, v3 As Double, v4 As String) As String
    		Dim part As String = String.Empty
    		For Each s As String In s1
    			s = s.Replace("AnyVal1", v1.ToString)
    			s = s.Replace("AnyVal2", v2)
    			s = s.Replace("AnyVal3", v3.ToString("0.000"))
    			s = s.Replace("AnyVal4", v4)
    			part &= Chr(34) & s & Chr(34) & Chr(32)
    		Next
    		Return part
    	End Function
    End Class
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful