-2

I have a sub that is supposed to play a music file. I can locate MyDocuments easily. I can even use Path.Combine to concatenate the rest of the string.

The full path should look something like this:

......Documents\JukeBox\MichaelJackson\01.wav

But I am getting double slashes not single ones

private static void playChoice(string band, int choice)
    {
        var myDocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string filename = "0" + choice;
        string[] paths = { myDocs, "JukeBox", band, filename, ".wav" };
        var fullPath = Path.Combine(@paths);
        var player = new System.Media.SoundPlayer(fullPath);
        player.Play();
    }

A) How do I strip out the double slashes since my verbatim specifier does not work B) The code looks awful - is there a better approach - or does anyone have a link to helpful literature

Beginner
  • 129
  • 10
  • 6
    You're completely misunderstanding what `@` does there. – SLaks Oct 23 '17 at 19:28
  • 5
    You're probably seeing the path in the debugger, which escapes strings for display. You probably don't actually have a problem (except that you're treating the extension as a folder). – SLaks Oct 23 '17 at 19:29

1 Answers1

5

Verbatim string literals is a feature of string literals that affect how the literal is parsed.
You don't have any string literals; that is completely irrelevant.

@paths is a completely different feature that lets an identifier be named after a keyword (eg, int @int). It's also irrelevant.

You're probably seeing the value in the debugger, which displays the C# source to write the value, including escape sequences. Your string doesn't actually have double-slashes.

However, Path.Combine() combines folders (by adding slashes between them); it makes no sense to pass an extension there.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    Or the extension should be concatenated with the file name instead of passed as a separate parameter to `Combine`: `string[] paths = { myDocs, "JukeBox", band, filename + ".wav" };` – Rufus L Oct 23 '17 at 19:34