<?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>Crows Programming &#187; General</title>
	<atom:link href="http://www.crowsprogramming.com/archives/category/general/feed" rel="self" type="application/rss+xml" />
	<link>http://www.crowsprogramming.com</link>
	<description>Computer Programming and Random Blurbs</description>
	<lastBuildDate>Tue, 08 Dec 2009 05:44:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>I&#8217;m Back</title>
		<link>http://www.crowsprogramming.com/archives/91</link>
		<comments>http://www.crowsprogramming.com/archives/91#comments</comments>
		<pubDate>Thu, 16 Jul 2009 02:58:14 +0000</pubDate>
		<dc:creator>crow</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=91</guid>
		<description><![CDATA[Hey guys, sorry I&#8217;ve been away for a bit. I&#8217;ll try and respond to your questions, especially those over on the C# dynamic web service page.
I&#8217;ll be adding a whole lot of new content as well. If anyone has any suggestions on a topic feel free to leave them for me.
See Ya!!
]]></description>
			<content:encoded><![CDATA[<p>Hey guys, sorry I&#8217;ve been away for a bit. I&#8217;ll try and respond to your questions, especially those over on the <a href="http://www.crowsprogramming.com/archives/66">C# dynamic web service page</a>.</p>
<p>I&#8217;ll be adding a whole lot of new content as well. If anyone has any suggestions on a topic feel free to leave them for me.</p>
<p>See Ya!!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crowsprogramming.com/archives/91/feed</wfw:commentRss>
		<slash:comments>48</slash:comments>
		</item>
		<item>
		<title>How To Extract A Zip File With C#</title>
		<link>http://www.crowsprogramming.com/archives/50</link>
		<comments>http://www.crowsprogramming.com/archives/50#comments</comments>
		<pubDate>Fri, 03 Apr 2009 05:49:51 +0000</pubDate>
		<dc:creator>crow</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=50</guid>
		<description><![CDATA[The .Net Framework Class Libraries are great and allow you to manipulate many different file formats. Unfortunately they seemed to have left out support for standard Zip files. Thankfully our good friends over at  ICSharpCode have released an awesome little library for handling Zip files called the SharpZipLib.
The library is very simple to use [...]]]></description>
			<content:encoded><![CDATA[<p>The .Net Framework Class Libraries are great and allow you to manipulate many different file formats. Unfortunately they seemed to have left out support for standard Zip files. Thankfully our good friends over at <a href=http://www.icsharpcode.net/> ICSharpCode</a> have released an awesome little library for handling Zip files called the SharpZipLib.</p>
<p>The library is very simple to use and supports several formats including:</p>
<ul>
<li> Zip </li>
<li> GZip </li>
<li> Tar </li>
<li> BZip2 </li>
</ul>
<h2>Using SharpZipLib </h2>
<p>Extracting a file with SharpZipLib is really quite simple. First you create a ZipInputStream from an existing FileStream to the zip file.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">ZipInputStream zinstream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ZipInputStream<span style="color: #000000;">&#40;</span>File.<span style="color: #0000FF;">OpenRead</span><span style="color: #000000;">&#40;</span>source<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Next you read the entries from the zip file, which could be either a file or directory.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">ZipEntry theEntry<span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>theEntry <span style="color: #008000;">=</span> zinstream.<span style="color: #0000FF;">GetNextEntry</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">// if theEntry is a directory then create it on disk</span>
   <span style="color: #008080; font-style: italic;">// otherwise it’s a file so read from zinstream just like</span>
   <span style="color: #008080; font-style: italic;">// you would any other file</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The ZipEntry represents either a file or directory that must be read and reproduced in the target directory where you are extracting the files to. GetNextEntry will continue to return either the next directory or the next file in the Zip file until no more exist. Here is a complete example of extracting all files (while maintaining directory structure) to a local folder.</p>
<h2>The Extraction Function</h2>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">int</span> ExtractAll<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> source, <span style="color: #FF0000;">string</span> destination<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            ZipInputStream zinstream <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// used to read from the zip file</span>
            <span style="color: #FF0000;">int</span> numFileUnzipped <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// number of files extracted from the zip file</span>
&nbsp;
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// create a zip input stream from source zip file</span>
                zinstream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ZipInputStream<span style="color: #000000;">&#40;</span>File.<span style="color: #0000FF;">OpenRead</span><span style="color: #000000;">&#40;</span>source<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// we need to extract to a folder so we must create it if needed</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>Directory.<span style="color: #0000FF;">Exists</span><span style="color: #000000;">&#40;</span>destination<span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span>
                    Directory.<span style="color: #0000FF;">CreateDirectory</span><span style="color: #000000;">&#40;</span>destination<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                ZipEntry theEntry<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// an entry in the zip file which could be a file or directory</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// now, walk through the zip file entries and copy each file/directory</span>
                <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>theEntry <span style="color: #008000;">=</span> zinstream.<span style="color: #0000FF;">GetNextEntry</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #FF0000;">string</span> dirname <span style="color: #008000;">=</span> Path.<span style="color: #0000FF;">GetDirectoryName</span><span style="color: #000000;">&#40;</span>theEntry.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// the file path</span>
                    <span style="color: #FF0000;">string</span> fname   <span style="color: #008000;">=</span> Path.<span style="color: #0000FF;">GetFileName</span><span style="color: #000000;">&#40;</span>theEntry.<span style="color: #0000FF;">Name</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>      <span style="color: #008080; font-style: italic;">// the file name</span>
&nbsp;
                    <span style="color: #008080; font-style: italic;">// if a path name exists we should create the directory in the destination folder</span>
                    <span style="color: #FF0000;">string</span> target <span style="color: #008000;">=</span> destination <span style="color: #008000;">+</span> Path.<span style="color: #0000FF;">DirectorySeparatorChar</span> <span style="color: #008000;">+</span> dirname<span style="color: #008000;">;</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>dirname.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>Directory.<span style="color: #0000FF;">Exists</span><span style="color: #000000;">&#40;</span>target<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> 
                        Directory.<span style="color: #0000FF;">CreateDirectory</span><span style="color: #000000;">&#40;</span>target<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    <span style="color: #008080; font-style: italic;">// now we know the proper path exists in the destination so copy the file there</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>fname <span style="color: #008000;">!=</span> <span style="color: #FF0000;">String</span>.<span style="color: #0000FF;">Empty</span><span style="color: #000000;">&#41;</span>
                    <span style="color: #000000;">&#123;</span>
                        DecompressAndWriteFile<span style="color: #000000;">&#40;</span>destination <span style="color: #008000;">+</span> Path.<span style="color: #0000FF;">DirectorySeparatorChar</span> <span style="color: #008000;">+</span> theEntry.<span style="color: #0000FF;">Name</span>, zinstream<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                        numFileUnzipped<span style="color: #008000;">++;</span>
                    <span style="color: #000000;">&#125;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                throw<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">finally</span>
            <span style="color: #000000;">&#123;</span>
                zinstream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> numFileUnzipped<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span></pre></div></div>

<p>And the DecompressAndWrite method:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> DecompressAndWriteFile<span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">string</span> destination, ZipInputStream source <span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            FileStream wstream <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">try</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #008080; font-style: italic;">// create a stream to write the file to</span>
                wstream <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span>destination<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> block <span style="color: #008000;">=</span> <span style="color: #FF0000;">2048</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// number of bytes to decompress for each read from the source</span>
&nbsp;
                <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span>block<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// location to decompress the file to</span>
&nbsp;
                <span style="color: #008080; font-style: italic;">// now decompress and write each block of data for the zip file entry</span>
                <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #FF0000;">int</span> size <span style="color: #008000;">=</span> source.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>data, <span style="color: #FF0000;">0</span>, data.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>size <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                        wstream.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span>data, <span style="color: #FF0000;">0</span>, size<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF;">else</span>
                        break<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// no more data</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">catch</span><span style="color: #000000;">&#40;</span>Exception<span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                throw<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0600FF;">finally</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span> wstream <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span> <span style="color: #000000;">&#41;</span>
                    wstream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span></pre></div></div>

<p>Pretty neat huh? I love how simple it is.<br />
If those methods are placed into a class called Zip, then extracting a Zip file is as easy as:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Zip.<span style="color: #0000FF;">ExtractAll</span><span style="color: #000000;">&#40;</span>myZipFile, myDestinationFolder<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>I’ve put together a sample project that uses the SharpZipLib to extract the contents of a zip file. It basically just allows you to select the zip file and the destination from a form and uses the methods above. You can find it below.</p>
<p><a href="http://www.crowsprogramming.com/wp-content/uploads/2009/04/zipfileexample.zip">DOWNLOAD SAMPLE APPLICATION</a></p>
<p>Let me know if you find this useful or need any help!!!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crowsprogramming.com/archives/50/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Documentation Template Via Visual Studio Macro</title>
		<link>http://www.crowsprogramming.com/archives/40</link>
		<comments>http://www.crowsprogramming.com/archives/40#comments</comments>
		<pubDate>Thu, 02 Apr 2009 06:50:51 +0000</pubDate>
		<dc:creator>crow</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=40</guid>
		<description><![CDATA[Consistency is the key when working on computer software, especially if you are developing a large system with multiple programmers. You should be consistent with the layout and format of your source code. Unfortunately this can be difficult to achieve when time is precious – and isn’t it always. However Visual Studios macro system makes [...]]]></description>
			<content:encoded><![CDATA[<p>Consistency is the key when working on computer software, especially if you are developing a large system with multiple programmers. You should be consistent with the layout and format of your source code. Unfortunately this can be difficult to achieve when time is precious – and isn’t it always. However Visual Studios macro system makes it a breeze to automate this task with little to no work. Here is a simple way to implement this in C#.</p>
<p>What I would like to be able to do is execute a simple macro and see a dialog box that lets me enter information specific to the source file I am working with. That information will then be inserted into the file using a predefined template. The dialog will look something like this: </p>
<p><img src="http://www.crowsprogramming.com/wp-content/uploads/2009/04/templatedialog.png" /></p>
<p>To accomplish this task we need to things; a .Net class library that implements the dialog and the macro to launch it and interact with Visual Studio using its automation model.<br />
<span id="more-40"></span><br />
Creating the dialog is easy.  Simply create a class library in the .Net language of your choice and add a form to it. Then make sure it has public properties that contain the information entered by the user so the macro can consume it. Once you have built your library you will have a .DLL file. This file needs to be copied to:<br /> “C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PublicAssemblies” <br />for Visual Studio 2008. For earlier versions the location will be similar. This will make your dialog type available to your macro.</p>
<p>Next comes the macro. To do this open the Macros IDE using Tools->Macros->Macros IDE. Right click MyMacros in the IDE and select Add->New Module. Name the new module AutoDocCSharpSourceFile. Right click MyMacros again and select add reference. Add a reference to your class library. In your new macro add the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Imports System.Text.RegularExpressions
&nbsp;
<span style="color: #000080;">Public</span> Module AutoDocCSharpSourceFile
    <span style="color: #000080;">Sub</span> AutoDocCSharpSourceFile()
        <span style="color: #008000;">' This is the main entry point for the macro
</span>        <span style="color: #008000;">' it will automatically document the c# source file selected
</span>        <span style="color: #008000;">' by the user in the custom dialog using the custom template
</span>        <span style="color: #008000;">' This should be used on new files only as it will not retain the
</span>        <span style="color: #008000;">' existing code
</span>
        <span style="color: #008000;">' note that we only acknowlege Windows line endings (carrige return/line feed)
</span>        <span style="color: #008000;">' if your source file is using a LF only you will have to replace all &quot;\r\n&quot; with &quot;\n&quot;
</span>
        <span style="color: #008000;">' also there is no error handling. You can catch any exceptions thrown and handle them
</span>        <span style="color: #008000;">' nicely or you can just let Visual Studio catch them and show them to you
</span>
        <span style="color: #008000;">' make sure Visual Studio has a document open... of course this may not be a C# file
</span>        <span style="color: #000080;">If</span> DTE.ActiveDocument <span style="color: #000080;">Is</span> <span style="color: #000080;">Nothing</span> <span style="color: #000080;">Then</span>
            MsgBox(&quot;Cannot Continue Because No Documents Are Open&quot;, MsgBoxStyle.OkOnly)
            Return
        <span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
&nbsp;
        <span style="color: #008000;">' first grab the text for the entire source file
</span>        <span style="color: #000080;">Dim</span> sel <span style="color: #000080;">As</span> TextSelection = DTE.ActiveDocument.Selection
        sel.SelectAll()
        <span style="color: #000080;">Dim</span> documentText <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = sel.Text
&nbsp;
        <span style="color: #008000;">' now extract the namespace name and the class name so
</span>        <span style="color: #008000;">' we can populate the dialog for the user
</span>        <span style="color: #000080;">Dim</span> namespaceName <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = GetNamespaceName(documentText)
        <span style="color: #000080;">Dim</span> className <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = GetClassName(documentText)
&nbsp;
        <span style="color: #008000;">' display the dialog, the user can enter source file specific information
</span>        <span style="color: #008000;">' such as the file description or rename the class etc...
</span>        <span style="color: #000080;">Dim</span> dialog <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> AutoTemplateDialog.AutoTemplateDialog(className, namespaceName)
        dialog.TopLevel = <span style="color: #000080;">True</span>
        dialog.ShowDialog()
        dialog.Focus()
&nbsp;
        <span style="color: #008000;">' exit if user hit cancel
</span>        <span style="color: #000080;">If</span> dialog.DialogResult = System.Windows.Forms.DialogResult.Cancel <span style="color: #000080;">Then</span>
            sel.StartOfDocument()
            dialog.Dispose()
            Return
        <span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
&nbsp;
        namespaceName = dialog.NamespaceName
        className = dialog.ClassName
&nbsp;
        <span style="color: #008000;">' we no longer need the contents of the source file
</span>        sel.Delete(sel.Text.Length)
        sel.StartOfDocument()
&nbsp;
        <span style="color: #008000;">' now insert the template documentation
</span>        GenFileDescription(dialog.Description)
        GenUsingStatements(documentText)
        GenFile(className, namespaceName, documentText)
&nbsp;
        dialog.Dispose() <span style="color: #008000;">' it seems visual studio prevents automatic disposal?
</span>        <span style="color: #008000;">' strange behavior results after a while if this isn't done
</span>    <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
    <span style="color: #000080;">Sub</span> GenFileDescription(<span style="color: #000080;">ByVal</span> desc <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>)
        <span style="color: #008000;">' This will generate the file description that goes at the top of
</span>        <span style="color: #008000;">' the source file
</span>        <span style="color: #008000;">' desc - A text description of the source file that will be inserted
</span>
        <span style="color: #000080;">Dim</span> fileDescription <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> <span style="color: #008000;">' the file description that goes into the description tag
</span>
        <span style="color: #000080;">If</span> desc = <span style="color: #000080;">String</span>.Empty <span style="color: #000080;">Then</span>
            fileDescription = &quot;//&quot; + Environment.NewLine
        <span style="color: #000080;">Else</span>
            <span style="color: #008000;">' split the description into seperate lines so that we can add them into single line comments
</span>            <span style="color: #000080;">Dim</span> lines <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>() = Regex.Split(desc, &quot;\r\n&quot;)
&nbsp;
            <span style="color: #008000;">' comment each description line
</span>            <span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> <span style="color: #000080;">line</span> <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> <span style="color: #000080;">In</span> lines
                fileDescription += (&quot;//     &quot; + <span style="color: #000080;">line</span> + Environment.NewLine)
            <span style="color: #000080;">Next</span>
        <span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
&nbsp;
        <span style="color: #008000;">' now insert the header template
</span>        DTE.ActiveDocument.Selection.Insert( _
            &quot;#region File Description&quot; &amp; vbNewLine &amp; _
            &quot;//------------------------------------------------------------------------------&quot; &amp; vbNewLine &amp; _
            &quot;// &lt;file&gt;&quot; &amp; vbNewLine &amp; _
            &quot;//     &quot; &amp; DTE.ActiveDocument.Name &amp; vbNewLine &amp; _
            &quot;// &lt;/file&gt;&quot; &amp; vbNewLine &amp; _
            &quot;//&quot; &amp; vbNewLine &amp; _
            &quot;// &lt;description&gt;&quot; &amp; vbNewLine &amp; _
                  fileDescription &amp; _
            &quot;// &lt;/description&gt;&quot; &amp; vbNewLine &amp; _
            &quot;//&quot; &amp; vbNewLine &amp; _
            &quot;//&quot; &amp; vbNewLine &amp; _
            &quot;// &lt;copyright file=&quot;&quot;&quot; &amp; DTE.ActiveDocument.Name &amp; &quot;&quot;&quot; company=&quot;&quot;My Company&quot;&quot;&gt;&quot; &amp; vbNewLine &amp; _
            &quot;//     Copyright (c) 2009 My Company Inc. All rights reserved.&quot; &amp; vbNewLine &amp; _
            &quot;// &lt;/copyright&gt;&quot; &amp; vbNewLine &amp; _
            &quot;//------------------------------------------------------------------------------&quot; &amp; vbNewLine &amp; _
            &quot;#endregion&quot; &amp; vbNewLine &amp; vbNewLine)
    <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
    <span style="color: #000080;">Sub</span> GenUsingStatements(<span style="color: #000080;">ByVal</span> documentText <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>)
        <span style="color: #008000;">' This will extract the using statements and wrap them
</span>        <span style="color: #008000;">' in a region
</span>        <span style="color: #008000;">' documentText is the body of the source file
</span>
        <span style="color: #000080;">Dim</span> mc <span style="color: #000080;">As</span> MatchCollection <span style="color: #008000;">' holds matches to the regular expression
</span>        <span style="color: #000080;">Dim</span> ex <span style="color: #000080;">As</span> Regex <span style="color: #008000;">' regular expression for finding the using statements
</span>        <span style="color: #000080;">Dim</span> nl <span style="color: #000080;">As</span> Regex <span style="color: #008000;">' holds maches for new lines
</span>
        <span style="color: #008000;">' find all using statements
</span>        ex = <span style="color: #000080;">New</span> Regex(&quot;using&quot;)
        nl = <span style="color: #000080;">New</span> Regex(&quot;\n&quot;)
&nbsp;
        mc = ex.Matches(documentText)
&nbsp;
        <span style="color: #008000;">' start new region in source file
</span>        DTE.ActiveDocument.Selection.Insert(&quot;#region Using Statements&quot; &amp; vbNewLine)
&nbsp;
        <span style="color: #008000;">' now place each using statement in the region
</span>        <span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> m <span style="color: #000080;">As</span> Match <span style="color: #000080;">In</span> mc
            <span style="color: #000080;">Dim</span> x <span style="color: #000080;">As</span> Match = nl.Match(documentText.Substring(m.Index)) <span style="color: #008000;">'end statement at newline
</span>
            <span style="color: #000080;">If</span> x.Success <span style="color: #000080;">Then</span>
                DTE.ActiveDocument.Selection.Insert(documentText.Substring(m.Index, x.Index))
            <span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
        <span style="color: #000080;">Next</span>
&nbsp;
        <span style="color: #008000;">' close off the region
</span>        DTE.ActiveDocument.Selection.Insert(&quot;#endregion&quot; &amp; vbNewLine &amp; vbNewLine)
&nbsp;
    <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
&nbsp;
    <span style="color: #000080;">Function</span> GetNamespaceName(<span style="color: #000080;">ByVal</span> documentText <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
        <span style="color: #008000;">' finds the root namespace defined in the document and returns it
</span>
        <span style="color: #000080;">Dim</span> mc <span style="color: #000080;">As</span> Match <span style="color: #008000;">' matches in regular expression
</span>
        <span style="color: #000080;">Dim</span> ex <span style="color: #000080;">As</span> Regex
        <span style="color: #000080;">Dim</span> nl <span style="color: #000080;">As</span> Regex
&nbsp;
        <span style="color: #008000;">' use regular expression to find the namespace name
</span>        ex = <span style="color: #000080;">New</span> Regex(&quot;namespace[^\r\n]*&quot;)
&nbsp;
        mc = ex.Match(documentText)
&nbsp;
        <span style="color: #008000;">' return result
</span>        <span style="color: #000080;">If</span> mc.Success <span style="color: #000080;">Then</span>
            Return mc.Value.Substring(10) <span style="color: #008000;">' using 10 will create a 
</span>            <span style="color: #008000;">' a problem if anything comes
</span>            <span style="color: #008000;">' before the namespace keyword
</span>        <span style="color: #000080;">Else</span>
            Return &quot;&quot;
        <span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
&nbsp;
    <span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
    <span style="color: #000080;">Function</span> GetClassName(<span style="color: #000080;">ByVal</span> documentText <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
        <span style="color: #008000;">' finds the class name defined in the document
</span>        <span style="color: #000080;">Dim</span> mc <span style="color: #000080;">As</span> Match
&nbsp;
        <span style="color: #000080;">Dim</span> ex <span style="color: #000080;">As</span> Regex
        <span style="color: #000080;">Dim</span> nl <span style="color: #000080;">As</span> Regex
&nbsp;
        <span style="color: #008000;">' use a regular expression just as in finding the namespace name
</span>        ex = <span style="color: #000080;">New</span> Regex(&quot;class[^\r\n]*&quot;)
&nbsp;
        mc = ex.Match(documentText)
&nbsp;
        <span style="color: #000080;">If</span> mc.Success <span style="color: #000080;">Then</span>
            Return mc.Value.Substring(6)
        <span style="color: #000080;">Else</span>
            Return &quot;&quot;
        <span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
&nbsp;
    <span style="color: #000080;">End</span> <span style="color: #000080;">Function</span>
&nbsp;
    <span style="color: #000080;">Sub</span> GenFile(<span style="color: #000080;">ByVal</span> className <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>, <span style="color: #000080;">ByVal</span> namespaceName <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>, <span style="color: #000080;">ByVal</span> documentText <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>)
        <span style="color: #008000;">' now we will generate the body of the source file
</span>
        <span style="color: #008000;">' insert initial class and namespace declaration
</span>        DTE.ActiveDocument.Selection.Insert(&quot;namespace &quot; &amp; namespaceName &amp; vbNewLine &amp; &quot;{&quot; &amp; vbNewLine &amp; vbTab &amp; &quot;public class &quot; &amp; className &amp; vbNewLine &amp; vbTab &amp; &quot;{&quot; &amp; vbNewLine)
&nbsp;
        <span style="color: #008000;">' insert the regions of the body
</span>        InsertRegion(&quot;Properties&quot;)
        InsertRegion(&quot;Events&quot;)
        InsertRegion(&quot;Constructors&quot;)
        InsertRegion(&quot;Public Methods&quot;)
        InsertRegion(&quot;Private Methods&quot;)
        InsertRegion(&quot;Fields&quot;)
&nbsp;
        <span style="color: #008000;">' close namespace and class
</span>        DTE.ActiveDocument.Selection.Insert(vbTab &amp; &quot;}&quot; &amp; vbNewLine &amp; &quot;}&quot;)
&nbsp;
    <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
    <span style="color: #000080;">Sub</span> InsertRegion(<span style="color: #000080;">ByVal</span> name <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>)
        <span style="color: #008000;">' just a helper routine to add a region statment to the source file
</span>        DTE.ActiveDocument.Selection.Insert(vbTab &amp; vbTab &amp; &quot;#region &quot; &amp; name &amp; vbNewLine &amp; vbTab &amp; vbTab &amp; &quot;#endregion&quot; &amp; vbNewLine &amp; vbNewLine)
    <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
<span style="color: #000080;">End</span> Module</pre></div></div>

<p>The macro is pretty straight forward with lots of documentation. It primarily is using the EnvDTE80.DTE2 class to interact with Visual Studio and write the source template.</p>
<p>Once you have saved the macro you can execute it by opening the Macro Explorer and running the AutoDocCSharpSourceFile macro. If you want you can also bind it to a key combination so you don’t have to use the explorer. You can accomplish this using the options dialog from the tools menu.</p>
<p>When you run the macro you should see a file header that looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080;">#region File Description</span>
<span style="color: #008080; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #008080; font-style: italic;">// &lt;file&gt;</span>
<span style="color: #008080; font-style: italic;">//     Program.cs</span>
<span style="color: #008080; font-style: italic;">// &lt;/file&gt;</span>
<span style="color: #008080; font-style: italic;">//</span>
<span style="color: #008080; font-style: italic;">// &lt;description&gt;</span>
<span style="color: #008080; font-style: italic;">//     This is a TEST!</span>
<span style="color: #008080; font-style: italic;">// &lt;/description&gt;</span>
<span style="color: #008080; font-style: italic;">//</span>
<span style="color: #008080; font-style: italic;">//</span>
<span style="color: #008080; font-style: italic;">// &lt;copyright file=&quot;Program.cs&quot; company=&quot;My Company&quot;&gt;</span>
<span style="color: #008080; font-style: italic;">//     Copyright (c) 2009 My Company Inc. All rights reserved.</span>
<span style="color: #008080; font-style: italic;">// &lt;/copyright&gt;</span>
<span style="color: #008080; font-style: italic;">//------------------------------------------------------------------------------</span>
<span style="color: #008080;">#endregion</span></pre></div></div>

<p>And several regions reserved for specific C# constructs. The one issue with the current implementation is that it only preserves a single class name, the namespace name, and the using directives. Anything else in the file is lost.  I’ve also noticed a now and then on Vista the dialog appears behind Visual Studio, if that’s the case just alt-tab to get it back.</p>
<p>I Hope this proves helpful to you and saves you some time. I would love to hear feedback on any issues or improvements that could be made. </p>
<p><a href="http://www.crowsprogramming.com/wp-content/uploads/2009/04/autotemplatedialog.zip">Here is my Visual Studio solution that implements the dialog window as well as the VB script macro.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.crowsprogramming.com/archives/40/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>WP-Syntax Test</title>
		<link>http://www.crowsprogramming.com/archives/10</link>
		<comments>http://www.crowsprogramming.com/archives/10#comments</comments>
		<pubDate>Sun, 08 Feb 2009 06:59:06 +0000</pubDate>
		<dc:creator>crow</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=10</guid>
		<description><![CDATA[Just wanted to check out the WP-Syntax plugin and see how it works/looks.

&#160;
#include &#60;stdio.h&#62;
#include &#60;stdlib.h&#62;
&#160;
int main&#40; void &#41;
&#123;
   printf&#40; &#34;HELLO WP-Syntax\n&#34; &#41;;
&#160;
   return 0;
&#125;

]]></description>
			<content:encoded><![CDATA[<p>Just wanted to check out the WP-Syntax plugin and see how it works/looks.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">&nbsp;
<span style="color: #339900;">#include &lt;stdio.h&gt;</span>
<span style="color: #339900;">#include &lt;stdlib.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">void</span> <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">&quot;HELLO WP-Syntax<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
   <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.crowsprogramming.com/archives/10/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Welcome!!</title>
		<link>http://www.crowsprogramming.com/archives/7</link>
		<comments>http://www.crowsprogramming.com/archives/7#comments</comments>
		<pubDate>Fri, 06 Feb 2009 06:57:19 +0000</pubDate>
		<dc:creator>crow</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.crowsprogramming.com/?p=7</guid>
		<description><![CDATA[Welcome to Crows Programming!
This site is dedicated to my various rants and raves, mostly related to computer programming and related technologies.
I look forward to hearing feedback from all of you on my thoughts and ideas!
~Crow&#8211;
]]></description>
			<content:encoded><![CDATA[<p>Welcome to Crows Programming!</p>
<p>This site is dedicated to my various rants and raves, mostly related to computer programming and related technologies.</p>
<p>I look forward to hearing feedback from all of you on my thoughts and ideas!</p>
<p>~Crow&#8211;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.crowsprogramming.com/archives/7/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
