With Visual Studio .NET 2003, we can specify the "Build Action" property of any file in the solution. This feature bring us the opportunity to embed the content of any file into the result assembly (assemblies) after compilation. It is so simple and can help us when we need some constant data to be deployed with our solution, by example, our application uses an XML schema that (after the application is completed) never changes, and we doesn't like to deploy a separate file with the schema content. Here is a step by step procedure to embed a file into the solution:
1. In the Solution Explorer, right click over the project where we want to have the embedded file, Add / Add Existing Item.
2. Select the file to embed.
3. Show the Property window of the file, and in the property "Build Action", select "Embedded Resource"
After that, when we compile the solution, we have the content of that file into the assembly, but... how we can read this data? Well, here is a method to extract the content of any embedded resource from the assembly using reflection and stream readers:
using
System.Reflection;
using System.IO;
private string getEmbeddedResourceContent(string resourceFileName)
{
Assembly myAsembly = Assembly.GetExecutingAssembly();
string resName = String.Empty;
foreach(string resourceName in myAsembly.GetManifestResourceNames())
{
if (resourceName.IndexOf(resourceFileName)>=0)
{
resName = resourceName;
break;
}
}
if (resName.Length > 0)
{
Stream s = myAsembly.GetManifestResourceStream(resName);
StreamReader sr = new StreamReader(s, System.Text.Encoding.GetEncoding(1252));
return sr.ReadToEnd();
}
else
{
throw new Exception("The resource can't be found");
}
}
Note that this method only works with text files and that we can use other assemblies distinct of the executing assembly to host the embedded resources.
posted on Wednesday, June 09, 2004 11:46 AM
Feedback
# re: Embedding Files into Assemblies
6/15/2004 10:17 AM |
I would like to know if you really nedd to loop through all the resources in your assembly to locate the one you are looking for...
# re: Embedding Files into Assemblies
6/15/2004 10:20 AM |
I think so, but if you know another way to get directly the resource that you want, show me!
6/18/2004 2:07 PM |
A refactor of a method posted by raul about reading resources from assemblies
# re: Embedding Files into Assemblies
12/9/2007 7:14 PM |
We offer the largest collection of polyphonic ringtones, monophonic ringtones, mobile videos, color wallpapers, color screensavers, real sounds.
# re: Embedding Files into Assemblies
12/24/2007 9:19 PM |
If you are looking for the replica watch and information about it, you came to the right place.
# re: Embedding Files into Assemblies
11/30/2009 11:12 PM |
Embedding files is a good metod for implementing...
# re: Embedding Files into Assemblies
11/30/2009 11:16 PM |
Oh MY God, this site is wonderful!!!
# re: Embedding Files into Assemblies
11/30/2009 11:53 PM |
I have a problems with the embedding files into assemblies, why?