<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Geekality &#187; Embedded Resources</title>
	<atom:link href="http://www.geekality.net/tag/embedded-resources/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.geekality.net</link>
	<description>With a hint of Social Ineptitude</description>
	<lastBuildDate>Tue, 27 Jul 2010 16:15:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to use assembly embedded resources</title>
		<link>http://www.geekality.net/2008/12/27/how-to-use-assembly-embedded-resources/</link>
		<comments>http://www.geekality.net/2008/12/27/how-to-use-assembly-embedded-resources/#comments</comments>
		<pubDate>Sat, 27 Dec 2008 16:13:16 +0000</pubDate>
		<dc:creator>Torleif</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Embedded Resources]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.geekality.net/?p=62</guid>
		<description><![CDATA[After some digging around, I found that it actually wasn&#8217;t that difficult at all. Getting it in Putting something in an assembly as an embedded resource is pretty easy. At least if you are using Visual Studio. Just add the &#8230; <a href="http://www.geekality.net/2008/12/27/how-to-use-assembly-embedded-resources/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After some digging around, I found that it actually wasn&#8217;t that difficult at all.</p>
<h2>Getting it in</h2>
<p>Putting something in an assembly as an embedded resource is pretty easy. At least if you are using Visual Studio. Just add the file to your project, click on it, and then under Properties set Build Action to Embedded Resource. And thats it!</p>
<h2>Getting it out</h2>
<p>Lets say we want an image called <em>hello.png</em> in a folder called <em>Wopdidoo</em> as a Stream.</p>
<p>If we are executing code in the same assembly, we can do as follows:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Assembly assembly <span style="color: #008000;">=</span> Assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetExecutingAssembly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
Stream imageStream <span style="color: #008000;">=</span> assembly<br />
&nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">GetManifestResourceStream</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;DefaultNameSpaceOfAssembly.Wopdidoo.hello.png&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>If you are not executing code in the same assembly you just have to get that assembly reference in a different way. I often use <code class="codecolorer csharp default"><span class="csharp">Assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetAssembly</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>T<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span></span></code>, where T is some type you know exists in the same assembly as the file you want. The rest is the same.</p>
<p>As far as I know, you use the stream as any other stream. Not sure if it is writable though? Probably not&#8230; let me know if you have some brilliant clues on that matter <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
<p> <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':?:' title=':?:' class='wp-smiley smiley-23' /> Remember to Dispose it when you are done.</p>
<h2>Finding it</h2>
<p>I sometimes find it a bit difficult to figure out that string which identifies the resource. I then often use the following code to &#8220;find&#8221; it:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> s <span style="color: #0600FF; font-weight: bold;">in</span> assembly<span style="color: #008000;">.</span><span style="color: #0000FF;">GetManifestResourceNames</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">Debug</span><span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>s<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p>It basically just scans through all the resource names and prints them out to the debug console <img src='http://www.geekality.net/wp-includes/images/blank.gif' alt=':)' title=':)' class='wp-smiley smiley-1' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.geekality.net/2008/12/27/how-to-use-assembly-embedded-resources/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
