'VB Net Quotes in a string
I need quotes in a string in VB Net.
c = C:\test.doc
How can I realize that the string looks like:
PrintFile /File="C:\test.doc" /OutputFile="C:\test.pdf"
online I read that I have to double the quotes... but this doesn't work:
" PrintFile /File=""" & c & """ /OutputFile=""" & pfadFertig & pdfName & ".pdf"""
How can I get c and pfadFertig & pdfName & .pdf appear in quotes?
Solution 1:[1]
You do indeed have to double the quotes. This is a great example of why you should use String.Format or, even better, string interpolation. Using concatenation operators (&) already makes your code harder to read but with all the extra quotes, it's harder still. Do this:
Dim str = $"PrintFile /File=""{c}"" /OutputFile=""{pfadFertig}{pdfName}.pdf"""
It's obviously much easier to read.
Also I suspect that pfadFertig is a folder path, in which case you should be using Path.Combine to create the file path:
Dim str = $"PrintFile /File=""{c}"" /OutputFile=""{Path.Combine(pfadFertig, pdfName)}.pdf"""
Path.Combine will ensure the correct number of slashes regardless of what trailing or leading slashes are included in the inputs, so you can never make a mistake. In this case, I could probably excuse one concatenation operator as it may seem more natural:
Dim str = $"PrintFile /File=""{c}"" /OutputFile=""{Path.Combine(pfadFertig, pdfName & ".pdf")}"""
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user18387401 |
