0

I have an audio file name with spaces

String filename = @"c:\users\me\desktop\this is audio.mp3";

say i want to open this file through cmd using external process,to open it i need the path to be like this : c:\users\me\desktop\"this is audio.mp3" (With quotes)

any ideas how to convert it to valid verbatim literal ?

I've tried the following:

String file2 = @"c:\users\me\desktop\""this is audio.mp3""";

but i got this

c:\users\me\desktop\\this is audio.mp3"

Howa
  • 60
  • 7
  • 3
    That is correct when displayed in the debugger's inspector. – Rudi Visser Mar 11 '13 at 12:32
  • `@"""c:\users\me\desktop\this is audio.mp3"""` in *C#* is same as `"\"c:\users\me\desktop\this is audio.mp3\""` in *C#* which turn is same as `"c:\users\me\desktop\this is audio.mp3"` in *normal text*. – publicgk Mar 11 '13 at 12:33
  • What you see in the watch is not what you get = WYSIWINWYG. Go ahead and try harder — output that string to a console window. – Ondrej Tucny Mar 11 '13 at 12:33

1 Answers1

1

You have escaped correctly.

The debugger is showing you a non literal string.

@"""c:\users\me\desktop\this is audio.mp3"""

And:

"\"c:\\users\\me\\desktop\\this is audio.mp3\""

Are identical, as far as the compiler/debugger are concerned.

They both produce a string containing: "c:\users\me\desktop\this is audio.mp3" (with the double quotes embedded).

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • but i still can't open that file ! – Howa Mar 11 '13 at 12:38
  • @Howa - What exception are you getting? Are you certain the file is there and that the running process has permissions to it? – Oded Mar 11 '13 at 12:39
  • hmm, say i want it like this c:\users\me\desktop\"this is audio.mp3", how to implement it using verbatim literal ? – Howa Mar 11 '13 at 12:39
  • @Howa - `@"c:\users\me\desktop\""this is audio.mp3"""`. – Oded Mar 11 '13 at 12:40
  • but \" in verbatim literal means \ not " – Howa Mar 11 '13 at 12:41
  • @Howa - Yes. But to get a `"`, you need to double it, as I did. The `\ ` is the _directory separator_ in that string. – Oded Mar 11 '13 at 12:42
  • ok i know that, but look at `"c:\users\me\desktop\""this is audio.mp3"""`, after desktop\""this is audio.mp3""", it'll be like this desktop\\"this is audio.mp3" – Howa Mar 11 '13 at 12:44
  • @Howa - What makes you say that? – Oded Mar 11 '13 at 12:46
  • sir i got it :), but in verbatim literal to get \ i have to write `@\"` , the problem is with the path, `"c:\users\me\desktop\""this is audio.mp3"""` , after `desktop`\ i need the `"` not \ – Howa Mar 11 '13 at 12:47
  • it wont give **"** because there's \" which gives \ – Howa Mar 11 '13 at 13:18