
{"id":1530,"date":"2014-10-30T18:32:47","date_gmt":"2014-10-30T22:32:47","guid":{"rendered":"http:\/\/www.ikriv.com\/blog\/?p=1530"},"modified":"2014-10-30T18:32:47","modified_gmt":"2014-10-30T22:32:47","slug":"one-more-time-about-resource-vs-embeddedresource","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=1530","title":{"rendered":"One more time about Resource vs. EmbeddedResource"},"content":{"rendered":"<p>In Visual Studio you can set &#8220;Build Action&#8221; for a project item to &#8216;Resource&#8217; or &#8216;Embedded Resource&#8217;, among other things. The difference between the two is confusing, and exact up-to-date documentation is hard to find. (<a href=\"http:\/\/stackoverflow.com\/questions\/145752\/what-are-the-various-build-action-settings-in-vs-net-project-properties-and-wh\">This StackOverflow post<\/a> is probably the best, but not 100% accurate.) Today I became confused one more time, so I decided to clear up the matter and put an end to the confusion.<br \/>\n<!--more--><br \/>\nFirst of all, <b>both<\/b> kinds of resource are actually embedded in the executable. &#8216;EmbeddedResource&#8217; files are placed directly into the executable as manifest resources. All &#8216;Resource&#8217; files are put in a special structured manifest resource named &#8216;<code><i>ProjectName<\/i>.g.Resources<\/code>&#8216;.<\/p>\n<p><b>Embedded Resources<\/b><\/p>\n<p>Embedded resources are the same as manifest resources. You can see them as <code>.mresource<\/code> records in ILDASM. If in a project named <code>MyProject<\/code> you create a file named <code>Texts\\Guide.txt<\/code> and mark it as EmbeddedResource, the project output will have a manifest resource named <code>MyProject.Texts.Guide.txt<\/code>.<\/p>\n<p>Embedded resources can be enumerated by calling <code>Assembly.GetManifestResourceNames()<\/code> and read via <code>Assembly.GetManifestResourceStream()<\/code>.<\/p>\n<p><b>Resx files<\/b><\/p>\n<p>Embedded resource has no predefined structure: it is just a named blob of bytes. So called &#8220;.resx&#8221; file is a special kind of embedded resource. It is a string-to-object dictionary that maps a name to an object. The object may be a string, an image, an icon, or a blob of bytes. Resx files can be used for localization: in addition to the name each object may have a <i>culture<\/i>, e.g. &#8220;Neutral&#8221;, or &#8220;English (UK)&#8221;.<\/p>\n<p>You can add resx file to your project by right clicking on the project name, choosing <code>Add-&gt;New Item...<\/code> and then selecting <code>Visual C# Items-&gt;General-&gt;Resource file<\/code>. The entire resx file is placed into the executable as a single manifest resource. In the name of the manifest resource &#8220;<code>.resx<\/code>&#8221; extension is replaced with &#8220;<code>.resources<\/code>&#8220;.<\/p>\n<p>Resx files are accessed via the <code>System.Resources.ResourceManager<\/code> class. Note that the &#8220;base name&#8221; supplied to the constructor is the file name <b>without<\/b> extension. You can retrieve a particular resource by calling <code>ResourceManager.GetString()<\/code>, <code>ResourceManager.GetObject()<\/code>, or <code>ResourceManager.GetStream()<\/code>. You can get an entire dictionary for a particular culture via <code>ResourceManager.GetResourceSet()<\/code>.<\/p>\n<p><b>Resource Build Action<\/b><\/p>\n<p>Files marked with build action of &#8220;Resource&#8221; are added to a special resx file called <code><i>ProjectName<\/i>.g.resx<\/code>. This file is generated during the build, it is not part of the project. You can access content of the &#8216;Resource&#8217; files by creating an instance of  <code>ResourceManager<\/code> and calling <code>GetStream(<i>filename<\/i>)<\/code>. Additionally, in WPF applications you can access these resources via <code>Application.GetResourceStream()<\/code> in C# and via things like <code>&lt;Image Source=\"<i>filepath<\/i>\" \/&gt;<\/code> in XAML.<\/p>\n<p><b>Project resources<\/b><\/p>\n<p>When you edit project resources on the Resources tab of the project property pages, Visual Studio creates another special resx file named <code>Properties\\Resources.resx<\/code>. Corresponding manifest resource name is <code><i>ProjectName<\/i>.Properties.Resources.resources<\/code> (yes, &#8220;resources&#8221; is repeated twice, one comes from the file name, and the other from the &#8220;.resx&#8221; extension being replaced with &#8220;.resources&#8221;). Visual Studio also creates a file called <code>Resources.Designer.cs<\/code> that contains code for easy access to the dictionary items. E.g. a string named &#8220;WindowHeader&#8221; may be accessed simply as <code>Properties.Resource.WindowHeader<\/code>, which will call <code>ResourceManager<\/code> behind the scenes.<\/p>\n<p><b>Putting it all together<\/b><\/p>\n<p>A sample program (<a href=\"http:\/\/www.ikriv.com\/\/blog\/wp-content\/uploads\/2014\/10\/ResourceTypes.zip\">ResourceTypes.zip<\/a>) demonstrates different kinds of resources and how to access them.<\/p>\n<p>It has<\/p>\n<ul>\n<li>One file with build action EmbeddedResource named <code>EmbeddedResourceFile.txt<\/code><\/li>\n<li>Two files with BuildAction &#8216;Resource&#8217; named <code>ResourceFile.txt<\/code> and <code>ResourceFile2.txt<\/code><\/li>\n<li> Standard project resx file <code>Properties\\Resources.resx<\/code> with some key-value pairs.\n<\/ul>\n<p>The output shows that there are three manifest resources, one of which is a simple file, and two others are resx resources with additional internal structure:<\/p>\n<pre>Manifest resources\n        ResourceTypes.g.resources:\n                '???\u263a   ?   lSystem.Resources.ResourceRea...'\n        ResourceTypes.Properties.Resources.resources:\n                '???\u263a   ?   lSystem.Resources.ResourceRea...'\n        ResourceTypes.EmbeddedResourceFile.txt:\n                'EmbeddedResourceFile.txt: it is compiled...'\n\nItems in ResourceTypes.g:\n        resourcefile2.txt: Stream: ResourceFile2.txt: it is compiled as 'Re...\n        resourcefile.txt: Stream: ResourceFile.txt: it is compiled as 'Res...\n\nItems in ResourceTypes.Properties.Resources:\n        MyKey: MyValue\n        Key2: Value2<\/pre>\n<p><a style=\"display:none\" href=\"http:\/\/www.codeproject.com\/script\/Articles\/BlogFeedList.aspx?amid=1181663\" rel=\"tag\">CodeProject<\/a><\/p>\n<p><b>Conclusion<\/b><\/p>\n<p>It would not hurt if the names were less confusing and the documentation were clearer and easier to find. Yet, the &#8220;resources&#8221; topic has a pretty long history, so I guess this is the best we could get for the money.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Visual Studio you can set &#8220;Build Action&#8221; for a project item to &#8216;Resource&#8217; or &#8216;Embedded Resource&#8217;, among other things. The difference between the two is confusing, and exact up-to-date <a href=\"https:\/\/ikriv.com\/blog\/?p=1530\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[3,12],"tags":[],"class_list":["entry","author-ikriv","has-more-link","post-1530","post","type-post","status-publish","format-standard","category-dotnet","category-wpf"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1530"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1530\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}