string path inside a directory of WPF c# solution

87 Views Asked by At

I created a directory inside my WPF solution called Sounds and it holds sound files.(For example: mySound.wav). Inside my code I use a List and there I have to add to those strings that relate to the sound files. In the beginning I used @"C:..." but I want it to be something like "UNIVERSAL" path. I tried using: "\Sounds\mySound.wav" but it generates an error. The lines that I use there this directory are:

myList.Add("\Sounds\11000_0.2s.wav");//Error
using (WaveFileReader reader = new WaveFileReader(sourceFile)) 

where sourceFile is a string which express a path of the file.

2

There are 2 best solutions below

0
On BEST ANSWER

Make sure that you check CopyToOutputDir in the properties of the soundfile, that will make sure the file is copied to the location you program runs from.

Also don't use single backslashes in the path since its an escape character. Instead, do one of the following things:

Use a verbatim string:

@"Sounds\11000_0.2s.wav"

Escape the escape char:

"Sounds\\11000_0.2s.wav"

Use forward slashes:

"Sounds/11000_0.2s.wav"

For more information on string literals check msdn.

0
On

You either need to escape the / in the string or add the string literal indicator @ at the beginning of the string.

Escape example:

var myFilePath = "c:\\Temp\\MyFile.txt";

String literal example:

var myFilePath = @"c:\Temp\MyFile.txt";