Here's some code to read data from an embedded resource:
public static class EmbeddedResourceReader
{
public static string ReadString(Assembly assembly, string resourceName)
{
if (assembly == null) throw new ArgumentNullException("assembly");
if (resourceName == null) throw new ArgumentNullException("resourceName");
using(Stream lStream = assembly.GetManifestResourceStream(resourceName))
using(StreamReader lReader = new StreamReader(lStream))
return lReader.ReadToEnd();
}
public static Icon ReadIcon(Assembly assembly, string resourceName)
{
if (assembly == null) throw new ArgumentNullException("assembly");
if (resourceName == null) throw new ArgumentNullException("resourceName");
using(Stream lStream = assembly.GetManifestResourceStream(resourceName))
return new Icon(lStream);
}
}
Basically, you can read any kind of data from an embedded resource, using the method
GetManifestResourceStream. The resource name you have to pass as an argument should be the default namespace of the assembly, appended with the filename. Here's an example how to use it:
Assembly assembly = this.GetType().Assembly;
string resourceName = "MyCompany.MyApplication.file.txt";
string content = EmbeddedResourceReader.ReadString(assembly, resourceName);
0 comments:
Post a Comment