Click here to Skip to main content
15,912,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
so heres the problem...........i just created tic tac toe and i also added a sound file in it like this........

System.Media.SoundPlayer winner = new System.Media.SoundPlayer(@"D:\Projects\Tic tac toe\Tic tac toe\bin\Debug\win1.wav");
winner.Play();


but the directory is the problem cause when the directory goes to some other location it will give an error i want to give it to some couple of friends but the problem here is that there directory path wouild be different so what do i do then.........i dont know how to explain it better so

if (explanation==bad){
please.forgive
}

plz help..
Regards,
Ahsan Naveed
Posted

Hi,
use a relational path instead, like this:

System.Media.SoundPlayer winner = new System.Media.SoundPlayer(@"win1.wav");
winner.Play();

This will take the wav from the executing (current) directory, so put the wav-file in the same directory as your executable.
 
Share this answer
 
Comments
Ahsan98 6-Sep-13 6:38am    
thanks 5ed it great just what i needed i had already kep it in the executing directory but i didnt know that it takes the file from that directory in this way
Hi Ahsan,

If you are using Windows Application add this file as a resource file
Steps to Add as a resource
1. Go to Properties in Solution Explorer
2. Open Resource.resx and click on add resource and then choose add existing file.
C#
3. WindowsFormsApplication1.Properties.Resources._01___Angreji_Beat___Cocktail__128_Kbps_

get the the file name as in step 3.


For Web Application Add a folder whichever name you like.ex "Music"
and add music file to this folder and get file using this code.

C#
System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Music\Adele-Skyfall.mp3");


Now this totally portable.
Hope this helps.
 
Share this answer
 
Comments
Ahsan98 6-Sep-13 6:51am    
your solution is very good although i do need one help about this....

actually a added the files in the resources...now your step 3 code i assume is the path or the code to call it plz give more detail about it tried using it like this
Tic_tac_toe.Properties.Resources.win1
didint work

i also tried this
System.Media.SoundPlayer winner = new System.Media.SoundPlayer("Tic_tac_toe.Properties.Resources.win1");
winner.Play();

didnt work so plz tell me exactly what to do.......
Ahsan98 6-Sep-13 6:51am    
and yes it is a win form
bitofweb 6-Sep-13 7:11am    
make sure you are using .wav file not mp3.
Ahsan98 6-Sep-13 8:21am    
I was using .wav files but i got my mistake and solved thnks for your help i added inverted commas in my path thought maybe i had to write the path like that for directory thnks very much your idea was very good


Regards,
Ahsan Naveed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900