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?

1 answer

  • answered 2022-05-07 02:42 user18387401

    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")}"""
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum