How to hide gif or mp3 files in my project?

I have a C # project with some gif and mp3 files

How can I combine these files in my project?

(I do not want them to be visible to users)

+4
source share
4 answers

You need to include them in the project as resources, and then access them later by reading from the DLL.

For gif files, you can simply delete them on the resources (in the project properties dialog->), and then access them through

var img = Properties.Resources.GifName; 

For mp3 files, you probably have to use the built-in resources and then read them as a stream. To do this, drag an item into the folder in your project dedicated to these file types. Right-click on the file in Explorer and show the properties panel and set the "build action" to "embedded resource".

Then you can use code similar to this (unverified translation from vb, sorry) to return the object to the stream. It is up to you to transform the stream into something your player can handle.

 using System.Linq; // from System.Core. otherwise just translate linq to for-each using System.IO; public Stream GetStream(string fileName) { // assume we want a resource from the same that called us var ass = Assembly.GetCallingAssembly(); var fullName = GetResourceName(fileName, ass); // ^^ should = MyCorp.FunnyApplication.Mp3Files.<filename>, or similar return ass.GetManifestResourceStream(fullName); } // looks up a fully qualified resource name from just the file name. this is // so you don't have to worry about any namespace issues/folder depth, etc. public static string GetResourceName(string fileName, Assembly ass) { var names = ass.GetManifestResourceNames().Where(n => n.EndsWith(fileName)).ToArray(); if (names.Count() > 1) throw new Exception("Multiple matches found."); return names[0]; } var mp3Stream = GetStream("startup-sound.mp3"); var mp3 = new MyMp3Class(mp3stream); // some player-related class that uses the stream 

Here are some links to get started.

+10
source

Add them as embedded resources and they will be compiled into a dll.

First add them to the project by simply dragging them to the solution explorer, then go to their properties to change their build action to built-in resources.

+7
source

Include them in the resource file (resx).

+4
source

You just have to assign them as resources. If you open the Properties panel in VS, make sure your assembly action is set to Resource and Copy to Output Directory is set to Do Not Copy. You can also manage resources from project properties in the resources tab.

+1
source

All Articles