Question Where to store icons and music used by my application ?

dlp_coasters

New member
Joined
Apr 2, 2019
Messages
2
Programming Experience
Beginner
Where should i store all the files used by my c# application like music or icons... Someone told me it has to be stored in the "bin/Debug" folder, but i'm not quite sure. Thanks in advance for your anwsers !
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,613
Location
Sydney, Australia
Programming Experience
10+
Saying that something should be stored in the bin\Debug folder is misleading. That folder is where VS places the output of the build process for a Debug build. You don't put anything there yourself. For a Release build, the output is placed in the bin\Release folder and you copy it from there to deploy it. If you have content files for your application then you can add them to the project in the Solution Explorer and set the Build Action property to Content and the Copy to output directory property to Copy if newer. They will then be copied to the output folder, wherever that may be, during the Build process and you can access them in code by referring to the program folder as the root path, e.g. Application.StartupPath in WinForms. I'd probably suggest adding a folder to your project and adding the files under that. In that case, the folder will be copied to the output as well, so account for that in the path you use at run time.

Another option is to add the files to the Resources page in the project properties and then access the data using Properties.Resources in code. Depending on the type of data, you may get an object that you can use directly or a stream that you can write to a file or an object. The main advantage of resources is that the user cannot edit or delete them as they can with separate files.
 

dlp_coasters

New member
Joined
Apr 2, 2019
Messages
2
Programming Experience
Beginner
Well i asked this question, but i dont even know if the files like the music has to be in a certain directory... does it have to ? I mean if i build it on my computer of course the app works with that background music but if i send it to someone who doesnt have the music file on their computer, will the app say that it cant find the music file ?
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,613
Location
Sydney, Australia
Programming Experience
10+
That's why you don't hard-code an absolute path: because it may not exist on a different machine. You pretty much always use a partial path relative to a well-known location. If you were to follow the advice I provided earlier and added Song.mp3 to a folder named Music then you could get the full path of that file like so:
C#:
var filePath = System.IO.Path.Combine(Application.StartupPath, "Song.mp3");
You can use Environment.GetFolderPath to get the paths of various well-known system folders, e.g. the current user's Documents folder.
 
Top Bottom