Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 播放项目内具有相对路径的wav文件_C#_Visual Studio 2012_Wav_Relative Path_Soundplayer - Fatal编程技术网

C# 播放项目内具有相对路径的wav文件

C# 播放项目内具有相对路径的wav文件,c#,visual-studio-2012,wav,relative-path,soundplayer,C#,Visual Studio 2012,Wav,Relative Path,Soundplayer,我在wav文件的相对路径和复制方面有一些问题。我有一个简单的代码,可以完美地工作: SoundPlayer player = new SoundPlayer(); player.SoundLocation = @"C:\Users\Admin\Documents\Visual Studio 2012\Projects\TestProject\TestProject\Data\Sounds\car.wav"; player.Play(); 我想以某种方式使用相对路径播放该文件,但我没有成功: S

我在wav文件的相对路径和复制方面有一些问题。我有一个简单的代码,可以完美地工作:

SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"C:\Users\Admin\Documents\Visual Studio 2012\Projects\TestProject\TestProject\Data\Sounds\car.wav";
player.Play();
我想以某种方式使用相对路径播放该文件,但我没有成功:

SoundPlayer player = new SoundPlayer();
player.SoundLocation = @"Data\Sounds\car.wav";
player.Play();

谢谢大家!

应用程序的根目录中是否有
Data
目录是否将目录内容复制为输出?

如果是,您的意思是,
Data\Sounds\car.wav

如果从Visual Studio运行,它将位于
[projectroot]\[release]\bin\Data\Sounds\car.wav


如果在bin文件夹中看不到此目录,则需要确保选择了要复制到输出目录的所有文件(这将复制目录结构)。您可以通过单击项目中的文件并选择该文件作为输出来完成此操作。

使用

获取文件的完整路径毕竟,使用绝对路径可能会更好。您可以从exe文件中获取根路径,然后将相对路径附加到该文件中。 像这样:

// getting root path
string rootLocation = typeof(Program).Assembly.Location;
// appending sound location
string fullPathToSound = Path.Combine(rootLocation, @"Data\Sounds\car.wav");
player.SoundLocation = fullPathToSound;
这是为我工作

System.Media.SoundPlayer player1 = new System.Media.SoundPlayer(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\1.wav");

是的,数据在根目录中。不是根据你问题中的文件路径,它不是。。。我通过右键单击Project并“添加新文件夹”,在TestProject中添加新目录,创建了新的数据目录。那不是根目录吗?:)编译后,您的根目录是bin\Debug或bin\Release目录。这不适用于me-Assembly。位置包含附加到末尾的.EXE,因此我以bin\Debug\Program.EXE\Data\Sounds结束,这是错误的。感谢您的贡献。你能解释一下你的回答是如何解决原来问题的吗?
System.Media.SoundPlayer player1 = new System.Media.SoundPlayer(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\1.wav");