Concatenating a complex string in VB. NET - vb.net

I am using START-PROCESS to call MSTEST with multiple arguments that define the container and test settings, however I think it's choking on the way I'm concatenating this. Should I use some other method of constructing this string before putting it into START-PROCESS?
Dim rwSettings As String = "\\PerfvsCtlr2\LoadtestSettings\PerfVSCtlr2forRemote.testsettings"
Dim rwContainer As String = "\\PerfvsCtlr2\LoadTest\LoadTestDefs\Heifer_Interactive_Peak_Workload.loadtest"
Dim rwResults As String = Workload.txtRwResults.Text
System.Diagnostics.Process.Start(Environment.GetEnvironmentVariable("VS110COMNTOOLS") & "..\Ide\MSTEST.EXE", "/Testsettings:""" & rwSettings & "" & " /Testcontainer:""" & rwContainer & "" & " /Resultsfile:""" & rwResults & "")
The problem is unknown currently, because process.start opens and closes the window far too quickly for me to catch any sort of error message. So my question is two-fold:
Does the above concatenation look correct? Is there a way I can get more information on either the final execution string Process.Start is putting together or the error message it's returning?

You can use Path.Combine to build paths and String.Format to build the arguments for Process.Start:
Dim rwSettings As String = "\\PerfvsCtlr2\LoadtestSettings\PerfVSCtlr2forRemote.testsettings"
Dim rwContainer As String = "\\PerfvsCtlr2\LoadTest\LoadTestDefs\Heifer_Interactive_Peak_Workload.loadtest"
Dim rwResults As String = "Workload.txtRwResults.Text"
Dim fileName = System.IO.Path.Combine(Environment.GetEnvironmentVariable("VS110COMNTOOLS"), "Ide\MSTEST.EXE")
Dim args = String.Format("/Testsettings:{0} /Testcontainer:{1} /Resultsfile:{2}", rwSettings, rwContainer, rwResults)
System.Diagnostics.Process.Start(fileName, args)
However, i must admit thar i'm not sure if this yields the desired result. It might give you an idea anyway.

