Getting an embedded resource file out of an assembly
Getting an embedded resource file out of an assembly (转载)
/// <summary>
/// Extracts an embedded file out of a given assembly.
/// </summary>
/// <param name="assemblyName">The namespace of you assembly.</param>
/// <param name="fileName">The name of the file to extract.</param>
/// <returns>A stream containing the file data.</returns>
public static Stream GetEmbeddedFile(string assemblyName, string fileName)
{
    try
    {
        System.Reflection.Assembly a = System.Reflection.Assembly.Load(assemblyName);
        Stream str = a.GetManifestResourceStream(assemblyName + "." + fileName);
           
        if(str == null)
            throw new Exception("Could not locate embedded resource '" + fileName + "' in assembly '" + assemblyName + "'");
        return str;
    }
    catch(Exception e)
    {
        throw new Exception(assemblyName + ": " + e.Message);
    }
}

A couple of notes about the embedding resources. If you're like me and you like to organize your resources, you prolly have them grouped into folders. In that case, the fileName parameter must include the path to the file. For example, if I have the following embedded resource:

Project Root/resources/images/top_left_corner.gif

the fileName arg would be:

myNamespace.resources.images.top_left_corner.gif

Don't ask me why you use a period instead of a typical directory separator. Mine is not to question why.

Perhaps you don't know the name of the assembly, or are just too lazy to pass it along, here are some overloads that take in alternative types - Type & Assembly

Getting an embedded resource file out of an assembly (转载)
public static Stream GetEmbeddedFile(System.Reflection.Assembly assembly, string fileName)
{
    string assemblyName = assembly.GetName().Name;
    return GetEmbeddedFile(assemblyName, fileName);
}

public static Stream GetEmbeddedFile(Type type, string fileName)
{
    string assemblyName = type.Assembly.GetName().Name;
    return GetEmbeddedFile(assemblyName, fileName);
}
So the following example returns a stream. But if you are constantly extracting XML docs, or bitmaps, wrap it up:

For Bitmaps:
Getting an embedded resource file out of an assembly (转载)
public static Bitmap GetEmbeddedImage(Type type, string fileName)
{
    Stream str = GetEmbeddedFile(type, fileName);
    return new Bitmap(str);
}


For XML Docs:
Getting an embedded resource file out of an assembly (转载)
public static XmlDocument GetEmbeddedXml(Type type, string fileName)
{
    Stream str = GetEmbeddedFile(type, fileName);
    XmlTextReader tr = new XmlTextReader(str);
    XmlDocument xml = new XmlDocument();
    xml.Load(tr);
    return xml;
}
Be careful when embedding A LOT of files as it will significantly increase the size of your assembly.

相关文章:

  • 2021-06-30
  • 2021-09-20
  • 2022-12-23
  • 2021-08-03
  • 2021-05-05
  • 2022-01-02
  • 2022-12-23
  • 2021-11-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-08-03
  • 2022-12-23
  • 2021-10-30
相关资源
相似解决方案