<?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>Ellis Web Development &#187; Code</title>
	<atom:link href="http://ellisweb.net/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://ellisweb.net</link>
	<description>Thoughts, Articles &#38; Links on Programming &#38; Technology by Yaakov Ellis</description>
	<lastBuildDate>Tue, 15 Nov 2011 14:43:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Numeric String Sort in C#</title>
		<link>http://ellisweb.net/2011/06/numeric-string-sort-in-c/</link>
		<comments>http://ellisweb.net/2011/06/numeric-string-sort-in-c/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 07:06:42 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[icomparer]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://ellisweb.net/?p=470</guid>
		<description><![CDATA[Scenario: you have a List&#60;string&#62; collection where the contents could be alpha, numeric, or alphanumeric. If you just sort the collection using the built-in Sort() method, it will use string.CompareTo(), treating each item in the collection as a string. Thus a collection with the following values ["1", "44", "22", 4", "5"] will end up being [...]]]></description>
			<content:encoded><![CDATA[<p>Scenario: you have a <code>List&lt;string&gt;</code> collection where the contents could be alpha, numeric, or alphanumeric. If you just sort the collection using the built-in <code>Sort()</code> method, it will use <code>string.CompareTo()</code>, treating each item in the collection as a string. Thus a collection with the following values <code>["1", "44", "22", 4", "5"]</code> will end up being sorted as <code>["1", "22", "4", "44", "5"]</code> &#8211; even though you might want the items to be treated as numbers (in which case they would be sorted as <code>["1", "4", "5", "22", "44"]</code>.</p>
<p>The solution to this is to use a custom comparer class. I put the following together:</p>
<pre class="csharp" name="code">public class NumericStringSort : IComparer {

    int IComparer.Compare(string a, string b) {
        decimal aDec;
        decimal bDec;
        if (decimal.TryParse(a, out aDec) &amp;&amp; decimal.TryParse(b, out bDec)) {
            return aDec.CompareTo(bDec);
        } else {
            return a.CompareTo(b);
        }
    }

    public static IComparer NumericStringSorter() {
        return (IComparer) new NumericStringSort();
    }
}</pre>
<p>As you can see, the class implements the <a href="http://msdn.microsoft.com/en-us/library/system.collections.icomparer.aspx">IComparer</a> interface. If both of the strings can be converted into decimals, then the decimal comparison is used. Otherwise the string comparison is used.</p>
<p>You can see a usage example through the following set of unit tests (as you can see, it also handles mixed alphanumeric sets gracefully, sorting all numeric items to the front of the list in numeric order, followed by all alpha entries in alphabetical order):</p>
<pre class="csharp" name="code">[TestFixture]
public class NumericStringSortTests {

    [Test]
    public void TestNumericSort_AllNumeric() {
        List items = new List { "1", "44", "22", "4", "5" };
        items.Sort(NumericStringSort.NumericStringSorter());
        List expectedItems = new List { "1", "4", "5", "22", "44" };
        Assert.IsTrue(items.SequenceEqual(expectedItems));
    }

    [Test]
    public void TestNumericSort_MixesAlphaNumeric() {
        List items = new List { "a", "c", "d", "b", "1", "2", "22", "3" };
        items.Sort(NumericStringSort.NumericStringSorter());
        List expectedItems = new List { "1", "2", "3", "22", "a", "b", "c", "d" };
        Assert.IsTrue(items.SequenceEqual(expectedItems));
    }

    [Test]
    public void TestNumericSort_AllAlpha() {
        List items = new List { "a", "c", "d", "b" };
        items.Sort(NumericStringSort.NumericStringSorter());
        List expectedItems = new List { "a", "b", "c", "d" };
        Assert.IsTrue(items.SequenceEqual(expectedItems));
    }

}</pre>
<p>This is something that I have needed on previous occasions &#8211; perhaps it may be of use to someone else.
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2011%2F06%2Fnumeric-string-sort-in-c%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2011%2F06%2Fnumeric-string-sort-in-c%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2011/06/numeric-string-sort-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Connecting to Excel and Access Files using .Net on a 64-bit Server</title>
		<link>http://ellisweb.net/2010/01/connecting-to-excel-and-access-files-using-net-on-a-64-bit-server/</link>
		<comments>http://ellisweb.net/2010/01/connecting-to-excel-and-access-files-using-net-on-a-64-bit-server/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 13:30:42 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[64-bit]]></category>
		<category><![CDATA[access]]></category>
		<category><![CDATA[ace]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[jet]]></category>
		<category><![CDATA[odbc]]></category>
		<category><![CDATA[oledb]]></category>

		<guid isPermaLink="false">http://ellisweb.net/?p=436</guid>
		<description><![CDATA[If you are trying to query a MS Excel (.xls, .xlsx, .xlsb) or MS Access (.mdb, .accdb) file on a 64-bit server and are getting one of the following error messages: The &#8216;Microsoft.Jet.OLEDB.4.0&#8242; provider is not registered on the local machine. The &#8216;Microsoft.ACE.OLEDB.12.0&#8242; provider is not registered on the local machine. Then it is necessary [...]]]></description>
			<content:encoded><![CDATA[<p>If you are trying to query a MS Excel (.xls, .xlsx, .xlsb) or MS Access (.mdb, .accdb) file on a 64-bit server and are getting one of the following error messages:</p>
<ul>
<li>The &#8216;Microsoft.Jet.OLEDB.4.0&#8242; provider is not registered on the local machine.</li>
<li>The &#8216;Microsoft.ACE.OLEDB.12.0&#8242; provider is not registered on the local machine.</li>
</ul>
<p>Then it is necessary to install <a href="http://www.microsoft.com/downloads/details.aspx?familyid=C06B8369-60DD-4B64-A44B-84B371EDE16D&amp;displaylang=en">2010 Office System Driver Beta: Data Connectivity Components</a> on the server (the reason for this is the old Jet4.0 drover does not exist for 64 bit, and the ACE driver needed to read the newer formats is not installed by default). If you are using a version of Windows with UAC enabled, be sure to do this as Administrator.</p>
<p>After installing the components, use the following connection string formats (from the page linked-to above):</p>
<ul>
<li>Using <strong>OLEDB</strong>, set the Provider argument of the ConnectionString property to “Microsoft.ACE.OLEDB.14.0”. Example: &#8220;Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Path_To_File&#8221;</li>
<li>Using <strong>OLEDB </strong>and connecting to a Microsoft Office Excel file, add “Excel 14.0” to the Extended Properties of the OLEDB connection string defined in the previous bullet point.</li>
<li>Using <strong>ODBC</strong>:
<ul>
<li>Connecting to Microsoft Office Access (.mdb or .accdb): set the Connection String to “Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=path to mdb/accdb file”</li>
<li>Connecting to Microsoft Office Excel (.xls, .xlsx, .xlsb): set the Connection String to “Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=path to xls/xlsx/xlsm/xlsb file”</li>
</ul>
</li>
</ul>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2010%2F01%2Fconnecting-to-excel-and-access-files-using-net-on-a-64-bit-server%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2010%2F01%2Fconnecting-to-excel-and-access-files-using-net-on-a-64-bit-server%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2010/01/connecting-to-excel-and-access-files-using-net-on-a-64-bit-server/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Fixing the &#8220;circular file references are not allowed&#8221; Error in ASP.net</title>
		<link>http://ellisweb.net/2009/12/fixing-the-circular-file-references-are-not-allowed-error-in-asp-net/</link>
		<comments>http://ellisweb.net/2009/12/fixing-the-circular-file-references-are-not-allowed-error-in-asp-net/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 09:41:06 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[circular]]></category>
		<category><![CDATA[error]]></category>

		<guid isPermaLink="false">http://ellisweb.net/?p=433</guid>
		<description><![CDATA[If you get the &#8220;circular file references are not allowed&#8221; error in an ASP.net Website Project and you do not have any controls that have any obvious circular references, what does the error mean and how do you fix it? See this blog post from Siderite Zackwehdex as well as this MSDN forum post: by [...]]]></description>
			<content:encoded><![CDATA[<p>If you get the &#8220;<em>circular file references are not allowed</em>&#8221; error in an ASP.net Website Project and you do not have any controls that have any obvious circular references, what does the error mean and how do you fix it?</p>
<p>See this <a href="http://siderite.blogspot.com/2007/09/circular-file-references-are-not.html">blog post</a> from Siderite Zackwehdex as well as this <a href="http://forums.asp.net/t/922622.aspx">MSDN forum post</a>: by default, in a Website Project, ASP.net compiles one dll per folder in an ASP.net project. So if you have the following setup:</p>
<p style="padding-left: 30px;">/folder1/Control1.ascx &gt; References Control2<br />
/folder2/Control2.ascx &gt; References Control3<br />
/folder1/Control3.ascx</p>
<p>This means that the folder1 dll will reference the folder2 dll which will again reference the folder1 dll, causing a &#8220;circular file reference&#8221;.</p>
<p>Ways to fix it:</p>
<ol>
<li>Rearrange the layout of your controls (or masterpages) to remove the circular references (normally this will mean moving one control to another folder &#8211; in the example above, move control2 to folder1). This is the preferred solution.</li>
<li>Use batch=&#8221;false&#8221; in the <a href="http://msdn.microsoft.com/en-us/library/s10awwz0.aspx">compilation</a> tag of the web.config file. This will cause a new dll to be created for each control/page in the site. This should fix the error but is really lousy for performance, so it should be avoided (especially on production sites).</li>
</ol>
<p>(This has happened to me a couple of times already, so posting it here as a reminder to myself for the next time).
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2009%2F12%2Ffixing-the-circular-file-references-are-not-allowed-error-in-asp-net%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2009%2F12%2Ffixing-the-circular-file-references-are-not-allowed-error-in-asp-net%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2009/12/fixing-the-circular-file-references-are-not-allowed-error-in-asp-net/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Optimal Setup for SyntaxHighlighter &amp; TinyMCE</title>
		<link>http://ellisweb.net/2009/11/optimal-setup-for-syntaxhighlighter-and-tinymce/</link>
		<comments>http://ellisweb.net/2009/11/optimal-setup-for-syntaxhighlighter-and-tinymce/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 11:50:53 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[WebDev]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[syntaxhighlighter]]></category>

		<guid isPermaLink="false">http://ellisweb.net/?p=424</guid>
		<description><![CDATA[My favorite Javascript-based WYSIWYG editor is TinyMCE. I find it the most visually appealing, and the most well constructed from all of the offerings available on the Internet. When posting code for others to see, I prefer using SyntaxHighlighter. This javascript-based formatting tool by Alex Gorbatchev allows you to put the code to be displayed [...]]]></description>
			<content:encoded><![CDATA[<p>My favorite Javascript-based WYSIWYG editor is <a href="http://tinymce.moxiecode.com/">TinyMCE</a>. I find it the most visually appealing, and the most well constructed from all of the offerings available on the Internet.</p>
<p>When posting code for others to see, I prefer using <a href="http://alexgorbatchev.com/wiki/SyntaxHighlighter">SyntaxHighlighter</a>. This javascript-based formatting tool by Alex Gorbatchev allows you to put the code to be displayed into a Pre or TextArea tag, formats based on the code language, and allows for good degradation in case of NoScript.</p>
<p>I have <a href="http://ellisweb.net/2008/08/using-syntaxhighlighter-to-format-code-in-wordpress/">previously posted</a> on how to get SyntaxHighlighter to play nicely with the built-in text editor inside WordPress (TinyMCE). Now for the first time I needed to set it up on another platform (Telligent Community Server). Here are my recommendations on what to use to achieve optimal setup (this can work on any platform):</p>
<p>Problem 1: <strong>Need Friendlier Way to Input Code</strong> &#8211; The default method for adding code to be formatted woudl be to open up HTML mode in TinyMCE, type in the Pre or TextArea tag with specially formatted attributes, and put in the code. This is ok for myself (this is what I use in WordPress), but when designing a site for others, a friendlier interface is preferable. I found a plugin for TinyMCE: <strong><a href="http://github.com/RichGuk/syntaxhl">SyntaxHL</a></strong> by <a href="http://27smiles.com/2008/04/07/tinymce-syntaxhighlighter-plugin/">Richard Grundy</a>. After installing this (download and copy files to tinymce/plugins directory, and add plugin and button to TinyMCE.Init), a new button is added to TinyMCE that loads a popup which allows easy entry of the code to be formatted and customization of language and display options, without the need to enter html mode. (I would prefer something that could load up highlighted text when the button is pressed, but I will settle on this one for now)</p>
<p>Problem 2: <strong>TinyMCE adds BR Tags within PRE</strong> &#8211; When opening up TinyMCE it performs some cleanup of the Html that is input. Part of this cleanup involves (by default) substituting &lt;BR /&gt; tags instead of New Lines in the source text. For code that has already been saved, this presents a problem. When editing a post with code formatted in a &lt;PRE&gt; tag, instead of preserving the new lines, &lt;BR /&gt;&#8217;s are inserted, completely messing up the formatting. This has been noted <a href="http://drupal.org/node/297240">by</a> <a href="http://www.mainelydesign.com/blog/view/performatted-text-tinymce-pre-tag/">others</a>, and has even been reported in the TinyMCE <a href="http://tinymce.moxiecode.com/punbb/viewtopic.php?id=859">forums</a>, but so far the bug is still in there. To the rescue, another plugin for TinyMCE: <strong><a href="http://sourceforge.net/tracker/index.php?func=detail&amp;aid=2671750&amp;group_id=103281&amp;atid=738747">PreElementFix</a></strong> by <a href="http://sourceforge.net/users/tan73">T Andersen</a>. After installing the files and adding the plugin to TinyMCE.Init, PRE acts as it should: no more insertion of &lt;BR /&gt; tags, and the tab key adds a Tab to the text, rather than advancing to the next field.
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2009%2F11%2Foptimal-setup-for-syntaxhighlighter-and-tinymce%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2009%2F11%2Foptimal-setup-for-syntaxhighlighter-and-tinymce%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2009/11/optimal-setup-for-syntaxhighlighter-and-tinymce/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Casting vs. Converting in .Net</title>
		<link>http://ellisweb.net/2008/12/casting-vs-converting-in-net/</link>
		<comments>http://ellisweb.net/2008/12/casting-vs-converting-in-net/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 20:40:55 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[cast]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[types]]></category>

		<guid isPermaLink="false">http://ellisweb.net/?p=406</guid>
		<description><![CDATA[I recently saw a piece of code in a project that was throwing an exception that I did not understand: string boolString = (string)DataBinder.Eval(DataItem, "IsNew"); One would expect that boolString would be assigned the value &#8220;True&#8221; or &#8220;False&#8221; depending on the value of the IsNew property of the DataItem object. However, this through an Exception [...]]]></description>
			<content:encoded><![CDATA[<p>I recently saw a piece of code in a project that was throwing an exception that I did not understand:</p>
<pre name="code" class="c#">
string boolString = (string)DataBinder.Eval(DataItem, "IsNew");
</pre>
<p>One would expect that boolString would be assigned the value &#8220;True&#8221; or &#8220;False&#8221; depending on the value of the IsNew property of the DataItem object. However, this through an Exception with the message: &#8220;Cannot explicitly convert bool to string&#8221;. What is going on here?</p>
<p>The syntax of <em>(<strong>type</strong>)variable</em> attempts to <a href="http://msdn.microsoft.com/en-us/library/ms173105(VS.80).aspx">explicitly cast</a> the variable into the given type. Casting does not attempt to interpret the data in the variable &#8211; it just tries to fit the object referred to by the variable into the new data type. This will work whenever the two types are somehow compatible (for example, an int can be cast into a float with no exceptions) though sometimes it may result in data loss (a float cast into an int). However, in cases where there is no connection between the two classes, casting will result in an Exception (like the one that I received above).</p>
<p>(There is another way to cast an object &#8211; using the <em>as</em> keyword. This will return a null if the cast fails, and is <a href="http://www.codeproject.com/KB/cs/csharpcasts.aspx">much faster</a> than the explicit casting referred to above).</p>
<p>However, when converting an object, the conversion function has &#8220;knowledge&#8221; of the data contained in both the source and final object types, and will create the equivalent of the variable in the new data type. In the case above, using the System.Convert.ToString() method in place of the attempted cast to (string) would have worked fine. This is because the ToString method &#8220;knows&#8221; that a bool cannot be case into a string &#8211; but it also knows what the equivalent string to each value of a bool will be, and is able to process the operation accordingly. In this case, reflector uncovers the following code (in the Boolean class):</p>
<pre name="code" class="c#">
public string ToString() {
    if (!this) {
        return "False";
    }
    return "True";
}
</pre>
<p>This is obviously a very simple type conversion. However, there are many more complex conversion utilities built into the .Net framework (accessible through the <a href="http://msdn.microsoft.com/en-us/library)/system.convert.aspx">System.Convert</a> class &#8211; see the DateTime.ToString conversion for an example of this. Also see the IConvertible interface). While conversions can be more expensive to run, since they are strongly typed, they are checked at compile time, and when used properly are more reliable to use when producing stable code. (See the MSDN article on <a href="http://msdn.microsoft.com/en-us/library/ms173105.aspx">Casting and Type Conversions</a> for more info).
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2008%2F12%2Fcasting-vs-converting-in-net%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2008%2F12%2Fcasting-vs-converting-in-net%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2008/12/casting-vs-converting-in-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Signing Code Using PVK and SPC Files</title>
		<link>http://ellisweb.net/2008/08/signing-code-using-pvk-and-spc-files/</link>
		<comments>http://ellisweb.net/2008/08/signing-code-using-pvk-and-spc-files/#comments</comments>
		<pubDate>Thu, 28 Aug 2008 08:26:56 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[authenticode]]></category>
		<category><![CDATA[code-signing]]></category>
		<category><![CDATA[encrypt]]></category>
		<category><![CDATA[pfx]]></category>
		<category><![CDATA[pvk]]></category>
		<category><![CDATA[spc]]></category>
		<category><![CDATA[thawte]]></category>

		<guid isPermaLink="false">http://ellisweb.net/?p=374</guid>
		<description><![CDATA[I have a Windows Forms application that is ready for distribution. One of the last steps is getting code-signing working. We purchased a Code Signing certificate from Thawte. This resulted in a PVK (private key) and an SPC (certificate) file being delivered. Then the question arose of how to go about using them. Referring to [...]]]></description>
			<content:encoded><![CDATA[<p>I have a Windows Forms application that is ready for distribution. One of the last steps is getting code-signing working. We purchased a <a href="http://www.thawte.com/code-signing/index.html">Code Signing</a> certificate from Thawte. This resulted in a PVK (private key) and an SPC (certificate) file being delivered. Then the question arose of how to go about using them.</p>
<p>Referring to the documentation for <a href="http://msdn.microsoft.com/en-us/library/aa387764.aspx">SignTool.exe</a>, there did not seem to be a way to sign the code using the PVK and SPC files via the command line. Though this was possible using the GUI program (accessible using the -signwizard command line option), in order to get this integrated with my build process, I needed a way to initiate the code signing fully from the command line.</p>
<p>The solution turned out to be the following:</p>
<p>1) <strong>Convert the PVK and SPC files to a PFX file</strong> (Personal Information Exchange file &#8211; this encapsulates the info found in the PVK and SPC files). This is done using the pvk2pfx.exe executable (which had been included as part of the .Net SDK, and was found in C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin on my computer). I used the following syntax to do this (Pass1 is the original password for the PVK file, Pass2 is the new password for the pfx file. <a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1853015&amp;SiteID=1#1869336">Ref</a>):</p>
<pre name="code" class="html">pvk2pfx.exe -pvk mykey.pvk -pi Pass1 -spc mycert.spc -pfx newpfxfile.pfx -po Pass2 -f</pre>
<p>After this ran, I now had a PFX file called newpfxfile.pfx ready to be used.</li>
<p>2) <strong>Use SignTool and the PFX file to sign my code</strong>. Now that I had a PFX file, I was able to sign my code (and timestamp it) using the following command line syntax:</p>
<pre name="code" class="html">signtool.exe sign /f newpfxfile.pfx /p Pass2 /d "AppDescription" /du "AppURL" /t http://timestamp.verisign.com/scripts/timstamp.dll LocationOfCode</pre>
<p>Seems simple, but it took quite a lot of research to get this process right. Hopefully the info can help save someone else some time.
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2008%2F08%2Fsigning-code-using-pvk-and-spc-files%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2008%2F08%2Fsigning-code-using-pvk-and-spc-files%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2008/08/signing-code-using-pvk-and-spc-files/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Memory Profiler Eating Up My Memory</title>
		<link>http://ellisweb.net/2008/08/memory-profiler-eating-up-my-memory/</link>
		<comments>http://ellisweb.net/2008/08/memory-profiler-eating-up-my-memory/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 19:01:54 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[windows forms]]></category>
		<category><![CDATA[dottrace]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[jet-brains]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[profiler]]></category>
		<category><![CDATA[ram]]></category>
		<category><![CDATA[task-manager]]></category>

		<guid isPermaLink="false">http://ellisweb.net/?p=370</guid>
		<description><![CDATA[I have a Windows Forms app that has a very long and complicated initialization process. As the release is approaching, today was my day to try and shoft things around in the init, get it loading faster. So on recommendation from a few sources, I downloaded a profiler &#8211; Jet Brains dotTrace. Install and first [...]]]></description>
			<content:encoded><![CDATA[<p>I have a Windows Forms app that has a very long and complicated initialization process. As the release is approaching, today was my day to try and shoft things around in the init, get it loading faster. So on recommendation from a few sources, I downloaded a profiler &#8211; Jet Brains <a href="http://www.jetbrains.com/profiler/">dotTrace</a>. Install and first profile went well. However, as I started to make more changes and run more profiles, I noticed that there were no improvements in initialization time. If anything, things were getting worse. Task Manager helped pinpoint the culprit:</p>
<p><a href="http://ellisweb.net/wp-content/uploads/2008/08/dottrace.jpg"><img class="aligncenter size-medium wp-image-371" title="dotTrace in the Task Manager" src="http://ellisweb.net/wp-content/uploads/2008/08/dottrace-300x272.jpg" alt="" width="300" height="272" /></a></p>
<p>If the numbers are too small, that is 1,241,596 KB. Yikes. Compare that with the second and third-place contestants, FireFox 3 at 174,584 KB, Visual Studio 2005 at 144,940 KB.</p>
<p>This is what happened after I closed the profiler (see if you can guess when that happened):</p>
<p><a href="http://ellisweb.net/wp-content/uploads/2008/08/dottrace-pagefile.jpg"><img class="aligncenter size-medium wp-image-372" title="Memory drops after dotTrace is closed" src="http://ellisweb.net/wp-content/uploads/2008/08/dottrace-pagefile-300x272.jpg" alt="" width="300" height="272" /></a></p>
<p>And I thought that Firefox 2 was using <a href="http://ellisweb.net/2007/04/firefox-the-memory-hog/">a lot of memory</a>. I know that these programs are complicated, but I find it hard to comprehend how a program is supposed to help you track the memory usage of the applications that you are debugging when it ends up gobbling down over 1.2GB of RAM all by itself and making the computer basically unusable.
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2008%2F08%2Fmemory-profiler-eating-up-my-memory%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2008%2F08%2Fmemory-profiler-eating-up-my-memory%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2008/08/memory-profiler-eating-up-my-memory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ajax Progress Image Generator</title>
		<link>http://ellisweb.net/2008/08/ajax-progress-image-generator/</link>
		<comments>http://ellisweb.net/2008/08/ajax-progress-image-generator/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 12:32:16 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[WebDev]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[animated-gif]]></category>
		<category><![CDATA[gif]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[progress]]></category>
		<category><![CDATA[web-design]]></category>

		<guid isPermaLink="false">http://ellisweb.net/?p=364</guid>
		<description><![CDATA[I was in need of an Ajax-style animated gif that could be used to indicate that something is going on in the background or loading. I have seen them on hundreds of sites, but I don&#8217;t feel comfortable just copying someone else&#8217;s image to use for myself. After a bit of searching, I found this [...]]]></description>
			<content:encoded><![CDATA[<p>I was in need of an Ajax-style animated gif that could be used to indicate that something is going on in the background or loading. I have seen them on hundreds of sites, but I don&#8217;t feel comfortable just copying someone else&#8217;s image to use for myself.</p>
<p>After a bit of searching, I found this site: <a href="http://ajaxload.info/">AjaxLoad.Info</a>. This is a nice, simple site that generates 37 different &#8220;loading&#8221; animated gifs, allowing you to customize the style, foreground and background colors, and transparency. Definitely a good site to know about.</p>
<table border="0" width="100%">
<tbody>
<tr>
<td style="text-align: center;"><img class="size-full wp-image-366" title="ajax-loader" src="http://ellisweb.net/wp-content/uploads/2008/08/ajax-loader.gif" alt="" width="32" height="32" /></td>
</tr>
</tbody>
</table>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2008%2F08%2Fajax-progress-image-generator%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2008%2F08%2Fajax-progress-image-generator%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2008/08/ajax-progress-image-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using SyntaxHighlighter to Format Code in WordPress</title>
		<link>http://ellisweb.net/2008/08/using-syntaxhighlighter-to-format-code-in-wordpress/</link>
		<comments>http://ellisweb.net/2008/08/using-syntaxhighlighter-to-format-code-in-wordpress/#comments</comments>
		<pubDate>Thu, 14 Aug 2008 08:04:15 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[formatting]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[syntax]]></category>
		<category><![CDATA[syntaxhighlighter]]></category>

		<guid isPermaLink="false">http://ellisweb.net/?p=228</guid>
		<description><![CDATA[Based on a question in the StackOverflow beta site, I did some quick research into what are the best ways to perform syntax highlighting on code that is posted on blogs. Among the methods that were suggested (by myself or others): Hack together your own display logic to format it as you see fit Use [...]]]></description>
			<content:encoded><![CDATA[<p>Based on a <a href="http://beta.stackoverflow.com/questions/9051/what-is-best-blogging-host-for-programmerscode-formatting">question</a> in the StackOverflow beta site, I did some quick research into what are the best ways to perform syntax highlighting on code that is posted on blogs. Among the methods that were suggested (by myself or others):</p>
<ol>
<li>Hack together your own display logic to format it as you see fit</li>
<li>Use the <a href="http://code.google.com/p/syntaxhighlighter/">SyntaxHighlighter</a> JavaScript library</li>
<li>Use <a href="http://windowslivewriter.spaces.live.com/">Windows Live Writer</a> with the <a href="http://gallery.live.com/liveItemDetail.aspx?li=1f57bd9b-a692-4593-9e9e-e2962d9c0eee&amp;bt=9&amp;pl=8">Insert Code</a> plugin (I discuss that <a href="http://ellisweb.net/2007/11/trying-out-windows-live-writer/">here</a>)</li>
<li>For WordPress, use the <a href="http://wordpress.org/extend/plugins/wp-syntax/">WP-Syntax</a> plugin</li>
</ol>
<p>Coincidentally, I had heard Scott Hanselman talking about how he does code formatting just a couple of days ago, in <a href="http://hanselminutes.com/default.aspx?showID=143">Hanselminutes #125</a>, where he described how he posted code on his blog by putting it inside &lt;pre&gt;&lt;/pre&gt; tags, adding specific name and class attributes, and letting some JavaScript library do the formatting work. So I went to his blog, opened up a post with some code, and found my way to the <a href="http://code.google.com/p/syntaxhighlighter/">SyntaxHighlighter</a> JavaScript library. This is a very nifty library that handles formatting very nicely for a number of popular programming and scripting languages, and seemed to have a very easy implementation. So I decided to implement it for formatting code on my site.<br />
<span id="more-228"></span></p>
<p><strong>Installing SyntaxHighlighter</strong></p>
<p>The basic steps that you have to follow are:</p>
<ol>
<li>Download the <a href="http://code.google.com/p/syntaxhighlighter/downloads/list">files</a></li>
<li>Upload the core JavaScript files, any JavaScript files related to languages that you would want to format, the swf and css files to somewhere on your server</li>
<li>Add references in your code to the different files</li>
</ol>
<p>A very good and more detailed guide on how you can do this with your template can be found in this <a href="http://fahdshariff.blogspot.com/2008/07/syntax-highlighting-code-in-webpages.html">blog post</a> by Fahd Sharif.</p>
<p><strong>Displaying Code</strong></p>
<p>Once you have the script, swf and css references integrated with your theme, you can post code using the following convention:</p>
<pre class="html" name="code">&lt;pre name="code" class="langName"&gt;
Type your code here
&lt;/pre&gt;</pre>
<p>If you are doing this in WordPress, you will need to use the HTML editor to insert this. Language name reference is <a href="http://code.google.com/p/syntaxhighlighter/wiki/Languages">here</a>.</p>
<p>Here is what is looks like in action:</p>
<pre class="c#" name="code">public static class StringExtension {
  // Extension method to return first letter of a string
  public static string GetFirstLetter(this string str) {
    string val = (str.Length &gt; 0) ? str.SubString(0,1) : "";
    return val;
  }
}</pre>
<p><strong>Fixing TinyMCE</strong></p>
<p>After I first got this implemented in my theme, I had some problems getting it to work on actual blog posts. I would go to HTML mode in the editor, put in the &lt;pre name=&#8221;code&#8221; class=&#8221;lang&#8221;&gt;&#8230;&lt;/pre&gt; syntax, go back to the Visual editor and finish my post, preview it, and no formatting would be applied. After checking the HTML source being output, I noticed that the <em>name=&#8221;code&#8221; </em>attribute of the <em>pre </em>tag was not being output. After some more investigation, I discovered that this attribute was being stripped by the TinyMCE editor when I switched from HTML to Visual editing modes.</p>
<p>It turns out that TinyMCE has its own built-in HTML validation that it employs when text is loaded into the Visual editor. Included in this is the ability to strip out attributes that are for whatever reason not &#8220;approved&#8221;. Tag:Name seems to be one of those.</p>
<p>One potential workaround would be to only use the HTML editor. Though I could do this, I like the Visual editor better when writing.</p>
<p>The alternative is to change the list of approved attributes for the <em>pre</em> tag so that <em>name</em> will no longer be stripped. After researching this a bit (references: <a href="http://wiki.moxiecode.com/index.php/TinyMCE:Configuration/extended_valid_elements">1</a>, <a href="http://wordpress.org/support/topic/180502?replies=2#post-775393">2</a>, <a href="http://wordpress.org/support/topic/156276?replies=17#post-774445">3</a>), I did the following:</p>
<ol>
<li>Open up /wp-includes/js/tinymce/tiny_mce_config.php</li>
<li>Go to approximately line 298</li>
<li>Make the following edit (what you are doing here is giving tinyMCE an explicit list of attributes that are acceptable for the <em>pre</em> tag):</li>
</ol>
<pre class="php:firstline[298]" name="code">// Original: $content .= $ext_plugins . 'tinyMCE.init({' . $mce_options . '});';
$content .= $ext_plugins . 'tinyMCE.init({extended_valid_elements : "pre[id|class|title|style|dir|lang|name|onclick|onkeypress]",' . $mce_options . '});';</pre>
<ol>
<li>Upload the new file</li>
<li>Clear your browser cache</li>
<li>Delete the /wp-content/uploads/js folder from the server</li>
<li>Do a hard-refresh of your editor page in WordPress</li>
</ol>
<p>You should now be able to toggle back and forth between HTML and Visual editor modes in WordPress without losing the pre:name attribute necessary for SyntaxHighlighter to work. You will have to repeat this whenever you upgrade WordPress.</p>
<p><strong>Why Not Just Use the Plugin</strong></p>
<p>WordPress junkies at this point are muttering to themselves: why go to all this trouble? There is already a <a href="http://wordpress.org/extend/plugins/syntaxhighlighter/">plugin</a> that implements SyntaxHighlighter functionality and spares you all of this hard work. Here are the reasons why I chose to do this the hard way:</p>
<ol>
<li>The plugin ditches the &lt;pre&gt; syntax for a custom syntax that looks like this: [sourcecode language="lang"]CODE GOES HERE[/sourcecode]. While this eliminates the pre:name stripping issue that I mentioned before, it introduces something that in my opinion is much worse: tinyMCE now gets rid of any spatial formatting that you are using. I like to indent my code when necessary. If I am typing in the <em>pre</em> tag, tinyMCE respects all of my spacing, and does not strip any of it out (that is the whole purpose of <a href="http://www.w3schools.com/TAGS/tag_pre.asp"><em>pre</em></a>. Thus, I can save all of my indenting and spacing without any difficulties. If you are using a custom bracketed tag like the plugin does, tinyMCE strips a good deal of indenting and spacing, leaving code that just looks ugly.</li>
<li>The aforementioned code formatting issues carry over to the RSS feed as well. This way just works better.</li>
<li>The plugin includes all language JavaScript files. I don&#8217;t need to print Ruby code right now &#8211; why should I have to include the JS for it with my page?</li>
<li>Doing the implementation yourself gives greater flexibility in terms of using the <a href="http://code.google.com/p/syntaxhighlighter/w/list">different configuration options</a> that are available with SyntaxHighlighter.</li>
<li>It is more fun this way</li>
</ol>
<p><strong>Update:</strong> Engfer <a href="http://www.engfers.com/2008/10/16/how-to-allow-stripped-element-attributes-in-wordpress-tinymce-editor/">lambasts me</a> for editing wordpress core files that will be overwritten whenever wordpress is upgraded. Instead, he created a wordpress plugin that will allow you to override and define the allowable attributes for any elements parsed by TinyMCE. It is available for download <a href="http://www.engfers.com/plugins/tinymce-valid-elements/">here</a> and is definitely a better way to go than manually editing files. Thanks!
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2008%2F08%2Fusing-syntaxhighlighter-to-format-code-in-wordpress%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2008%2F08%2Fusing-syntaxhighlighter-to-format-code-in-wordpress%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2008/08/using-syntaxhighlighter-to-format-code-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Detecting Application Idle State in Windows Forms</title>
		<link>http://ellisweb.net/2008/02/detecting-application-idle-state-in-windows-forms/</link>
		<comments>http://ellisweb.net/2008/02/detecting-application-idle-state-in-windows-forms/#comments</comments>
		<pubDate>Thu, 07 Feb 2008 11:44:21 +0000</pubDate>
		<dc:creator>Yaakov Ellis</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[windows forms]]></category>
		<category><![CDATA[application idle]]></category>
		<category><![CDATA[idle]]></category>

		<guid isPermaLink="false">http://ellisweb.net/2008/02/detecting-application-idle-state-in-windows-forms/</guid>
		<description><![CDATA[On a recent project, I had the need to detect whether or not the application is idle, and if so, for how long has the idle state persisted. Idle in my case is defined as no mouse movement or keyboard activity when any of the forms of the application are in focus. If a different [...]]]></description>
			<content:encoded><![CDATA[<p>On a recent project, I had the need to detect whether or not the application is idle, and if so, for how long has the idle state persisted. Idle in my case is defined as no mouse movement or keyboard activity when any of the forms of the application are in focus. If a different program is in focus, I define this as being an application-idle state (for my app), regardless of whether or not there is input activity from keyboard or mouse.</p>
<p>On researching the subject, I found several approaches. The <a href="http://www.thescripts.com/forum/thread256888.html">main approach</a> I have seen is to use the static <a href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.idle.aspx">Application.Idle</a> event. This event fires whenever &#8220;application finishes processing and is about to enter the idle state&#8221; &#8211; In other words, whenever the application&#8217;s message queue is empty. The problem with this approach is that this event fires a lot, so much that it becomes impractical for tracking idle state the way that I need it (it doesn&#8217;t help that any Timer operating to track how long the idle state persists would set off Application.Idle, further complicating the situation).</p>
<p>The other approach that I have seen is to set up some Windows hooks to detect mouse and keyboard activity. I have zero experience operating with the Windows API, so thankfully, I found a post by Johan Danforth that gives some working code for doing exactly what I needed: <a href="http://weblogs.asp.net/jdanforth/archive/2006/06/21/454219.aspx">Detecting Idle Time with Mouse and Keyboard Hooks</a>. I integrated the code with my application and tested it out and it worked great.</p>
<p>There was one problem however: this code detects idle time for all applications. In other words, if your application is open but not in focus and you use your mouse or keyboard, the code changes your application status from Idle to Active. For my purposes (see definition if <em>idle</em> above) this is not good enough. So I inspected different properties of the System,Windows.Forms.Form class to see what could tell me whether or not a given form is active. The first candidates were Focused, TopLevel, TopMost and Visible, but none of these did the job. The property that ended up telling me exactly what I needed to know is <a href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.containsfocus.aspx">ContainsFocus</a>. This is a property of the Control class (from which Form inherits) and it &#8220;Gets a value indicating whether the control, or one of its child controls, currently has the input focus&#8221;. (Focused is not good enough, since it only returns true when the form itself has focus, but returns false when a child control contained within the form has focus).</p>
<p>I also needed to detect whether any of the secondary forms of my application had focus (since I could have more than one window open at a time, only one of which could have focus). Here is the code that I used:</p>
<pre name="code" class="c#">private bool DoesApplicationHaveFocus() {
  bool hasFocus = false;
  if (ContainsFocus) {
    hasFocus = true;
  } else {
    FormCollection forms = Application.OpenForms;
    foreach (Form f in forms) {
      if (f != null &#038;&#038; f.ContainsFocus) {
        hasFocus = true;
        break;
      }
    }
  }
  return hasFocus;
}</pre>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fellisweb.net%2F2008%2F02%2Fdetecting-application-idle-state-in-windows-forms%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fellisweb.net%2F2008%2F02%2Fdetecting-application-idle-state-in-windows-forms%2F&amp;source=yaakov&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://ellisweb.net/2008/02/detecting-application-idle-state-in-windows-forms/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

