<?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>Huddled Masses &#187; .Net</title>
	<atom:link href="http://huddledmasses.org/tag/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Long-Path Module (Experimental.IO)</title>
		<link>http://huddledmasses.org/long-path-module-experimental-io/</link>
		<comments>http://huddledmasses.org/long-path-module-experimental-io/#comments</comments>
		<pubDate>Fri, 04 Feb 2011 06:01:23 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[IO]]></category>
		<category><![CDATA[Path]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1651</guid>
		<description><![CDATA[Thanks to a late night on my part, and the efforts of the developers on the Microsoft BCL project who wrote the Experimental.IO LongPath assembly &#8230; with no further ado, I present a really basic module for dealing with long file paths. There&#8217;s no help yet, and very little error handling. But if you needed [...]]]></description>
			<content:encoded><![CDATA[	<p>Thanks to a late night on my part, and the efforts of the developers on the <a href="http://bcl.codeplex.com/">Microsoft <span class="caps">BCL</span> project</a> who wrote the Experimental.IO <a href="http://bcl.codeplex.com/releases/view/42783">LongPath</a> assembly &#8230; with no further ado, I present a really basic module for dealing with long file paths. </p>

	<p>There&#8217;s no help yet, and very little error handling. But if you needed long paths (I know one of you did, because Jacques Willemen asked for it), then here are: Get-LongPath, Copy-LongPath, Move-LongPath, Remove-LongPath, Get-ContentLongPath, Set-ContentLongPath &#8230; and &#8230; uhm &#8230; Format-Color</p>

	<p><a href="http://HuddledMasses.org/downloads/Experimental.IO.zip">Download the Experimental.IO Long Path Module</a></p>

	<p>The script source is <a href="http://poshcode.org/2488">on PoshCode</a> so please help me: write the next version of this module by contributing there.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/long-path-module-experimental-io/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XPath and Namespaces in PowerShell</title>
		<link>http://huddledmasses.org/xpath-and-namespaces-in-powershell/</link>
		<comments>http://huddledmasses.org/xpath-and-namespaces-in-powershell/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 15:44:10 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Namespace]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Xml]]></category>
		<category><![CDATA[XPath]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1326</guid>
		<description><![CDATA[Although there are a few &#8220;CSS Selector&#8221; libraries, most browsers haven&#8217;t even implemented CSS3 selectors, never mind frameworks like .Net or scripting languages like Javascript or PowerShell so XPath remains the most powerful way to deal with finding specific data in an XML file, and by extension, XHTML and even HTML files (if you can [...]]]></description>
			<content:encoded><![CDATA[	<p>Although there are a few &#8220;<span class="caps">CSS</span> Selector&#8221; libraries, most <em>browsers</em> haven&#8217;t even implemented CSS3 selectors, never mind frameworks like .Net or scripting languages like Javascript or PowerShell  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' />  so XPath remains the most powerful way to deal with finding specific data in an <span class="caps">XML</span> file, and by extension, <span class="caps">XHTML</span> and even <span class="caps">HTML</span> files (if you can convert them using something like <a href="http://code.msdn.microsoft.com/SgmlReader">SgmlReader</a>) is to use XPath queries.</p>

	<p>There are a lot of <a href="http://www.w3schools.com/xpath/">XPath tutorials</a> around the web, so there&#8217;s no need for me to get into that very much, but I just wanted to write a brief note about using XPath with documents that have namespaces (particularly, from .Net).  The problem is that in order to select nodes that have a namespace assigned, you <strong>must</strong> use a namespace prefix and a <code>NamespaceManager</code>. So even if it&#8217;s the default namespace, if there&#8217;s an xmlns=&#8221;...&#8221; on the document, you have to create and use a prefix.</p>

	<p>The bottom line is this: if you have an <span class="caps">XML</span> document that looks like this:</p>

	<div class="xml code xml" style="font-family:monospace;"><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;event</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/win/2004/08/events/event&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;eventid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>100<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/eventid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;level<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/level<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;task<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4002<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/task<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;opcode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>34<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/opcode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;eventdata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;BootTsVersion&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;BootStartTime&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>2009-11-22T08:45:11.640400200Z<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;BootEndTime&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>2009-11-22T08:48:23.432178500Z<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;SystemBootInstance&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>18<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;data</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;UserBootInstance&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>15<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/data<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/eventdata<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/event<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp;</div>

	<p>That bit at the top where it says: <code>xmlns=&#34;http://schemas.microsoft.com/win/2004/08/events/event&#34;</code> is assigning a <strong>default</strong> namespace. Sometimes you&#8217;ll see something like this (eg: in <span class="caps">RSS</span>):</p>

	<div class="xml code xml" style="font-family:monospace;"><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rss</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span> <span style="color: #000066;">xmlns:media</span>=<span style="color: #ff0000;">&quot;http://search.yahoo.com/mrss/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;channel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Buttons<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>http://www.flickr.com/photos/jaykul/4114286262/<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Quick cut-n-paste from a screenshot<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pubdate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Tue, 17 Nov 2009 20:46:31 -0800<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pubdate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;guid</span> <span style="color: #000066;">ispermalink</span>=<span style="color: #ff0000;">&quot;false&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>tag:flickr.com,2004:/photo/4114286262<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/guid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;media:content</span> <span style="color: #000066;">url</span>=<span style="color: #ff0000;">&quot;http://farm3.static.flickr.com/2552/4114286262_001e2f8905_o.png&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;image/jpeg&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;157&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;337&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;media:title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Buttons<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/media:title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;media:description</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;html&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #ddbb00;">&amp;lt;</span>p<span style="color: #ddbb00;">&amp;gt;</span>Quick cut-n-paste from a screenshot into your mockup<span style="color: #ddbb00;">&amp;lt;</span>/p<span style="color: #ddbb00;">&amp;gt;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/media:description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;media:thumbnail</span> <span style="color: #000066;">url</span>=<span style="color: #ff0000;">&quot;http://farm3.static.flickr.com/2552/4114286262_cba8aaf967_s.jpg&quot;</span> <span style="color: #000066;">height</span>=<span style="color: #ff0000;">&quot;75&quot;</span> <span style="color: #000066;">width</span>=<span style="color: #ff0000;">&quot;75&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;media:credit</span> <span style="color: #000066;">role</span>=<span style="color: #ff0000;">&quot;photographer&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Jaykul<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/media:credit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;media:category</span> <span style="color: #000066;">scheme</span>=<span style="color: #ff0000;">&quot;urn:flickr:tags&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>screenshot snagit<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/media:category<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/media:thumbnail<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/media:content<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/channel<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/rss<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div>

	<p>This one assigns a specific prefix &#8220;media&#8221; to the namespace url &#8220;http://search.yahoo.com/mrss/&#8221; ...</p>

	<h3>Selecting nodes with namespaces</h3>

	<p>In either case, if you want to select a node that&#8217;s assigned to a namespace (which is <span class="caps">ALL</span> the nodes in the first example, but just the ones that start with media: in the second example) in .net, you have to specify the namespace in order to select those nodes with XPath.  PowerShell 2.0 has a <code>Select-Xml</code> cmdlet which accepts <code>-Namespaces</code> as a parameter: you simply provide a hashtable of names to urls.</p>

	<p>If you had loaded the first document above into <code>$xml</code>, you could select the BootStartTime and BootEndTime using an XPath query like this: <code>//e:Data[</code>Name = &#8216;BootStartTime&#8217; or <code>Name = &#39;BootEndTime&#39;]</code> but we have to <span class="caps">DEFINE</span> that &#8220;e&#8221; namespace.  To do so using <code>Select-Xml</code> you just pass it into the command</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$xpath</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033;">Start</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//e:Data[@Name = 'BootStartTime']/text()&quot;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">End</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//e:Data[@Name = 'BootEndTime']/text()&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #660033; font-weight: bold;">$ns</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span> e <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;http://schemas.microsoft.com/win/2004/08/events/event&quot;</span> <span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Start</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Select-<span style="font-style: normal;">Xml</span></span> <span style="color: #000066;">-Xml</span> <span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #000066;">-XPath</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #660033;">Start</span> <span style="color: #000066;">-Namespace</span> <span style="color: #660033; font-weight: bold;">$ns</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033;">Select</span> <span style="color: #000066;">-Expand</span> Node <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-expand</span> Value<br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$End</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Select-<span style="font-style: normal;">Xml</span></span> <span style="color: #000066;">-Xml</span> <span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #000066;">-XPath</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #666699; font-weight: bold;">End</span> <span style="color: #000066;">-Namespace</span> <span style="color: #660033; font-weight: bold;">$ns</span> <span style="color: #66cc66;">|</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033;">Select</span> <span style="color: #000066;">-Expand</span> Node <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-expand</span> Value<br />
<br />
<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$End</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$Start</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># Displays: 00:03:11.7917783</span><br />
&nbsp;</div>

	<p>Of course, you don&#8217;t have to use Select-Xml, you can do this in plain .Net without cmdlets (and this is what you would <strong>have</strong> to do in C#). In fact, depending on the situation, it might even be simpler:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$xpath</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033;">Start</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//e:data[@name = 'BootStartTime']/text()&quot;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">End</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//e:data[@name = 'BootEndTime']/text()&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$ns</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> <span style="color: #003366; font-weight: bold;">Xml</span>.<span style="color: #003366;">XmlNamespaceManager</span> <span style="color: #660033; font-weight: bold;">$xml</span>.<span style="color: #003366;">NameTable</span><br />
<span style="color: #660033; font-weight: bold;">$ns</span>.<span style="color: #003366;">AddNamespace</span><span style="color: #333;">&#40;</span> <span style="color: #009900;">&quot;e&quot;</span>, <span style="color: #009900;">&quot;http://schemas.microsoft.com/win/2004/08/events/event&quot;</span> <span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Start</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$xml</span>.<span style="color: #003366;">SelectSingleNode</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #660033;">Start</span>, <span style="color: #660033; font-weight: bold;">$ns</span> <span style="color: #333;">&#41;</span>.<span style="color: #003366;">Value</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$End</span> &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$xml</span>.<span style="color: #003366;">SelectSingleNode</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #666699; font-weight: bold;">End</span>, <span style="color: #660033; font-weight: bold;">$ns</span> <span style="color: #333;">&#41;</span>.<span style="color: #003366;">Value</span><br />
<br />
<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$End</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$Start</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># Displays: 00:03:11.7917783</span><br />
&nbsp;</div>

	<h3>Selecting nodes regardless of namespaces</h3>

	<p>There are, however, two ways that you could avoid specifying the namespaces.  The first is to just avoid specifying the node name at all. In that first example, that would be fairly easy, because the &#8220;BootStartTime&#8221; and &#8220;BootEndTime&#8221; names are unique to the nodes we&#8217;re interested in (even if there were boatloads of identical events, you&#8217;ll never have a Name=&#8220;BootStartTime&#8221; attribute on the <system> tag, for instance).  The second way is to filter using the local-name function, and specify just the &#8220;local&#8221; name (which is the non-namespace-qualified part of the name).</p>

	<p>So to ignore the tag name, we can just use a * for a wildcard.  The only real difference is that we don&#8217;t need the namespace, and the XPath pattern will be different:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$xpath</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033;">Start</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//*[@Name = 'BootStartTime']/text()&quot;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">End</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//*[@Name = 'BootEndTime']/text()&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Start</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Select-<span style="font-style: normal;">Xml</span></span> <span style="color: #000066;">-xml</span> <span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #660033;">Start</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Node</span>.<span style="color: #003366;">Value</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$End</span> &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Select-<span style="font-style: normal;">Xml</span></span> <span style="color: #000066;">-xml</span> <span style="color: #660033; font-weight: bold;">$xml</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #666699; font-weight: bold;">End</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Node</span>.<span style="color: #003366;">Value</span><br />
<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$End</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$Start</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># Displays: 00:03:11.7917783</span><br />
&nbsp;</div>

	<p>Or to specify the local name, we can use the Local-Name function.  Again, we don&#8217;t need a namespace, we are just changing the XPath to be a more specific match:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$xpath</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033;">Start</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//*[local-name() = 'Data' and @Name = 'BootStartTime']/text()&quot;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">End</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;//*[local-name() = 'Data' and @Name = 'BootEndTime']/text()&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Start</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$xml</span>.<span style="color: #003366;">SelectSingleNode</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #660033;">Start</span> <span style="color: #333;">&#41;</span>.<span style="color: #003366;">Value</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$End</span> &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$xml</span>.<span style="color: #003366;">SelectSingleNode</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$xpath</span>.<span style="color: #666699; font-weight: bold;">End</span> <span style="color: #333;">&#41;</span>.<span style="color: #003366;">Value</span><br />
<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$End</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$Start</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># Displays: 00:03:11.7917783</span><br />
&nbsp;</div></system></p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/1b73f9a4-bb71-44bc-844b-1302b5a0151a/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=1b73f9a4-bb71-44bc-844b-1302b5a0151a" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/xpath-and-namespaces-in-powershell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Custom IComparers in PowerShell (and Add-Type for v1)</title>
		<link>http://huddledmasses.org/custom-icomparers-in-powershell-and-add-type-for-v1/</link>
		<comments>http://huddledmasses.org/custom-icomparers-in-powershell-and-add-type-for-v1/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 04:53:44 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[BinarySearch]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Searching]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=878</guid>
		<description><![CDATA[Someone asked the question in #PowerShell (on irc.Freenode.net): How do I find an item (in an array) based on one of it&#8217;s properties? Actually, the question was rather more complicated than that. They were importing a bunch of users from a csv file, and wanted to sort them and search them based on a specific [...]]]></description>
			<content:encoded><![CDATA[	<p>Someone asked the question in #PowerShell (on irc.Freenode.net):</p>

	<h3>How do I find an item (in an array) based on one of it&#8217;s properties?</h3>

	<p>Actually, the question was rather more complicated than that. They were importing a bunch of users from a csv file, and wanted to sort them and search them based on a specific column. There are many ways to skin this cat. Imagine that you have a <span class="caps">CSV</span> file, and have imported it, like so:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Content</span></span> users.<span style="color: #003366;">csv</span> @<span style="color: #009900;">&quot;<br />
LastName, FirstName, UserName, Url<br />
Bennett, Joel, Jaykul, http://HuddledMasses.org<br />
Rottenberg, Hal, HalR9000, http://halr9000.com<br />
Hicks, Jeffrey, SapienScripter, http://blog.sapien.com/<br />
&quot;</span>@<br />
<br />
<span style="color: #660033; font-weight: bold;">$users</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Csv</span></span> .\users.<span style="color: #003366;">csv</span></div>

	<p>Now, imagine that the <span class="caps">CSV</span> file has thousands of users in it, and that you need to not only sort the data by first name or last name on demand, but you also need to pull users from the list (by name) on demand.</p>

	<p>These are trivial tasks in PowerShell:<span id="more-878"></span></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$users</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$users</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Sort</span> Lastname, Firstname<br />
<span style="color: #666666; font-style: italic;"># or </span><br />
<span style="color: #660033; font-weight: bold;">$users</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$users</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Sort</span> Firstname, Lastname<br />
<br />
<span style="color: #666699; font-weight: bold;">return</span> <span style="color: #660033; font-weight: bold;">$users</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Where</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Firstname</span> <span style="color: #000066;">-eq</span> <span style="color: #009900;">&quot;Bob&quot;</span> <span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> <span style="color: #000066;">-First</span> <span style="color: #cc66cc;">1</span><br />
&nbsp;</div>

	<p>This <em>works</em> but will have some performance issues when the list gets too long, because your search algorithm is &#8230; well, we could say it&#8217;s O(n), or just say it&#8217;s slow, because you&#8217;re basically going through the whole list every time. And I do mean the <span class="caps">WHOLE</span> list. You were looking for &#8220;Bob&#8221; ... but the <code>Where-Object</code> cmdlet doesn&#8217;t know your data is sorted, and doesn&#8217;t know you only want one user, so it&#8217;s going to iterate the whole list, because <code>Select-Object</code> can&#8217;t signal it to stop.</p>

	<p>There are several ways to solve this, but what it comes down to is that you need a (better) search algorithm than Where-Object can implement: simply put, you&#8217;d like to use a binary search, since the data is already sorted.</p>

	<h4>Binary Search</h4>

	<p>I&#8217;m not going to get into this much, but to put it simply: a binary search algorithm is pretty much the fastest search you can have, but it depends on the data being pre-sorted in order.  It works by starting in the middle, and comparing the middle item to the item you are searching for. If what you&#8217;re searching for comes <span class="caps">BEFORE</span> that item, then it discards the second half of the items, and examines the middle item of the first half.  In this way, it eliminates half the items each time, and returns the correct item (if it exists) in roughly (log n) steps.</p>

	<h4>So how do we do that in PowerShell?</h4>

	<p>Well, that&#8217;s easy: Just use the static method: <code>[Array]::BinarySearch</code>&#8230; The only problem is, in order for BinarySearch to work, the items have to be not only sorted, but comparable.  Because we&#8217;re trying to compare PSObjects, you can&#8217;t just compare two items from the $users array and determine the correct order&#8230;</p>

	<p>If you wanted to use <code>[Array]::Sort</code> you could use an overload that lets you pass in a second array (of comparable items) to serve as the keys for the non-comparable items, but this won&#8217;t work for BinarySearch:<br />
<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Array</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Sort</span><span style="color: #333;">&#40;</span> $<span style="color: #333;">&#40;</span>@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$users</span><span style="color: #66cc66;">|%</span><span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Firstname</span><span style="color: #66cc66;">+</span><span style="color: #009900;">&quot; &quot;</span><span style="color: #66cc66;">+</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Lastname</span><span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span>,<span style="color: #660033; font-weight: bold;">$users</span> <span style="color: #333;">&#41;</span></div>

	<p>So you need what is an &#8220;IComparer.&#8221;  That is, a class which implements a &#8220;Compare&#8221; method which can be used to determine the order of the objects.  Sadly, this requires a custom type &#8212; something you can&#8217;t do in pure PowerShell script, believe it or not.  PowerShell does not have the ability to create custom classes (which is how you can tell it&#8217;s not a real programming language), so when you need a custom type in PowerShell you have to drop to another .Net language like C# or Python. </p>

	<p>In PowerShell 2.0 there is a cmdlet &#8220;Add-Type&#8221; which can take a string representing C# source-code, and compile it in memory for immediate use, but in PowerShell 1.0 you need to have a script version of it, which I called New-Type, to avoid confusion because it only handles one of Add-Type&#8217;s four usage models:</p>

	<p><script type="text/javascript" src="http://PoshCode.org/embed/720"></script></p>

	<p>Once you have Add-Type, you can create a ScriptBlock-based implementation of IComparer, by passing this as a string:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Type</span></span> @<span style="color: #009900;">&quot;</span></div><br />
<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Management.Automation</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">sealed</span> <span style="color: #FF0000;">class</span> ScriptBlockComparer <span style="color: #008000;">:</span> IComparer<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; ScriptBlock _Comparer<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> ScriptBlockComparer<span style="color: #000000;">&#40;</span>ScriptBlock Comparer<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> ComparerScript <span style="color: #008000;">=</span> Comparer<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> ScriptBlock ComparerScript<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _Comparer<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;set <span style="color: #000000;">&#123;</span> _Comparer <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Compare<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> x, <span style="color: #FF0000;">object</span> y<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">try</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>ComparerScript.<span style="color: #0000FF;">Invoke</span><span style="color: #000000;">&#40;</span>x, y<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">BaseObject</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">catch</span><span style="color: #000000;">&#40;</span>Exception ex<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> InvalidOperationException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Comparer Script failed to return an integer!&quot;</span>, ex<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div><br />
<div class="posh code posh" style="font-family:monospace;"><span style="color: #009900;">&quot;@</span></div>

	<p>So now you have everything you need, and you can write your custom comparer, and do binary search. The one thing you need to be aware of is that when you&#8217;re doing binary search, you&#8217;re going to want to pass in the thing you&#8217;re searching for (ie: the first name) not an Object with a FirstName property. So your binary search needs a case for when the second parameter is a string:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$cmp</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> ScriptBlockComparer <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$one</span>, <span style="color: #660033; font-weight: bold;">$two</span><span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$two</span> <span style="color: #000066;">-isnot</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">throw</span> <span style="color: #009900;">&quot;This comparer expects to compare objects to strings&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$first</span>,<span style="color: #660033; font-weight: bold;">$last</span><span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$two</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Split</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot; &quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$ord</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$one</span>.<span style="color: #003366;">Firstname</span>.<span style="color: #003366;">CompareTo</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$first</span><span style="color: #333;">&#41;</span> <br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$ord</span> <span style="color: #000066;">-eq</span> <span style="color: #cc66cc;">0</span> <span style="color: #000066;">-and</span> <span style="color: #660033; font-weight: bold;">$last</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$ord</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$one</span>.<span style="color: #003366;">LastName</span>.<span style="color: #003366;">CompareTo</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$last</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">return</span> <span style="color: #660033; font-weight: bold;">$ord</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Now you can use this to search</span><br />
<span style="color: #660033; font-weight: bold;">$users</span><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Array</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">BinarySearch</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$users</span>, <span style="color: #009900;">&quot;Joel&quot;</span>, <span style="color: #660033; font-weight: bold;">$cmp</span> <span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666666; font-style: italic;"># Or this</span><br />
<span style="color: #660033; font-weight: bold;">$users</span><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Array</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">BinarySearch</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$users</span>, <span style="color: #009900;">&quot;Joel Bennett&quot;</span>, <span style="color: #660033; font-weight: bold;">$cmp</span> <span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp;</div>

	<p>Now, you may not think this is a big deal, so let me just share with you the results of searching a collection of about 49000 users:</p>

<table>
<tr><th>Duration(Sec)</th><th>Commmand</th></tr>
<tr><td>6.74000</td><td>$users = $users | Sort FirstName, LastName</td></tr>
<tr><td>6.68200</td><td>$users | Where { $_.FirstName -eq &#8220;Joel&#8221; }</td></tr>
<tr><td>0.02900</td><td>$users[[Array]::BinarySearch( $users, &#8220;Joel&#8221;, $cmp )</td></tr>
</table>

	<p>What you see here is two things:
	<ol>
		<li>Searching for a single user using <code>Where-Object</code> takes pretty much as long as (re)sorting the whole array, because every single item must be checked.</li>
	</ol>
	<ol>
		<li>Using BinarySearch rocks  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':D' class='wp-smiley' /> </li>
	</ol><br />
What you might not notice is that <code>[Array]::BinarySearch</code> returns an index, not the item &#8230; so it&#8217;s particularly useful if you need to find the first of something, etc. because you can just increment the result. </p>

	<p><strong>BinarySearch is not always faster</strong>: If your array is initially unsorted, the requirement to pre-sort the array costs basically the same as any speedup you might get for a single search. Also, because of the call to the script, if the array is <strong>very</strong> small, like the four users in the sample above, it&#8217;s actually faster to just loop through them (20ms versus 24ms on my PC). The BinarySearch starts paying off pretty quickly as you scale up the number of searches and records, but it <strong>does take <em>both</em></strong> more records (the break-even point for just the search is about 10, in this contrived example), and more searches (if you have to eat the cost of the initial sort).</p>

	<p>In any case, New-Type certainly has many other uses, and the <strong>ScriptBlockComparer</strong> can be used for other things as well &#8230; although I will say that as far as I can tell, sorting is almost always better performed with the sort cmdlet than with [Array]::Sort and an IComparer.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/custom-icomparers-in-powershell-and-add-type-for-v1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NativeConsole.cs &#8211; Run console apps and get their output</title>
		<link>http://huddledmasses.org/nativeconsolecs-run-console-apps-and-get-their-output/</link>
		<comments>http://huddledmasses.org/nativeconsolecs-run-console-apps-and-get-their-output/#comments</comments>
		<pubDate>Fri, 05 Sep 2008 18:53:18 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Console]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=649</guid>
		<description><![CDATA[To those of you who are not software developers: feel free to skip this post A while ago I wrote a little class for calling console apps from a .Net application, and I&#8217;ve been using it in several of my apps (most notably in PoshConsole) and it works great, but since the only place I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[	<p>To those of you who are not software developers: feel free to skip this post  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>

	<p>A while ago I wrote a little class for calling console apps from a .Net application, and I&#8217;ve been using it in several of my apps (most notably in <a href="http://CodePlex.com/PoshConsole">PoshConsole</a>) and it works great, but since the only place I&#8217;ve really published it is in PoshConsole, I thought I&#8217;d write it up here, and share it with you &#8230;</p>

	<p>Basically, it&#8217;s a slick invisible event-based wrapper around the Windows native console.  What I mean is, it calls AllocConsole when it&#8217;s instantiated to create a native console, and hides the console window so it doesn&#8217;t show up. This allows you to run any console app you need to from within your app without having it popup a black window  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' />  Note this doesn&#8217;t let you run graphical consoles like <span class="caps">EDIT</span>.<span class="caps">COM</span>, but it can handle interactive apps like cmd.exe, batch files, or ftp.exe).  All you really have to do is create one of my NativeConsole objects, handle its WriteOutputLine and WriteErrorLine events &#8230; and use its WriteInput method to send input or commands to the console app.</p>

	<p>You can check out how it works in my WPF-based PoshConsole, and you can get the latest version of it from that project as well (it&#8217;s in \trunk\Huddled\Interop\NativeConsole.cs) but for now, here&#8217;s the single file source code, with a more liberal set of licenses than I allow for PoshConsole.<span id="more-649"></span></p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #008080; font-style: italic;">// Copyright (c) 2008 Joel Bennett</span><br />
<br />
<span style="color: #008080; font-style: italic;">// Permission is hereby granted, free of charge, to any person obtaining a copy </span><br />
<span style="color: #008080; font-style: italic;">// of this software and associated documentation files (the &quot;Software&quot;), to deal</span><br />
<span style="color: #008080; font-style: italic;">// in the Software without restriction, including without limitation the rights </span><br />
<span style="color: #008080; font-style: italic;">// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell </span><br />
<span style="color: #008080; font-style: italic;">// copies of the Software, and to permit persons to whom the Software is </span><br />
<span style="color: #008080; font-style: italic;">// furnished to do so, subject to the following conditions:</span><br />
<br />
<span style="color: #008080; font-style: italic;">// The above copyright notice and this permission notice shall be included in </span><br />
<span style="color: #008080; font-style: italic;">// all copies or substantial portions of the Software.</span><br />
<br />
<span style="color: #008080; font-style: italic;">// THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR </span><br />
<span style="color: #008080; font-style: italic;">// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, </span><br />
<span style="color: #008080; font-style: italic;">// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE </span><br />
<span style="color: #008080; font-style: italic;">// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER </span><br />
<span style="color: #008080; font-style: italic;">// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span><br />
<span style="color: #008080; font-style: italic;">// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE </span><br />
<span style="color: #008080; font-style: italic;">// SOFTWARE.</span><br />
<br />
<span style="color: #008080; font-style: italic;">// *****************************************************************************</span><br />
<span style="color: #008080; font-style: italic;">// NOTE: YOU MAY *ALSO* DISTRIBUTE THIS FILE UNDER ANY OF THE FOLLOWING LICENSES:</span><br />
<span style="color: #008080; font-style: italic;">// BSD: &nbsp; http://opensource.org/licenses/bsd-license.php</span><br />
<span style="color: #008080; font-style: italic;">// MIT: &nbsp; http://opensource.org/licenses/mit-license.html</span><br />
<span style="color: #008080; font-style: italic;">// Ms-PL: http://opensource.org/licenses/ms-pl.html</span><br />
<span style="color: #008080; font-style: italic;">// GPL 2: http://opensource.org/licenses/gpl-2.0.php</span><br />
<br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Diagnostics</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Runtime.InteropServices</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">namespace</span> Huddled.<span style="color: #0000FF;">Interop</span><br />
<span style="color: #000000;">&#123;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// A wrapper around AllocConsole, with some nice eventing to handle</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> NativeConsole <span style="color: #008000;">:</span> IDisposable<br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// The API/Interop/PInvoke methods for the NativeConsole </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">internal</span> <span style="color: #FF0000;">class</span> NativeMethods<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080;">#region &nbsp;Fields (5)</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> UInt32 DUPLICATE_SAME_ACCESS <span style="color: #008000;">=</span> 0x00000002<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> LWA_ALPHA <span style="color: #008000;">=</span> 0x2<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> LWA_COLORKEY <span style="color: #008000;">=</span> 0x1<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> WS_EX_LAYERED <span style="color: #008000;">=</span> 0x80000<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> WS_EX_TRANSPARENT <span style="color: #008000;">=</span> 0x00000020<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080;">#region &nbsp;Enums (3)</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> ShowState <span style="color: #008000;">:</span> <span style="color: #FF0000;">int</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SW_HIDE <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// and lots of others</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> GwlIndex <span style="color: #008000;">:</span> <span style="color: #FF0000;">int</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Id <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">12</span><span style="color: #000000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Style <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">16</span><span style="color: #000000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ExStyle <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">-</span><span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> StdHandle <span style="color: #008000;">:</span> <span style="color: #FF0000;">int</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// The standard input device</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INPUT_HANDLE <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">10</span>, <span style="color: #008080; font-style: italic;">//(DWORD)-10 &nbsp; &nbsp;The standard input device.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// The standard output device.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OUTPUT_HANDLE <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">11</span>, <span style="color: #008080; font-style: italic;">//(DWORD)-11 &nbsp; The standard output device.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// The standard error device.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ERROR_HANDLE <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">12</span> <span style="color: #008080; font-style: italic;">//(DWORD)-12 &nbsp;The standard error device.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080;">#region &nbsp;Methods (13)</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// &nbsp;Public Methods (13)</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span><span style="color: #0600FF;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #FF0000;">Bool</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> AllocConsole<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span><span style="color: #0600FF;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #FF0000;">Bool</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> CloseHandle<span style="color: #000000;">&#40;</span>IntPtr hHandle<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span><span style="color: #0600FF;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #FF0000;">Bool</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> CreatePipe<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">out</span> IntPtr hReadPipe, <span style="color: #0600FF;">out</span> IntPtr hWritePipe, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">ref</span> SECURITY_ATTRIBUTES lpPipeAttributes, <span style="color: #FF0000;">uint</span> nSize<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> DuplicateHandle<span style="color: #000000;">&#40;</span>IntPtr hSourceProcessHandle,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IntPtr hSourceHandle, IntPtr hTargetProcessHandle, <span style="color: #0600FF;">out</span> IntPtr lpTargetHandle,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">uint</span> dwDesiredAccess, <span style="color: #FF0000;">bool</span> bInheritHandle, <span style="color: #FF0000;">uint</span> dwOptions<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>, ExactSpelling <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span><span style="color: #0600FF;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #FF0000;">Bool</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> FreeConsole<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> IntPtr GetConsoleWindow<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;user32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">int</span> GetWindowLong<span style="color: #000000;">&#40;</span>IntPtr hWnd, GwlIndex nIndex<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> ReadFile<span style="color: #000000;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IntPtr hFile, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// handle to file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> lpBuffer, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// data buffer</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">int</span> nNumberOfBytesToRead, &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// number of bytes to read</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">out</span> <span style="color: #FF0000;">int</span> lpNumberOfBytesRead, &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// number of bytes read</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IntPtr overlapped &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// overlapped buffer</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;user32.dll&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> SetLayeredWindowAttributes<span style="color: #000000;">&#40;</span>IntPtr hwnd, <span style="color: #FF0000;">int</span> crKey, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">byte</span> bAlpha, <span style="color: #FF0000;">int</span> dwFlags<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span><span style="color: #0600FF;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #FF0000;">Bool</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> SetStdHandle<span style="color: #000000;">&#40;</span>StdHandle nStdHandle, IntPtr hHandle<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;user32.dll&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">int</span> SetWindowLong<span style="color: #000000;">&#40;</span>IntPtr hWnd, GwlIndex nIndex, <span style="color: #FF0000;">int</span> dwNewLong<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;user32.dll&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span><span style="color: #0600FF;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #FF0000;">Bool</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">bool</span> ShowWindow<span style="color: #000000;">&#40;</span>IntPtr hWnd, ShowState nCmdShow<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>DllImport<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;kernel32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">extern</span> <span style="color: #FF0000;">int</span> WriteFile<span style="color: #000000;">&#40;</span>IntPtr hFile, <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buffer,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">int</span> numBytesToWrite, <span style="color: #0600FF;">out</span> <span style="color: #FF0000;">int</span> numBytesWritten, IntPtr lpOverlapped<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#91;</span>StructLayout<span style="color: #000000;">&#40;</span>LayoutKind.<span style="color: #0000FF;">Sequential</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">struct</span> SECURITY_ATTRIBUTES<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> nLength<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> IntPtr lpSecurityDescriptor<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#91;</span>MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #FF0000;">Bool</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">bool</span> bInheritHandle<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> OutputEventArgs<br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Text<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#region Delegate and Events (3)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Delegate, I changed this to be compatible with normal wpf/forms events</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">delegate</span> <span style="color: #0600FF;">void</span> OutputDelegate<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> source, OutputEventArgs args<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Events (2)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> OutputDelegate WriteErrorLine<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> OutputDelegate WriteOutputLine<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#region &nbsp;Private Fields (14)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Track whether Dispose has been called.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> disposed <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// A nice handle to our console window</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> IntPtr handle<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// And our process</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Process</span> process<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Our two threads</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> Thread outputThread, errorThread<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// and the original handles to the console</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> IntPtr stdOutRead, stdOutWrite, stdInRead, stdInWrite, stdErrRead, stdErrWrite<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// and the copy handles ...</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> IntPtr stdOutReadCopy, stdInWriteCopy, stdErrReadCopy<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#region &nbsp;Constructors and Destructors (2)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Initializes a new instance of the &lt;see cref=&quot;NativeConsole&quot;/&gt; class.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> NativeConsole<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Make ourselves a nice console</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">AllocConsole</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// hide the window ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;handle <span style="color: #008000;">=</span> NativeMethods.<span style="color: #0000FF;">GetConsoleWindow</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">ShowWindow</span><span style="color: #000000;">&#40;</span>handle, NativeMethods.<span style="color: #0000FF;">ShowState</span>.<span style="color: #0000FF;">SW_HIDE</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//NativeMethods.SetWindowLong(handle, NativeMethods.GwlIndex.ExStyle, </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// (NativeMethods.GetWindowLong(handle, NativeMethods.GwlIndex.ExStyle) |</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// NativeMethods.WS_EX_LAYERED | NativeMethods.WS_EX_TRANSPARENT));</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//NativeMethods.SetLayeredWindowAttributes(handle, 0, 0, NativeMethods.LWA_ALPHA);</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;process <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Process</span>.<span style="color: #0000FF;">GetCurrentProcess</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">SECURITY_ATTRIBUTES</span> saAttr<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Set the bInheritHandle flag so pipe handles are inherited.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;saAttr.<span style="color: #0000FF;">nLength</span> <span style="color: #008000;">=</span> Marshal.<a href="http://www.google.com/search?q=sizeof+msdn.microsoft.com"><span style="color: #008000;">SizeOf</span></a><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">SECURITY_ATTRIBUTES</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;saAttr.<span style="color: #0000FF;">bInheritHandle</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;saAttr.<span style="color: #0000FF;">lpSecurityDescriptor</span> <span style="color: #008000;">=</span> IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span><br />
<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// The steps for redirecting STDOUT:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Create anonymous pipe to be STDOUT for us.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Set STDOUT of our process to be WRITE handle to the pipe.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Create a (noninheritable) duplicate of the read handle, and...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Close the inheritable read handle.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">CreatePipe</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">out</span> stdOutRead, <span style="color: #0600FF;">out</span> stdOutWrite, <span style="color: #0600FF;">ref</span> saAttr, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't create the STDOUT pipe&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">SetStdHandle</span><span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">StdHandle</span>.<span style="color: #0000FF;">OUTPUT_HANDLE</span>, stdOutWrite<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't redirect STDOUT!&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Create noninheritable read handle and close the inheritable read handle.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">DuplicateHandle</span><span style="color: #000000;">&#40;</span>process.<span style="color: #0000FF;">Handle</span>, stdOutRead, process.<span style="color: #0000FF;">Handle</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">out</span> stdOutReadCopy, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF;">false</span>, NativeMethods.<span style="color: #0000FF;">DUPLICATE_SAME_ACCESS</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't Duplicate STDOUT Handle&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdOutRead<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// For the output handles we need a thread to read them</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;outputThread <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Thread<span style="color: #000000;">&#40;</span>OutputThread<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;outputThread.<span style="color: #0000FF;">SetApartmentState</span><span style="color: #000000;">&#40;</span>ApartmentState.<span style="color: #0000FF;">STA</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;outputThread.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// The steps for redirecting STDERR are the same:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Create anonymous pipe to be STDERR for us.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Set STDERR of our process to be WRITE handle to the pipe.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Create a (noninheritable) duplicate of the read handle and </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Close the inheritable read handle.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">CreatePipe</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">out</span> stdErrRead, <span style="color: #0600FF;">out</span> stdErrWrite, <span style="color: #0600FF;">ref</span> saAttr, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't create the STDERR pipe&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">SetStdHandle</span><span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">StdHandle</span>.<span style="color: #0000FF;">ERROR_HANDLE</span>, stdErrWrite<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't redirect STDERR!&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Create noninheritable read handle and close the inheritable read handle.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">DuplicateHandle</span><span style="color: #000000;">&#40;</span>process.<span style="color: #0000FF;">Handle</span>, stdErrRead, process.<span style="color: #0000FF;">Handle</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">out</span> stdErrReadCopy, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF;">false</span>, NativeMethods.<span style="color: #0000FF;">DUPLICATE_SAME_ACCESS</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't Duplicate STDERR Handle&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdErrRead<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// For the output handles we need a thread to read them</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;errorThread <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Thread<span style="color: #000000;">&#40;</span>ErrorThread<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;errorThread.<span style="color: #0000FF;">SetApartmentState</span><span style="color: #000000;">&#40;</span>ApartmentState.<span style="color: #0000FF;">STA</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;errorThread.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// The steps for redirecting STDIN:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Create anonymous pipe to be STDIN for us.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Set STDIN of our process to be READ handle to the pipe.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Create a (noninheritable) duplicate of the WRITE handle and </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// * Close the inheritable WRITE handle.</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">CreatePipe</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">out</span> stdInRead, <span style="color: #0600FF;">out</span> stdInWrite, <span style="color: #0600FF;">ref</span> saAttr, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't create the StdIn pipe&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">SetStdHandle</span><span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">StdHandle</span>.<span style="color: #0000FF;">INPUT_HANDLE</span>, stdInRead<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't redirect StdIn!&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Create noninheritable read handle and close the inheritable read handle.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>NativeMethods.<span style="color: #0000FF;">DuplicateHandle</span><span style="color: #000000;">&#40;</span>process.<span style="color: #0000FF;">Handle</span>, stdInWrite, process.<span style="color: #0000FF;">Handle</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">out</span> stdInWriteCopy, <span style="color: #FF0000;">0</span>, <span style="color: #0600FF;">false</span>, NativeMethods.<span style="color: #0000FF;">DUPLICATE_SAME_ACCESS</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">System.<span style="color: #0000FF;">Diagnostics</span></span>.<span style="color: #0000FF;">Trace</span>.<span style="color: #0000FF;">TraceError</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Couldn't Duplicate StdIn Handle&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdInWrite<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Releases unmanaged resources and performs other cleanup operations </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// before the &lt;see cref=&quot;Console&quot;/&gt; is reclaimed by garbage collection.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// Use C# destructor syntax for finalization code.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// This destructor will run only if the Dispose method does not get called.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;remarks&gt;NOTE: Do not provide destructors in types derived from this class.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/remarks&gt;</span><br />
&nbsp; &nbsp; &nbsp; ~NativeConsole<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Instead of cleaning up in BOTH Dispose() and here ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// We call Dispose(false) for the best readability and maintainability.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Dispose<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#endregion</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#region Methods (5)</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// &nbsp;Public Methods (2)</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// Implement IDisposable</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// Performs application-defined tasks associated with </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// freeing, releasing, or resetting unmanaged resources.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// This object will be cleaned up by the Dispose method.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Therefore, we call GC.SupressFinalize to tell the runtime </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// that we dont' need to be finalized (we would clean up twice)</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GC.<span style="color: #0000FF;">SuppressFinalize</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Dispose<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// Writes the input.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;input&quot;&gt;The input.&lt;/param&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> WriteInput<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> input<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bytes <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span>.<span style="color: #0000FF;">UTF8Encoding</span>.<span style="color: #0600FF;">Default</span>.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span>input<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">int</span> written<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">int</span> hresult <span style="color: #008000;">=</span> NativeMethods.<span style="color: #0000FF;">WriteFile</span><span style="color: #000000;">&#40;</span>stdInWriteCopy, bytes, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bytes.<span style="color: #0000FF;">Length</span>, <span style="color: #0600FF;">out</span> written, IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>hresult <span style="color: #008000;">!=</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Exception<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Error Writing to StdIn, HRESULT: &quot;</span> <span style="color: #008000;">+</span> hresult.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// &nbsp;Private Methods (3)</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// Handles actual cleanup actions, under two different scenarios</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;param name=&quot;disposing&quot;&gt;if set to &lt;c&gt;true&lt;/c&gt; we've been called directly or </span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// indirectly by user code and can clean up both managed and unmanaged resources.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// Otherwise it's been called from the destructor/finalizer and we can't</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// reference other managed objects (they might already be disposed).</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">///&lt;/param&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span> disposing<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Check to see if Dispose has already been called.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>disposed<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// // If disposing equals true, dispose all managed resources ALSO.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>disposing<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorThread.<span style="color: #0000FF;">Abort</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputThread.<span style="color: #0000FF;">Abort</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//WriteInput(&quot;\n&quot;);</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bytes <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Text</span></span>.<span style="color: #0000FF;">UTF8Encoding</span>.<span style="color: #0600FF;">Default</span>.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">char</span><span style="color: #000000;">&#41;</span><span style="color: #FF0000;">26</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">int</span> written<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">WriteFile</span><span style="color: #000000;">&#40;</span>stdErrWrite, bytes, bytes.<span style="color: #0000FF;">Length</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">out</span> written, IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">WriteFile</span><span style="color: #000000;">&#40;</span>stdOutWrite, bytes, bytes.<span style="color: #0000FF;">Length</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">out</span> written, IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//errorThread.Join();</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//outputThread.Join();</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Clean up UnManaged resources</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// If disposing is false, only the following code is executed.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">FreeConsole</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//NativeMethods.CloseHandle(stdOutWrite);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//NativeMethods.CloseHandle(stdOutReadCopy);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//NativeMethods.CloseHandle(stdErrWrite);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//NativeMethods.CloseHandle(stdErrReadCopy);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdInWriteCopy<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdInRead<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">Message</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Trace.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">StackTrace</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;disposed <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// The ErrorThread ThreadStart delegate</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> ErrorThread<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">int</span> BytesRead<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> BufBytes <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">4096</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// consider wrapping this in a System.IO.FileStream</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">ReadFile</span><span style="color: #000000;">&#40;</span>stdErrReadCopy, BufBytes, <span style="color: #FF0000;">4096</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">out</span> BytesRead, IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>WriteErrorLine <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WriteErrorLine<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> OutputEventArgs <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Text <span style="color: #008000;">=</span> UTF8Encoding.<span style="color: #0600FF;">Default</span>.<span style="color: #0000FF;">GetString</span><span style="color: #000000;">&#40;</span>BufBytes, <span style="color: #FF0000;">0</span>, BytesRead<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>ThreadAbortException<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">finally</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdErrWrite<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdErrReadCopy<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// The OutputThread ThreadStart delegate</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> OutputThread<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">int</span> BytesRead<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> BufBytes <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">4096</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// consider wrapping this in a System.IO.FileStream</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">ReadFile</span><span style="color: #000000;">&#40;</span>stdOutReadCopy, BufBytes, <span style="color: #FF0000;">4096</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">out</span> BytesRead, IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>WriteOutputLine <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WriteOutputLine<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> OutputEventArgs <span style="color: #000000;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Text <span style="color: #008000;">=</span> UTF8Encoding.<span style="color: #0600FF;">Default</span>.<span style="color: #0000FF;">GetString</span><span style="color: #000000;">&#40;</span>BufBytes, <span style="color: #FF0000;">0</span>, BytesRead<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>ThreadAbortException<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">finally</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdOutWrite<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">CloseHandle</span><span style="color: #000000;">&#40;</span>stdOutReadCopy<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080;">#endregion</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/nativeconsolecs-run-console-apps-and-get-their-output/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Visual Studio 2008</title>
		<link>http://huddledmasses.org/visual-studio-2008/</link>
		<comments>http://huddledmasses.org/visual-studio-2008/#comments</comments>
		<pubDate>Sat, 24 Nov 2007 02:51:50 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[VS2008]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/visual-studio-2008/</guid>
		<description><![CDATA[Well, Visual Studio 2008 launched while I was off in Houston, Texas &#8230; if you don&#8217;t have an MSDN subscription, you can check out Visual C# Express 2008 and the rest of the Visual 2008 Express editions, or the trial versions and of course, the new .Net Framework 3.5]]></description>
			<content:encoded><![CDATA[	<p>Well, <a href="http://msdn2.microsoft.com/en-us/vstudio/default.aspx">Visual Studio 2008</a> launched while I was off in Houston, Texas &#8230; if you don&#8217;t have an <a href="http://msdn2.microsoft.com/subscriptions/bb608344.aspx"><span class="caps">MSDN</span> subscription</a>, you can check out <a href="http://www.microsoft.com/express/vcsharp/">Visual C# Express 2008</a> and the rest of the Visual <a href="http://www.microsoft.com/express">2008 Express</a> editions, or the <a href="http://msdn2.microsoft.com/en-us/vstudio/products/aa700831.aspx">trial versions</a> and of course, the new <a href="http://go.microsoft.com/?linkid=7755937">.Net Framework 3.5</a></p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/visual-studio-2008/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Register DLL &#8211; Calling RegSvr32 without MessageBoxes in PowerShell</title>
		<link>http://huddledmasses.org/register-dll-calling-regsvr32-without-messageboxes-in-powershell/</link>
		<comments>http://huddledmasses.org/register-dll-calling-regsvr32-without-messageboxes-in-powershell/#comments</comments>
		<pubDate>Mon, 16 Jul 2007 05:00:17 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/jaykul/register-dll-calling-regsvr32-without-messageboxes-in-powershell/</guid>
		<description><![CDATA[Someone asked in the #PowerShell channel on FreeNode for a way to register dll&#8217;s (you know, old COM libraries for use in scripts or apps) ... specifically, they wanted to create a script that could register a bunch of Dll files, and know which ones passed or failed. It turned out to be an interesting [...]]]></description>
			<content:encoded><![CDATA[	<p>Someone asked in the #PowerShell channel on FreeNode for a way to register dll&#8217;s (you know, old <span class="caps">COM</span> libraries for use in scripts or apps) ... specifically, they wanted to create a script that could register a bunch of Dll files, and know which ones passed or failed.</p>

	<p>It turned out to be an interesting problem, because even though <a href="http://msdn2.microsoft.com/en-us/library/ms177531.aspx">the source code</a> on the <span class="caps">MSDN</span> Library has a /C parameter to cause the application to write to stdout, the one which comes with windows doesn&#8217;t, so the only obvious way to get output is in a MessageBox.  However, there is also an exit code, but the exit code doesn&#8217;t seem to end up in <code>$LASTEXITCODE</code> or <code>$?</code> at all (maybe because the app is actually not a console app?  I don&#8217;t get it, really). So the real problem became: how do I capture the exit code of an application in PowerShell?</p>

	<blockquote>
		<p> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  Someone sent me an email today (Jan 7 2010) mentioning that if you nest your call to regsvr32 inside a call to cmd.exe &#8230; it <strong>does</strong> set the $LastExitCode:<div class="posh code posh" style="font-family:monospace;">cmd <span style="color: #66cc66;">/</span>c <span style="color: #009900;">&quot;regsvr32 /s 'C:\Path\To\Some.dll'&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$LastExitCode</span> <span style="color: #666666; font-style: italic;">## Will be set correctly.</span></div>
	</blockquote>

	<p>Well, the simplest thing would be to just go around that &#8212; to compile and distribute the <span class="caps">MSDN</span> sample with the <code>/c</code> console output option so I don&#8217;t need the exit code.  Of course, they are very clear that we shouldn&#8217;t do that because the sample isn&#8217;t &#8220;safe,&#8221; and distributing it afterward would probably be a problem with licensing.  Well, I don&#8217;t know about that, but I figured the best thing would be to just use the exit code from the existing RegSvr32.exe rather than asking people to use what they would perceive as a 3rd party RegSvr32.</p>

	<p>And if we play around a bit with the .Net <a href="http://msdn2.microsoft.com/en-us/library/System.Diagnostics.Process.aspx">Process</a> and <a href="http://msdn2.microsoft.com/en-us/library/System.Diagnostics.ProcessStartInfo.aspx">ProcessStartInfo</a> classes, it&#8217;s not to hard to get the exit code &#8230;</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">#### Get-ExitCode.ps1</span><br />
<span style="color: #666666; font-style: italic;">##########################################################</span><br />
<span style="color: #666666; font-style: italic;"># initialize things</span><br />
<span style="color: #660033; font-weight: bold;">$p</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Diagnostics</span>.<span style="color: #666699; font-weight: bold;">Process</span><br />
<span style="color: #660033; font-weight: bold;">$si</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Diagnostics</span>.<span style="color: #003366;">ProcessStartInfo</span><br />
<span style="color: #660033; font-weight: bold;">$exit</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$false</span><br />
<br />
<span style="color: #666666; font-style: italic;"># split off the first argument (the command) from any others</span><br />
<span style="color: #660033; font-weight: bold;">$si</span>.<span style="color: #003366;">FileName</span>, <span style="color: #660033; font-weight: bold;">$si</span>.<span style="color: #003366;">Arguments</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$args</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$p</span>.<span style="color: #003366;">StartInfo</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$si</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$p</span>.<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; <span style="color: #660033; font-weight: bold;">$p</span>.<span style="color: #003366;">WaitForExit</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; <span style="color: #660033; font-weight: bold;">$exit</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$p</span>.<span style="color: #003366;">ExitCode</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #660033; font-weight: bold;">$p</span>.<span style="color: #003366;">Dispose</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$exit</span><br />
&nbsp;</div>

	<p>After a bit more experimentation with the return values, I came up with a function called RegisterDllServer which appears below.  The function has a <code>-Verbose</code> switch and a <code>-Debug</code> switch which turn on some helpful messages (the Debug switch actually allows the RegSvr32 message boxes to show up, so you shouldn&#8217;t use that if you&#8217;re calling it from within a script).<span id="more-423"></span></p>

	<p>So, to answer the original question, to use this to register all the dll&#8217;s in a folder you could just call it like <code>ls *.dll | RegSvr32</code> using the alias that the script sets up at the end, or to be more complete, call it like: <code>ls *.dll | % {$r=$(RegSvr $_); if($r -gt 0){ &#34;FAILED: $_&#34; } else { &#34;SUCCEEDED: $_&#34; }}</code> &#8230; nice, eh?</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">#### RegisterDllServer.ps1</span><br />
<span style="color: #666666; font-style: italic;">################################################################################</span><br />
<span style="color: #666666; font-style: italic;">#### If you'd rather use it as a script:</span><br />
<span style="color: #666666; font-style: italic;">#### * Comment out the first two and last two lines of script</span><br />
<span style="color: #666666; font-style: italic;">#### * Uncomment the next line instead!</span><br />
<span style="color: #666666; font-style: italic;"># param([string]$DllPath, [switch]$Uninstall = $false, [switch]$Verbose = $false, [switch]$Debug = $false)</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> RegisterDllServer<span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$DllPath</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Uninstall</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$false</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Verbose</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$false</span>, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$Debug</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$false</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#123;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">begin</span><br />
&nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Write</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&amp;</span><span style="color: #333;">&#123;</span><span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Verbose</span> <span style="color: #000066;">-or</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$VerbosePreference</span> <span style="color: #000066;">-ne</span> <span style="color: #009900;">&quot;SilentlyContinue&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;Write-Output&quot;</span> <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span> <span style="color: #009900;">&quot;Write-Verbose&quot;</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><br />
&nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; <span style="color: #666699; font-weight: bold;">process</span><br />
&nbsp; <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$DllPath</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Path</span></span> <span style="color: #660033; font-weight: bold;">$DllPath</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$p</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Diagnostics</span>.<span style="color: #666699; font-weight: bold;">Process</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$si</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Diagnostics</span>.<span style="color: #003366;">ProcessStartInfo</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$si</span>.<span style="color: #003366;">FileName</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;regsvr32.exe&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$si</span>.<span style="color: #003366;">Arguments</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`&quot;</span>$DllPath<span style="color: #000099; font-weight: bold;">`&quot;</span>&quot;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Debug</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$si</span>.<span style="color: #003366;">Arguments</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;/s &quot;</span> <span style="color: #66cc66;">+</span> <span style="color: #660033; font-weight: bold;">$si</span>.<span style="color: #003366;">Arguments</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Uninstall</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$si</span>.<span style="color: #003366;">Arguments</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;/u &quot;</span> <span style="color: #66cc66;">+</span> <span style="color: #660033; font-weight: bold;">$si</span>.<span style="color: #003366;">Arguments</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$p</span>.<span style="color: #003366;">StartInfo</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$si</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$result</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$p</span>.<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$p</span>.<span style="color: #003366;">WaitForExit</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">int</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$exit</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$p</span>.<span style="color: #003366;">ExitCode</span>; <br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$p</span>.<span style="color: #003366;">Dispose</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">switch</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$exit</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">0</span> <span style="color: #333;">&#123;</span> <span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$Write</span> <span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`&quot;</span>$DllPath<span style="color: #000099; font-weight: bold;">`&quot;</span> Registered Successfully&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">1</span> <span style="color: #333;">&#123;</span> <span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$Write</span> <span style="color: #009900;">&quot;Bad arguments to RegSvr32&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">2</span> <span style="color: #333;">&#123;</span> <span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$Write</span> <span style="color: #009900;">&quot;OLE initilization failed for <span style="color: #000099; font-weight: bold;">`&quot;</span>$DllPath<span style="color: #000099; font-weight: bold;">`&quot;</span>&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">3</span> <span style="color: #333;">&#123;</span> <span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$Write</span> <span style="color: #009900;">&quot;Failed to load the module <span style="color: #000099; font-weight: bold;">`&quot;</span>$DllPath<span style="color: #000099; font-weight: bold;">`&quot;</span>, you may need to check for problems with dependencies.&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">4</span> <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Uninstall</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$Write</span> <span style="color: #009900;">&quot;Can't find DllUnregisterServer entry point in the file <span style="color: #000099; font-weight: bold;">`&quot;</span>$DllPath<span style="color: #000099; font-weight: bold;">`&quot;</span>, maybe it's not a .DLL or .OCX?&quot;</span> <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span> <span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$Write</span> <span style="color: #009900;">&quot;Can't find DllRegisterServer entry point in the file <span style="color: #000099; font-weight: bold;">`&quot;</span>$DllPath<span style="color: #000099; font-weight: bold;">`&quot;</span>, maybe it's not a .DLL or .OCX?&quot;</span> <span style="color: #333;">&#125;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #cc66cc;">5</span> <span style="color: #333;">&#123;</span> &nbsp;<span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$Write</span> <span style="color: #009900;">&quot;The assembly <span style="color: #000099; font-weight: bold;">`&quot;</span>$DllPath<span style="color: #000099; font-weight: bold;">`&quot;</span> was loaded, but the call to DllRegisterServer failed.&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Debug</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$Write</span> <span style="color: #009900;">&quot;Call RegisterDllServer again with the -Debug switch to see more information in a MessageBox.&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; default <span style="color: #333;">&#123;</span> <span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$Write</span> <span style="color: #009900;">&quot;Something went wrong, with Exit Code $exit!&quot;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">return</span> <span style="color: #660033; font-weight: bold;">$exit</span>; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&amp;</span><span style="color: #660033; font-weight: bold;">$Write</span> <span style="color: #009900;">&quot;Failed to find the file <span style="color: #000099; font-weight: bold;">`&quot;</span>$DllPath<span style="color: #000099; font-weight: bold;">`&quot;</span> please check the path.&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">return</span> <span style="color: #cc66cc;">3</span>;<br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Alias</span></span> RegSvr32 RegisterDllServer<br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/register-dll-calling-regsvr32-without-messageboxes-in-powershell/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Microsoft Command Line Standard</title>
		<link>http://huddledmasses.org/microsoft-command-line-standard/</link>
		<comments>http://huddledmasses.org/microsoft-command-line-standard/#comments</comments>
		<pubDate>Fri, 18 May 2007 00:02:25 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/jaykul/microsoft-command-line-standard-2/</guid>
		<description><![CDATA[Microsoft has published a new Command Line Standard which sets out the standard for writing command line applications for Microsoft platforms &#8230; While it&#8217;s clearly based on the way they&#8217;ve written PowerShell, it is a spec that is &#8220;independent of any specific implementation of a shell, set of utilities or command creation technologies&#8221; and should [...]]]></description>
			<content:encoded><![CDATA[	<p>Microsoft has published a new <a href="http://www.microsoft.com/technet/scriptcenter/topics/winpsh/cmdline_std.mspx">Command Line Standard</a> which sets out the standard for writing command line applications for Microsoft platforms &#8230; While it&#8217;s clearly based on the way they&#8217;ve written PowerShell, it is a spec that is &#8220;independent of any specific implementation of a shell, set of utilities or command creation technologies&#8221; and should be used for any command line interface apps for <span class="caps">DOS</span>, too (especially since doing so would enable your app to function in a PowerShell pipeline as well).  </p>

	<p>Anyway, it looks really interesting, and I can&#8217;t help but think that perhaps the Linux command-line could benefit from apps implementing this standard too  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> . In particular, I like the <span class="caps">TCSV</span> format option as a way of enhancing plain old <span class="caps">CSV</span> data with type and structure information &#8230; and the standardized naming convention.  Ultimately, these five points (from the spec) should be the standard for any command-line interface (although I know many linux die-hards will note they&#8217;ve been living without number five for many years):</p>

	<ol>
		<li>Consistent verb and parameter naming conventions.</li>
		<li>Consistent command syntax</li>
		<li>Common behaviors.</li>
		<li>Consistent error messages and exit codes.</li>
	</ol>
	<ol>
		<li>Schema-enhanced data streams and formatting.</li>
	</ol>

	<p>However, <a href="http://www.microsoft.com/technet/scriptcenter/topics/winpsh/cmdline_std.mspx#EWHAC">Appendix A – Standardized Verb Sets and Verb Names</a> got my attention in particular, because it actually obsoletes a few of the Verbs that were in <a href="http://blogs.msdn.com/powershell/archive/2006/04/25/583257.aspx">this list on the PowerShell blog</a> and which they&#8217;ve actually been using in the <a href="http://www.codeplex.com/PowerShellCX">Community Extensions</a> &#8230; Specifically, <strong>Write</strong> is marked as obsolete in favor of <strong>Set</strong> &#8212; and it&#8217;s pair <strong>Get</strong>, is to be used instead of the <em>obsolete</em> <strong>Read</strong>.  In addition, there&#8217;s no mention of <strong>Out</strong> and <em>*Where*</em> is only mentioned as an obsolete version of <strong>Resolve</strong> (which really made me wonder about &#8220;Where-Object&#8221; but I guess that can be the exception that proves the rule?).  The spec actually says that it is <strong>required</strong> to use verbs from the list of standard verbs in <a href="http://www.microsoft.com/technet/scriptcenter/topics/winpsh/cmdline_std.mspx#EWHAC">Appendix A.</a>, so there doesn&#8217;t seem to be an option to invent your own verbs.</p>

	<p>If you have any comments about the spec, I guess you should put them <a href="http://blogs.msdn.com/powershell/archive/2007/05/17/microsoft-command-line-standard.aspx">on this post on their blog</a>, rather than on <a href="http://www.codeplex.com/PowerShellCX/Thread/View.aspx?ThreadId=10423">this thread in the <span class="caps">PSCX</span> forum</a>.</p>

	<p>I&#8217;ll have to write another post on this later, after I&#8217;ve had a chance to re-read the spec and digest it a bit &#8230; so far it looks really good, but it sure would throw the monkey-wrench in my old console &#8220;Hello World&#8221; apps in C++ and Java &#8230; I wonder if someone will create a wrapper to make writing compliant command line apps easier (without resorting to writing PowerShell commandlets which are practically compliant automatically).  I also wonder how long it will be until a new PowerShell release comes out that actually understands <span class="caps">TCSV</span>...</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/microsoft-command-line-standard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Control Windows from PowerShell</title>
		<link>http://huddledmasses.org/control-windows-from-powershell/</link>
		<comments>http://huddledmasses.org/control-windows-from-powershell/#comments</comments>
		<pubDate>Thu, 01 Mar 2007 04:54:53 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/jaykul/control-windows-from-powershell/</guid>
		<description><![CDATA[[New] CodePlex Project As I announced recently I have created a Windows Automation PowerShell Snapin project to house this snapin on CodePlex, and added a bunch of features. You should head over there for the download. Original Post I&#8217;ve been crazy busy the last few weeks, but I&#8217;ve been really rather distracted from my main [...]]]></description>
			<content:encoded><![CDATA[	<h3>[New] CodePlex Project</h3>

	<p>As I <a href="/window-gui-automation-from-powershell/">announced recently</a> I have created a <a href="http://www.codeplex.com/WASP">Windows Automation PowerShell Snapin project</a> to house this snapin on CodePlex, and added a bunch of features. You should head over there for the download.</p>

	<h3>Original Post</h3>

	<p>I&#8217;ve been crazy busy the last few weeks, but I&#8217;ve been really rather distracted from <a href="/category/development/recommender/">my main project</a> and it&#8217;s past time I got back to it.  To really feel like I&#8217;ve moved on, I need to write a bunch of articles here and publish a couple of cool apps and some source code so that I can feel like I&#8217;ve reached a <em>release</em> point.  What I&#8217;ve been playing with in all this coding is the new technology that&#8217;s been coming out of Microsoft in the last couple months: <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx">PowerShell</a>, .<span class="caps">NET</span> 3 and <a href="http://wpf.netfx3.com/">WPF</a>, and <a href="http://WindowsVista.com">Windows Vista</a> so you may or may not actually be able to use these apps &#8230;</p>

	<p>So yeah, anyway &#8230; I&#8217;ve been playing around with PowerShell cmdlets, and as a shell geek, I couldn&#8217;t help wondering why there wasn&#8217;t a way to enumerate windows and move them around &#8230; so I wrote one.  Eventually this will <a href="http://www.codeplex.com/PowerShellCX/Thread/View.aspx?ThreadId=7291">show up</a> in the <a href="http://www.codeplex.com/PowerShellCX">PowerShell Community Extensions</a> &#8212; but I&#8217;m posting it here for now because the powershell cmdlet process is pretty interesting.</p>

	<p>I started off by writing a wrapper for some of the Win32 APIs, and then a PSSnapIn which provides Get-Window and Remove-Window.  When you execute Get-Window, the resulting object is a Win32.Window which has methods for closing, moving, resizing, maximizing and restoring, etc.  The Remove-Window cmdlet is just a wrapper to call the Close() method, but it seems to make sense in the PowerShell pipe, since you can pipe the window objects through filters etc.  </p>

	<p>I still need to finish up the Move-Window cmdlet &#8212; it&#8217;s an advanced window-organizing feature.  Right now it&#8217;s only feature is that it can tile windows vertically or horizontally with percentages specified by you.  The features there are a little weak, but it&#8217;s past time I show you what I mean.  </p>

	<p>What you would want to do is install the snapin and then try typing these commands one at a time (so you can see the results).</p>

	<div class="psh code psh" style="font-family:monospace;"><br />
for( $i = 0; $i -lt 3; $i++){notepad}<br />
Get-Window *notepad* &gt; Notepad.txt<br />
notepad Notepad.txt<br />
Get-Window &quot;Untitled - N*&quot; | Move-Window &quot;20%,30%,50%&quot;<br />
ps notepad | Move-Window &quot;20%,30%,20%,30%&quot; 0<br />
(Get-Window *Notepad.txt*).Minimize()<br />
$notes = Get-Window *notepad*<br />
$notes[0].Minimize()<br />
Get-Window | where {$_.ProcessName -eq &quot;notepad&quot; -and -not $_.IsMinimized} | Remove-Window<br />
ps notepad | Remove-Window<br />
&nbsp;</div>

	<p>A little explanation is in order:
	<ul>
		<li>Obviously the first three lines basically start up a few copies of notepad &#8212; three of them empty, one with a text file.  </li>
		<li>The next line shows how Get-Window works with wildcard matching on the window titles &#8230; and how Move-Window uses percentage values to organize windows. </li>
		<li>Then you see that you can pipe the output of Get-Process (aliased as <em>ps</em>) through Move-Window.  You can pipe it through Get-Window too, but the important take away here is that Remove-Window and Move-Window can both take processes as input &#8212; as long as those processes have actual windows.  You&#8217;ll also notice that final parameter &#8220;0&#8221; to cause Move-Window to tile horizontally instead of vertically &#8212; &#8220;1&#8221; is the default.</li>
		<li>The next three lines are examples of how you can call methods on individual Window objects.</li>
	</ul>
	<ul>
		<li>And finally, the next-to-last line is just an example of how you can use Where-Object to filter windows, and the last line is just to get rid of the remaining instances of notepad.</li>
	</ul></p>

	<p>You can download <a href="/downloads/Win32.Window%20for%20PowerShell.7z">Win32.Window for PowerShell</a> (it&#8217;s a 7-zip file, you&#8217;ll need <a href="http://tugzip.com">TugZip</a> or <a href="http://7-Zip.org">7-Zip</a>) ... it includes the source (which you can use under any of several open source licenses including Ms-PL, <span class="caps">BSD</span> and <span class="caps">GPL</span>) and the actual <span class="caps">DLL</span> in the bin/Release directory, along with the scripts Install.ps1 and Uninstall.ps1  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<p>Feature requests and code contributions are both welcome!</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/control-windows-from-powershell/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Windows RSS Platform &#8211; Part the first.</title>
		<link>http://huddledmasses.org/windows-rss-platform-part-the-first/</link>
		<comments>http://huddledmasses.org/windows-rss-platform-part-the-first/#comments</comments>
		<pubDate>Sun, 07 Jan 2007 04:45:17 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Recommender]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/jaykul/windows-rss-platform-part-the-first/</guid>
		<description><![CDATA[The inaptly named Windows RSS Platform is actually part of IE7, not part of Windows, and therefore is available on Windows XP if IE 7 has been installed, as well as on Windows Vista (where IE 7 is included originally). However, having said that, it isn&#8217;t just for IE: it includes a complete COM API [...]]]></description>
			<content:encoded><![CDATA[	<p>The inaptly named Windows <span class="caps">RSS</span> Platform is actually part of IE7, not part of Windows, and therefore is available on Windows XP if IE 7 has been installed, as well as on Windows Vista (where IE 7 is included originally).  However, having said that, it isn&#8217;t just for IE: it includes a <a href="http://msdn2.microsoft.com/en-us/library/ms684701.aspx">complete <span class="caps">COM</span> API</a> which is usable from script or the .Net Framework, and the header files are part of the <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=C2B1E300-F358-4523-B479-F53D234CDCCF&#38;displaylang=en">Windows Platform SDK</a> and usable from C/C++.</p>

	<p>The <span class="caps">RSS</span> Platform is intended to introduce a unified approach to <span class="caps">RSS</span> for Windows applications, where all applications use the same <span class="caps">RSS</span> feed store, and a service handles downloading the <span class="caps">RSS</span> feeds &#8212; including enclosures if requested &#8212; and normalizes them so applications need not handle parsing all the different feed formats (that is, you only need to parse the Microsoft-normalized <span class="caps">RSS</span> 2.0 with extensions).  </p>

	<p>As a platform for building RSS-based applications, it&#8217;s very well done, and well thought out. It&#8217;s now ridiculously easy to create an <span class="caps">RSS</span> reader, since the platform removes all need to parse <span class="caps">XML</span> except in the weirdest situations, and allows all applications to be instantly integrated on the same list of <span class="caps">RSS</span> feeds &#8230; let me show you &#8230;.<span id="more-364"></span></p>

	<p>I&#8217;ll be using the <span class="caps">RSS</span> Platform in <a href="/recommender/">my <span class="caps">SOM</span> Recommender</a> project, so I&#8217;ll have some more sample code I can share later on, including use of some of the events (there are events for all the things that could happen to feed folders or feeds themselves, including creations, renaming, deletion, etc). At the very least, you&#8217;d want to check out <a href="http://msdn2.microsoft.com/en-us/library/ms686317.aspx">GetWatcher</a> method (see the <a href="http://msdn2.microsoft.com/en-us/library/ms686416.aspx">examples</a>) and the <a href="http://msdn2.microsoft.com/en-us/library/ms685489.aspx">FeedItemCountChanged</a> and <a href="http://msdn2.microsoft.com/en-us/library/ms685487.aspx">FeedDownloadCompleted</a> events.  </p>

	<p>For now, I have a very simple example: a single function which exercises some of the most interesting methods in the <span class="caps">API</span>, but doesn&#8217;t use any of the events for detecting the arrival of new feed items nor the asynchronous download feature.  This simple example is a console application which will fetch a feed.  As you can see, it parses the command line for the <code>url</code> and <code>name</code> parameters.  </p>

	<p>Once you&#8217;ve specified a feed by name and <span class="caps">URL</span>, it will be stored in the &#8220;RssMe Feeds&#8221; folder so you can just refer to it by name in the future.  However, <strong>there&#8217;s one major problem</strong> with that: the FeedStore doesn&#8217;t have a concept of aliases or shortcuts, and the feed URLs have to be unique, so if you&#8217;ve already bookmarked a feed, RssMe would be able to show you the items from it, but not pull it up by name, unless you specify the full path to it. Honestly, this is the most frustrating thing to me (as a developer, <strong>and</strong> as a user) about the feed store:  what I&#8217;d like to be able to do is add a feed in multiple folders and have them automatically share the &#8220;read&#8221; state of the items.</p>

	<p>That would allow applications to create a subfolder (as RssMe does) and rely on it as a way of organizing the feeds they use. As it is, if third-party applications want to use only <em>some</em> of the feeds in the feed store, they will have to keep a list of URLs separately since they may not be able to add the feed to <em>their own</em> folder if it&#8217;s already in the store elsewhere.  Once they&#8217;ve done that, they can just retrieve them by <span class="caps">URL</span> from the store.</p>

	<p>Although the source code is inline below, you can download the <a href="/downloads/samples/RssMe%20Source.zip">Visual Studio 2005 project</a> which can be compiled by just running msbuild (that&#8217;s in your Microsoft.Net\Framework folder in C:\Windows &#8230; for some reason it&#8217;s not on the path, so you&#8217;ll probably need to type the whole path.  If you&#8217;re not familiar with MSBuild, try the /help argument   <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' />  but you can specify that you want to build the &#8220;Release&#8221; build by just running: <code>MSBuild /p:Configuration=Release</code> assuming you&#8217;ve fixed the path problem).  Or you can download the <a href="/downloads/samples/RssMe.zip">compiled example</a></p>

	<p>The command-line for this sample app is basically:
	<ul>
		<li><code>RssMe /url:http://huddledmasses.org/feed/atom/ /name:HM.O</code></li>
		<li><code>RssMe /url:http://del.icio.us/rss/ /name:&#34;Delicious Recent&#34;</code></li>
	</ul>
	<ul>
		<li><code>RssMe /name:HM.O</code></li>
	</ul></p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.Feeds.Interop</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Text.RegularExpressions</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">// A simple test of the IE7 &quot;Windows RSS Platform&quot;</span><br />
<span style="color: #0600FF;">namespace</span> RssFeedDemo<br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #FF0000;">class</span> RssMe<br />
<span style="color: #000000;">&#123;</span><br />
<span style="color: #0600FF;">static</span> <span style="color: #FF0000;">int</span> Main<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; <span style="color: #FF0000;">string</span> feedName <span style="color: #008000;">=</span> <span style="color: #0600FF;">null</span>, feedUrl <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #FF0000;">string</span> fldrName <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;RssMe Feeds&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #FF0000;">int</span> result <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; <span style="color: #008080; font-style: italic;">// Everything comes from the FeedsManager</span><br />
&nbsp; FeedsManager mgr <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> FeedsManagerClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #008080; font-style: italic;">// And everything is inside the RootFolder</span><br />
&nbsp; IFeedFolder root <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>IFeedFolder<span style="color: #000000;">&#41;</span>mgr.<span style="color: #0000FF;">RootFolder</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #008080; font-style: italic;">// Specifically, all the feeds are ...</span><br />
&nbsp; IFeed feed <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span><br />
<br />
&nbsp; <span style="color: #008080;">#region process the command-line arguments</span><br />
&nbsp; Regex re <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Regex<span style="color: #000000;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #666666;">@&quot;(?:(?:/url:\s*&quot;</span><span style="color: #666666;">&quot;?(?:&lt;url&gt;https?://<span style="color: #008080; font-weight: bold;">\S</span>+)&quot;</span><span style="color: #666666;">&quot;?<span style="color: #008080; font-weight: bold;">\s</span>*)|&quot;</span> <span style="color: #008000;">+</span><br />
&nbsp; &nbsp; <span style="color: #666666;">@&quot;(?:/name:\s*&quot;</span><span style="color: #666666;">&quot;?(?:&lt;name&gt;<span style="color: #008080; font-weight: bold;">\S</span>+)&quot;</span><span style="color: #666666;">&quot;?<span style="color: #008080; font-weight: bold;">\s</span>*))+&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; Match mx <span style="color: #008000;">=</span> re.<span style="color: #0000FF;">Match</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Join</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot; &quot;</span>, args<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>mx.<span style="color: #0000FF;">Success</span><span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>mx.<span style="color: #0000FF;">Groups</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;url&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Success</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; feedUrl <span style="color: #008000;">=</span> mx.<span style="color: #0000FF;">Groups</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;url&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Access feeds by URL ...</span><br />
&nbsp; &nbsp; &nbsp; feed <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>IFeed<span style="color: #000000;">&#41;</span>mgr.<span style="color: #0000FF;">GetFeedByUrl</span><span style="color: #000000;">&#40;</span>feedUrl<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>mx.<span style="color: #0000FF;">Groups</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;name&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Success</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; feedName <span style="color: #008000;">=</span> mx.<span style="color: #0000FF;">Groups</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;name&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Value</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// if they specified only the name</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>mx.<span style="color: #0000FF;">Groups</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;url&quot;</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Success</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span> <span style="color: #008080; font-style: italic;">// we assume they already stored it previously</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>mgr.<span style="color: #0000FF;">ExistsFeed</span><span style="color: #000000;">&#40;</span>fldrName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\</span><span style="color: #008080; font-weight: bold;">\</span>&quot;</span> <span style="color: #008000;">+</span> feedName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; feed <span style="color: #008000;">=</span><span style="color: #000000;">&#40;</span>IFeed<span style="color: #000000;">&#41;</span>mgr.<span style="color: #0000FF;">GetFeed</span><span style="color: #000000;">&#40;</span>fldrName<span style="color: #008000;">+</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\</span><span style="color: #008080; font-weight: bold;">\</span>&quot;</span><span style="color: #008000;">+</span>feedName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>mgr.<span style="color: #0000FF;">ExistsFeed</span><span style="color: #000000;">&#40;</span>feedName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; feed <span style="color: #008000;">=</span><span style="color: #000000;">&#40;</span>IFeed<span style="color: #000000;">&#41;</span>mgr.<span style="color: #0000FF;">GetFeed</span><span style="color: #000000;">&#40;</span>feedName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; <span style="color: #0600FF;">else</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span><br />
<span style="color: #666666;">@&quot;You should specify the url and/or name of the feed:<br />
&nbsp; &nbsp; rssme /name:delicious /url:http://del.icio.us/rss/<br />
Once you've defined a name and url, you can just use:<br />
&nbsp; &nbsp; rssme /name:delicious<br />
<br />
Please specify a name for this feed:<br />
&gt; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; feedName <span style="color: #008000;">=</span> Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>feedName.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">return</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Try getting the feed by it's expected path...</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>mgr.<span style="color: #0000FF;">ExistsFeed</span><span style="color: #000000;">&#40;</span>fldrName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\</span><span style="color: #008080; font-weight: bold;">\</span>&quot;</span> <span style="color: #008000;">+</span> feedName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; feed <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>IFeed<span style="color: #000000;">&#41;</span>mgr.<span style="color: #0000FF;">GetFeed</span><span style="color: #000000;">&#40;</span>fldrName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\</span><span style="color: #008080; font-weight: bold;">\</span>&quot;</span> <span style="color: #008000;">+</span> feedName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">else</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span><br />
<span style="color: #666666;">@&quot;Please specify an http Url for this feed:<br />
&gt; &quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; feedUrl <span style="color: #008000;">=</span> Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; <span style="color: #008080;">#endregion process the command-line arguments</span><br />
<br />
&nbsp; <span style="color: #008080; font-style: italic;">// if the feed doesn't already exist ...</span><br />
&nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span> <span style="color: #008000;">==</span> feed<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Test for the existence of sub folders </span><br />
&nbsp; &nbsp; IFeedFolder somrFeeds <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>root.<span style="color: #0000FF;">ExistsSubfolder</span><span style="color: #000000;">&#40;</span>fldrName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; somrFeeds <span style="color: #008000;">=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#40;</span>IFeedFolder<span style="color: #000000;">&#41;</span>root.<span style="color: #0000FF;">GetSubfolder</span><span style="color: #000000;">&#40;</span>fldrName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">else</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// and you can create folders very easily too</span><br />
&nbsp; &nbsp; &nbsp; somrFeeds <span style="color: #008000;">=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#40;</span>IFeedFolder<span style="color: #000000;">&#41;</span>root.<span style="color: #0000FF;">CreateSubfolder</span><span style="color: #000000;">&#40;</span>fldrName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Here's the catch:</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Each URL can only be in the storage once!</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// So, try getting the feed by it's URL</span><br />
&nbsp; &nbsp; feed <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>IFeed<span style="color: #000000;">&#41;</span>mgr.<span style="color: #0000FF;">GetFeedByUrl</span><span style="color: #000000;">&#40;</span>feedUrl<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// if we found the feed by URL, but not by name</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// we won't be able to (re)create it in here ...</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span> <span style="color: #008000;">!=</span> feed<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><br />
<span style="color: #666666;">@&quot;Couldn't save this feed with that name because it &quot;</span><br />
<span style="color: #008000;">+</span> <span style="color: #666666;">@&quot;already exists in another location.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; result <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">else</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Create a new feed specifiying a name and url</span><br />
&nbsp; &nbsp; &nbsp; feed <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>IFeed<span style="color: #000000;">&#41;</span>somrFeeds.<span style="color: #0000FF;">CreateFeed</span><span style="color: #000000;">&#40;</span>feedName, feedUrl<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; feed.<span style="color: #0000FF;">MaxItemCount</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// store all the items</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// If you wanted to download enclosures...</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// feed.DownloadEnclosuresAutomatically = true;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Override the default synchronization schedule</span><br />
&nbsp; &nbsp; &nbsp; feed.<span style="color: #0000FF;">Interval</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">60</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// specify interval in minutes</span><br />
&nbsp; &nbsp; &nbsp; feed.<span style="color: #0000FF;">SyncSetting</span> <span style="color: #008000;">=</span> FEEDS_SYNC_SETTING.<span style="color: #0000FF;">FSS_INTERVAL</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Manually download it for starters</span><br />
&nbsp; &nbsp; &nbsp; feed.<span style="color: #0000FF;">Download</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; <span style="color: #008080; font-style: italic;">// You can check how many items or unread items</span><br />
&nbsp; <span style="color: #FF0000;">int</span> itemCount <span style="color: #008000;">=</span> feed.<span style="color: #0000FF;">ItemCount</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #FF0000;">int</span> readCount <span style="color: #008000;">=</span> itemCount <span style="color: #008000;">-</span> feed.<span style="color: #0000FF;">UnreadItemCount</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; <span style="color: #008080; font-style: italic;">// You can use .Items in a foreach loop</span><br />
&nbsp; IFeedsEnum items <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>IFeedsEnum<span style="color: #000000;">&#41;</span>feed.<span style="color: #0000FF;">Items</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #008080; font-style: italic;">// But it would force starting from the oldest</span><br />
&nbsp; <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> index <span style="color: #008000;">=</span> readCount<span style="color: #008000;">;</span> index <span style="color: #008000;">&lt;</span> itemCount<span style="color: #008000;">;</span> <span style="color: #008000;">++</span>index<span style="color: #000000;">&#41;</span><br />
&nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; IFeedItem item <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>IFeedItem<span style="color: #000000;">&#41;</span>items.<span style="color: #0000FF;">Item</span><span style="color: #000000;">&#40;</span>index<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Access the item.Xml if you need to, or use the</span><br />
&nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// title, link, description, comments, etc...</span><br />
&nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">Title</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">Link</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;=================================&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; <span style="color: #008080; font-style: italic;">// Once we've processed them, mark them as read.</span><br />
&nbsp; feed.<span style="color: #0000FF;">MarkAllItemsRead</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <br />
&nbsp; <span style="color: #008080; font-style: italic;">// Some extra information</span><br />
&nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span>.<span style="color: #0000FF;">Format</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;{0} unread out of {1} &quot;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;total items in RSS Platform storage.&quot;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">+</span><span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>Press ENTER to continue.<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span>, <br />
&nbsp; &nbsp; itemCount <span style="color: #008000;">-</span> readCount, itemCount<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; Console.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #0600FF;">return</span> result<span style="color: #008000;">;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/windows-rss-platform-part-the-first/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

