Posted By

jimfred on 05/02/11


Tagged

visual Studio


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

danfinrud55


Use a Visual Studio embedded resource to store binary blobs of data (like a ZIP file)


 / Published in: C#
 

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.

  1. /// <summary>
  2. /// Retrieve a byte array from an embedded resource.
  3. /// This function uses Assembly.GetManifestResourceStream() to read an embedded resource.
  4. /// The use of embedded resources allows the .EXE or assembly to be used as a container for
  5. /// data/blobs that might otherwise be stored in an external file.
  6. /// </summary>
  7. /// <param name="resourceNameArg">Name of file embedded in project, e.g. "MyData.bin"</param>
  8. /// <returns>An array of bytes containing the resource contents.</returns>
  9. /// <remarks>
  10. /// Instructions:
  11. /// 1) Add a file to the project e.g., MyData.bin, text.txt, database.mdb, final.hex etc.
  12. /// 2) For that file, set its Properties / Build Action = Embedded Resource.
  13. /// Other properties keep defaults (do not copy, blank tool, blank namespace).
  14. /// 3) At runtime, retrieve resource using this static function.
  15. ///
  16. /// Notes:
  17. /// 1) A re-build of the project includes any changes to the resource file - automatically.
  18. /// 2) The resource editor isn't used and doesn't show the embedded resource.
  19. /// 3) This technique can be used for 'blobs' or non-typical resources such as:
  20. /// Binary data, databases, information generated by external compilers/tools.
  21. /// 4) To get a list of resource names during development, inspect this:
  22. /// System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()
  23. /// The resource name is the embedded file, prepended with the app name e.g., "MyApplication.MyData.bin"
  24. /// and the name passed to this function would be "MyData.bin" (without the app name).
  25. /// 5) This function is static; can be accessed by forms, app etc.
  26. /// 6) For big resources, consider using the stream rather than a byte[].
  27. /// 7) resourceManager is an alternative approach to get a stream
  28. /// But, the resource name gets changed/mangled.
  29. /// ResourceManager resourceManager = new ResourceManager(assembly.GetName().Name + ".Properties.Resources", assembly);
  30. /// stream = resourceManager.GetStream(resourceNameArg);
  31. /// 8) A clumsy alternative to an embedded resource might involve converting
  32. /// data into C# code used to initialize a byte array e.g.:
  33. /// public static byte[] some_bin = new byte[]
  34. /// {
  35. /// 0x02, 0x07, 0xF3, 0xAA, 0xAA, ...
  36. /// };
  37. /// </remarks>
  38. /// <see>
  39. /// http://stackoverflow.com/questions/725915/how-to-access-a-binary-resource-in-a-c-application
  40. /// http://msdn.microsoft.com/en-us/library/aa287676%28v=vs.71%29.aspx
  41. /// </see>
  42. static private byte[] GetResourceBytes( string resourceNameArg )
  43. {
  44.  
  45. Stream stream = GetResourceStream(resourceNameArg);
  46.  
  47. if (stream == null)
  48. {
  49. return null; // resource not found.
  50. }
  51.  
  52. byte[] bytes = new byte[stream.Length];
  53.  
  54. stream.Read( bytes, 0, (int)stream.Length);
  55.  
  56. return bytes;
  57.  
  58. }
  59.  
  60. /// <summary>
  61. /// Retrieve a Stream from an embedded resource.
  62. /// This function uses Assembly.GetManifestResourceStream() to get a Stream for an embedded resource.
  63. /// The use of embedded resources allows the .EXE or assembly to be used as a container for data/blobs
  64. /// that might otherwise be stored in an external file.
  65. /// </summary>
  66. /// <param name="resourceNameArg">Name of file embedded in project, e.g. "MyData.bin"</param>
  67. /// <returns>A Stream for the embedded resource. It'll be null if the resource cannot be found.</returns>
  68. /// <example>
  69. /// // Get an array of strings, similar to System.IO.File.ReadAllLines()
  70. /// Stream s = GetResourceStream("MyData.bin");
  71. /// StreamReader r = new StreamReader(s);
  72. /// string str = r.ReadToEnd();
  73. /// string[] lines = str.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
  74. /// </example>
  75. static private Stream GetResourceStream(string resourceNameArg)
  76. {
  77. System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
  78.  
  79. string resourceName = assembly.GetName().Name + "." + resourceNameArg;
  80. // Expect a string of the form "MyApp.MyData.bin".
  81.  
  82. System.IO.Stream stream = assembly.GetManifestResourceStream(resourceName);
  83.  
  84. return stream;
  85. }

Report this snippet  

You need to login to post a comment.