{"id":875,"date":"2012-05-02T09:21:08","date_gmt":"2012-05-02T07:21:08","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=875"},"modified":"2012-05-02T09:21:08","modified_gmt":"2012-05-02T07:21:08","slug":"loading-custom-resources","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2012\/05\/loading-custom-resources\/","title":{"rendered":"Loading custom resources"},"content":{"rendered":"<p>The idea to use &#8216;custom&#8217; resources in a .Net app might not make sense for all &#8211; and there may be times when it does not make sense at all since .Net apps (using VS.Net) have a built-in editor to manage embedded resources for an app. But&#8230; {here it comes} there could be some more flexibility to do it yourself like the &#8216;resource files&#8217; does not have to be all located in the same directory (from a source point of view) and you can choose whether you want to include the files separately from the main executable when distributing the app.<\/p>\n<p>First off, to you have to add a file (the resource) to the project &#8211; either by using the &#8216;Add new Item&#8217; \u00a0or &#8216;Add Existing Item&#8217;\u00a0context menus. Then under &#8216;Properties&#8217; of the file &#8216;Build Action&#8217; to &#8216;Embedded Resource&#8217;. Optionally you can set the &#8216;Copy to Output Directory&#8217; but for this blog entry it won&#8217;t be used. Take note of the relative directory name (to the project&#8217;s root) since that will become part of the name of the resource &#8211; including the exact &#8216;case&#8217; of the letters. For example, adding a text file named &#8216;<strong><em>CreateTables.sql<\/em><\/strong>&#8216; under a project directory &#8216;<strong><em>SQL<\/em><\/strong>&#8216; would result in a resource name &#8216;<strong><em>&lt;Default namespace&gt;.SQL.CreateTables.sql<\/em><\/strong>&#8216;. Yes that is correct, the default namespace of the project is prepended to the full resource name.<\/p>\n<p>Now to use the resource file (actually only the content since the file name has no use inside the project) you can use the following code:<\/p>\n<blockquote><p><span style=\"color: #0000ff;\">public static string<\/span> GetResourceByName(<span style=\"color: #0000ff;\">string<\/span> name)<br \/>\n{<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">Stream<\/span> s = System.Reflection.<span style=\"color: #0000ff;\">Assembly<\/span>.GetExecutingAssembly().GetManifestResourceStream(name);<br \/>\nSystem.IO.<span style=\"color: #0000ff;\">StreamReader<\/span> sr = new <span style=\"color: #0000ff;\">StreamReader<\/span>(s);<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #0000ff;\">return<\/span> sr.ReadToEnd();<\/p>\n<p>}<\/p>\n<p>&#8230;<\/p>\n<p><span style=\"color: #0000ff;\">string<\/span> sql = GetResourceByName(&#8216;<span style=\"color: #008000;\">myNamespace.SQL.CreateTables.sql<\/span>&#8216;);<\/p><\/blockquote>\n<p>One small note: this example code does no error checking.<\/p>\n<p>You can make the resource name a constant inside the project or create an enum using something like a string value attribute to match the enum entries to a string or something. Also, this example assumes the content is a text file but you can use the same principle of any type of content (like images, audio, video or whatever).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The idea to use &#8216;custom&#8217; resources in a .Net app might not make sense for all &#8211; and there may be times when it does not make sense at all since .Net apps (using VS.Net) have a built-in editor to &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2012\/05\/loading-custom-resources\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,8,9,303],"class_list":["post-875","post","type-post","status-publish","format-standard","hentry","category-development","tag-net","tag-c","tag-code","tag-development"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/875","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=875"}],"version-history":[{"count":4,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/875\/revisions"}],"predecessor-version":[{"id":879,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/875\/revisions\/879"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}