I suspect that your problem is that you are not closing your quotation marks, for instance:
" /Testcontainer:""" & rwContainer & ""
Should be:
" /Testcontainer:""" & rwContainer & """"
Notice that the double-quotation mark at the end needs to be a quadruple quotation mark. Simply saying "" means an empty string.
Should you use something else? Probably. It would be more readable and efficient if you used StringBuilder or String.Format, but even so, you'll still have to fix the closing quotes issue.

Related

How to add quotation marks to 2 strings and variable in the middle

I'm trying to add quotations to a piece of string that is broken in 2 pieces with a variable in the middle.
I have tried many, many ways...many ways and failed.
Example:
Dim path as String = "C:\Users\" & CurrentUser & "\folder\path to something\"
I need the whole result to be in quotations to pass it to a command that requires the path with spaces in quotations.
"C:\Users\Nemo\Folder\Path to something\"
Any help is appreciated.
You should just be able to use "" inside the string assignment that you have. So with your example it would look like this:
Dim path As String = """C:\Users\" & CurrentUser & "\folder\path to something\"""
IMHO it is a good idea to use CHR(34) instead of actual quotes when concatenating strings. Your code should look like this:
Dim current user as string="Nemo"
Dim path as String = CHR(34) & "C:\Users\" & CurrentUser & "\folder\path to something\" & CHR(34)
The result will be:
"C:\Users\Nemo\folder\path to something\"

Vba to call a powershell script or .bat file

I have a line to call a powershell script and it works but the powershell windows gets closed without it working.
the code is, any ideas?
Additionally I would like it to be able to run .bat files (I tried Shell ( & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1" & pathcrnt) bit I get the same error.
Sub CallBatch()
Dim pathcrnt As String
pathcrnt = ActiveWorkbook.Path
Shell ("PowerShell " & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1" & pathcrnt)
End Sub
Please see the link below and make sure you have permissions for the machine.
http://www.tek-tips.com/viewthread.cfm?qid=1585510
I don't know about your setup or security, so if you encounter an error, search for 'PS permissions', or something along those lines. The VBA script will definitely work!
If you insert a space after .ps1 it should work:
Sub CallBatch()
Dim pathcrnt As String
pathcrnt = ActiveWorkbook.Path
Shell "PowerShell " & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1 " & pathcrnt
End Sub
Imagine the resulting string; for pathcrnt C:\Windows it would be this: "PowerShell C:\Windows\GUI-getuserpropertiesV2.10.ps1C:\Windows"
You also might want to enclose the path elements with double quotes to prevent spaces messing with your parameters, like this:
Shell "PowerShell """ & pathcrnt & "\GUI-getuserpropertiesV2.10.ps1"" """ & pathcrnt & """"
Since double quotes also are string delimiters in VBA you have to escape them with another double quote.

find & vbCrLf & within a string - vb.net

In a string I have something like "First & vbCrLf & Name" - however, I want to take out the & vbCrLf & so it doesnt cause a line break.
I have done something like
If theString.Contains("& vbCrLf &") Then
' and replace, could do this above of course, but I just want it to go into the IF
End If
and
If theString.Contains("\n") Then
' and replace, could do this above of course, but I just want it to go into the IF
End If
and even "\r\n" but to no avail.
What am I missing?
If theString.Contains(vbCrLf) Then
'Do something
End If
Alternatively...
theString = theString.Replace(vbCrLf, "")
Try:
If theString.Contains(Environment.NewLine) Then
' Code goes here
End If
Remove the vbCrLf from the string literal in Contains.
testVal = testVal.Replace(vbCrLf, String.Empty).Replace("&", String.Empty)
Metacharacters not supported by VB.Net for Strings - can be used with RegEx and probably a few other .Net functions.
In your OP I think you intended:
If theString.Contains("& vbCrLf &") Then
to be
If theString.Contains(vbCrLf) Then
You can test for and replace in one command:
Dim s As String = vbCrLf
MsgBox(s.Length)
s = s.Replace(vbCrLf, "")
MsgBox(s.Length)

quotes in vb.net links

I want to make a link from what the user filepath that is given from an openfiledialog. But I can't get all the quotes in the right places
DOCTextBox.Text = "" & TitleTextBox.Text & ""
In order to put a " in a VB.Net string literal, you need to write "".
For example:
"<a href=""" & HttpUtility.HtmlAttributeEncode(OpenFileDialog1.FileName) _
& """target=_""blank"">" & HttpUtility.HtmlEncode(TitleTextBox.Text) & "</a>"
DOCTextBox.Text = "" & TitleTextBox.Text & ""
It sounds like you're trying to add quotes into the final string produced from the expression. In order to do that use a pair of double quotes "". For example
""
You can use triple quotes such as """

New line in VB.NET

Why, when I do im my code:
"Land Location \\r\\n Roundoff (C)"
I see the \\r\\n and not a new line feeder at the output?
Any idea how to do that?
As I said I must have only one string there, without using a "&". Can I put that vbCrLf inside of my string somehow?
There is no \ escape codes in VB so you can't put a line break in a string literal. The only escape character in VB strings is the double quotation marks used to insert a quotation mark in a string.
You can use the VB constant for a Windows type line break:
"Land Location " & vbCrLf & " Roundoff (C)"
For the code to be platform independent, you should use the NewLine property instead:
"Land Location " & Environment.NewLine & " Roundoff (C)"
Whether you should use the platform independent code or not depends on the situation.
If you need it as a single string for some reason, you would have to use a marker for the line break, that you replace when you use the string:
Dim s As String = "Land Location \n Roundoff (C)"
s = Replace(s, "\n", Environment.NewLine)
May be "Land Location " & vbCR & vbLF .....
--
Edit: per #JeffSahol's comments, you can use string interpolation since VB 14, so it can be like
$"...{vbCrLf}..."
Instead of including the newline manually in the String use System.Environment.NewLine.
vbCrLf
vbCr
vbLf