URL: http://stackoverflow.com/questions/725915/how-to-access-a-binary-resource-in-a-c-application
Retrieve a byte array from an embedded resource. This function uses Assembly.GetManifestResourceStream() and Stream.Read() to read an embedded resource. The use of embedded resources allows the .EXE or assembly to be used as a container for data/blobs that might otherwise be stored in an external file.
Instructions: 1) Add a file to the Visual Studio project e.g., MyData.bin, text.txt, database.mdb, final.hex etc. 2) For that file, set its Properties / Build Action = Embedded Resource. Other properties keep defaults (do not copy, blank tool, blank namespace). 3) At runtime, retrieve resource using this static function.
Notes: 1) A re-build of the project includes any changes to the resource file - automatically. 2) The resource editor isn't used and doesn't show the embedded resource. 3) This technique can be used for 'blobs' or non-typical resources such as: Binary data, databases, information generated by external compilers/tools. 4) To get a list of resource names during development, inspect this: System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames(). The resource name is the embedded file, prepended with the app name e.g., "MyApplication.MyData.bin" and the name passed to this function would be "MyData.bin" (without the app name). 5) This function is static; can be accessed by forms, app etc. 6) For big resources, consider using the stream rather than a byte[]. 7) resourceManager is an alternative approach to get a stream But, the resource name gets changed/mangled. ResourceManager resourceManager = new ResourceManager(assembly.GetName().Name + ".Properties.Resources", assembly); stream = resourceManager.GetStream(resourceNameArg); 8) A clumsy alternative to an embedded resource might involve converting data into C# code used to initialize a byte array.
/// <summary> /// Retrieve a byte array from an embedded resource. /// This function uses Assembly.GetManifestResourceStream() to read an embedded resource. /// The use of embedded resources allows the .EXE or assembly to be used as a container for /// data/blobs that might otherwise be stored in an external file. /// </summary> /// <param name="resourceNameArg">Name of file embedded in project, e.g. "MyData.bin"</param> /// <returns>An array of bytes containing the resource contents.</returns> /// <remarks> /// Instructions: /// 1) Add a file to the project e.g., MyData.bin, text.txt, database.mdb, final.hex etc. /// 2) For that file, set its Properties / Build Action = Embedded Resource. /// Other properties keep defaults (do not copy, blank tool, blank namespace). /// 3) At runtime, retrieve resource using this static function. /// /// Notes: /// 1) A re-build of the project includes any changes to the resource file - automatically. /// 2) The resource editor isn't used and doesn't show the embedded resource. /// 3) This technique can be used for 'blobs' or non-typical resources such as: /// Binary data, databases, information generated by external compilers/tools. /// 4) To get a list of resource names during development, inspect this: /// System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames() /// The resource name is the embedded file, prepended with the app name e.g., "MyApplication.MyData.bin" /// and the name passed to this function would be "MyData.bin" (without the app name). /// 5) This function is static; can be accessed by forms, app etc. /// 6) For big resources, consider using the stream rather than a byte[]. /// 7) resourceManager is an alternative approach to get a stream /// But, the resource name gets changed/mangled. /// ResourceManager resourceManager = new ResourceManager(assembly.GetName().Name + ".Properties.Resources", assembly); /// stream = resourceManager.GetStream(resourceNameArg); /// 8) A clumsy alternative to an embedded resource might involve converting /// data into C# code used to initialize a byte array e.g.: /// public static byte[] some_bin = new byte[] /// { /// 0x02, 0x07, 0xF3, 0xAA, 0xAA, ... /// }; /// </remarks> /// <see> /// http://stackoverflow.com/questions/725915/how-to-access-a-binary-resource-in-a-c-application /// http://msdn.microsoft.com/en-us/library/aa287676%28v=vs.71%29.aspx /// </see> static private byte[] GetResourceBytes( string resourceNameArg ) { Stream stream = GetResourceStream(resourceNameArg); if (stream == null) { return null; // resource not found. } stream.Read( bytes, 0, (int)stream.Length); return bytes; } /// <summary> /// Retrieve a Stream from an embedded resource. /// This function uses Assembly.GetManifestResourceStream() to get a Stream for an embedded resource. /// The use of embedded resources allows the .EXE or assembly to be used as a container for data/blobs /// that might otherwise be stored in an external file. /// </summary> /// <param name="resourceNameArg">Name of file embedded in project, e.g. "MyData.bin"</param> /// <returns>A Stream for the embedded resource. It'll be null if the resource cannot be found.</returns> /// <example> /// // Get an array of strings, similar to System.IO.File.ReadAllLines() /// Stream s = GetResourceStream("MyData.bin"); /// StreamReader r = new StreamReader(s); /// string str = r.ReadToEnd(); /// string[] lines = str.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); /// </example> static private Stream GetResourceStream(string resourceNameArg) { System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); string resourceName = assembly.GetName().Name + "." + resourceNameArg; // Expect a string of the form "MyApp.MyData.bin". System.IO.Stream stream = assembly.GetManifestResourceStream(resourceName); return stream; }
You need to login to post a comment.
