2013-04-17 44 views
1

我在wav文件的相對路徑和複製方面存在一些問題。我有這個簡單的代碼,其完美的作品:播放項目內有相對路徑的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(); 

謝謝!

回答

3

Data目錄在你的應用程序的根目錄下? 您是否將目錄內容複製爲輸出?

如果是這樣,你的意思是,Data\Sounds\car.wav

其中,如果從Visual Studio運行將是[projectroot]\[release]\bin\Data\Sounds\car.wav

如果你沒有看到在你的bin文件夾這個目錄中,你需要確保你選擇你想要複製到文件你的輸出目錄(它將複製目錄結構)。您可以通過單擊項目中的文件並選擇文件作爲輸出來完成此操作。

+0

是的,數據是在根目錄下。 – Cristiano

+0

不是根據你的問題的文件路徑,它不是。 –

+0

Hm ...我通過在ProjectProject中添加新目錄並右鍵單擊Project和'Add new folder'來創建新的Data目錄。這不是一個根目錄嗎? :) – Cristiano

2

你可能會更好使用絕對路徑,畢竟。您可以從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; 
0
//WindowsFormsApplication4.exe is name of name space this file name found in Debug file 

//you should copy your "sound.wav" into your Debug file 


     string x = (Assembly.GetEntryAssembly().Location + ""); 
     x = x.Replace("WindowsFormsApplication4.exe", "sound.wav"); 
     SoundPlayer player1 = new SoundPlayer(x); 
     player1.Play();