<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Jeremy Simmons: The Blog - Code</title>
    <link>http://www.jeremysimmons.net/blog/</link>
    <description>Four out of Five Coders recommend</description>
    <language>en-us</language>
    <copyright>Jeremy Simmons 2005-2009</copyright>
    <lastBuildDate>Tue, 06 Oct 2009 15:46:36 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.2.8279.16125</generator>
    <managingEditor>jsimmons@jeremysimmons.net</managingEditor>
    <webMaster>jsimmons@jeremysimmons.net</webMaster>
    <item>
      <trackback:ping>http://www.jeremysimmons.net/blog/Trackback.aspx?guid=4335905a-431c-47b1-bb7f-696ebb4bf550</trackback:ping>
      <pingback:server>http://www.jeremysimmons.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeremysimmons.net/blog/PermaLink,guid,4335905a-431c-47b1-bb7f-696ebb4bf550.aspx</pingback:target>
      <dc:creator>Jeremy Simmons</dc:creator>
      <wfw:comment>http://www.jeremysimmons.net/blog/CommentView,guid,4335905a-431c-47b1-bb7f-696ebb4bf550.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeremysimmons.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=4335905a-431c-47b1-bb7f-696ebb4bf550</wfw:commentRss>
      <title>Databinding to a XmlDataProvider in WPF</title>
      <guid isPermaLink="false">http://www.jeremysimmons.net/blog/PermaLink,guid,4335905a-431c-47b1-bb7f-696ebb4bf550.aspx</guid>
      <link>http://www.jeremysimmons.net/blog/2009/10/06/DatabindingToAXmlDataProviderInWPF.aspx</link>
      <pubDate>Tue, 06 Oct 2009 15:46:36 GMT</pubDate>
      <description>I've resolved to start making the shift to WPF. It's about time. The technology has only been around for a good 3+ years. So far, I'm very excited by what I see. An invaluable reference has been &lt;a href="http://www.amazon.com/Programming-WPF-Chris-Sells/dp/0596510373/"&gt;Programming
WPF 2nd edition&lt;/a&gt; by Chris Sells. Specifically the appendices. The explination of
Xaml, and the Xaml Extensions removes the veil of confusion from all of the arcane
looking symbols going on in the Xaml code. Thanks for the awesome book Chris! I was
going through an article published in MSDN Magazine, &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163299.aspx"&gt;Data
Binding in WPF&lt;/a&gt; by John Papa. I encountered a very frustrating bug and I felt the
need to let the next developer know about the solution I uncovered. (The next developer
is sometimes me. I do very much love finding my own posts months or years down the
road that help me out). Maybe this will help you out.&lt;br&gt;
&lt;br&gt;
So, I'm building my Xaml out, and I have a perfectly good XmlDataProvider in my Resources.&lt;br&gt;
&lt;pre&gt;&amp;lt;XmlDataProvider x:Key="MoreColors" XPath="/colors"&amp;gt;&lt;br&gt;
&amp;lt;x:XData&amp;gt;&lt;br&gt;
&amp;lt;colors&amp;gt;&lt;br&gt;
&amp;lt;color name="pink"/&amp;gt;&lt;br&gt;
&amp;lt;color name="white"/&amp;gt;&lt;br&gt;
&amp;lt;color name="black"/&amp;gt;&lt;br&gt;
&amp;lt;color name="cyan"/&amp;gt;&lt;br&gt;
&amp;lt;color name="gray"/&amp;gt;&lt;br&gt;
&amp;lt;color name="magenta"/&amp;gt;&lt;br&gt;
&amp;lt;/colors&amp;gt;&lt;br&gt;
&amp;lt;/x:XData&amp;gt;&lt;br&gt;
&amp;lt;/XmlDataProvider&amp;gt;&lt;br&gt;
&lt;/pre&gt;
&lt;p&gt;
I Created the Binding to my Listbox as such.
&lt;/p&gt;
&lt;pre&gt;
&lt;br&gt;
&lt;/pre&gt;
&lt;pre&gt;
&lt;listbox x:name="lbColor" itemssource="{Binding Source={StaticResource Colors},  XPath=color/@name}"&gt;
&lt;/listbox&gt;
&lt;/pre&gt;
&lt;p&gt;
This whole thing is working in the Designer (Visual Studio 2008), but is displaying &lt;b&gt;nothing&lt;/b&gt; at
runtime. I whisk away to the &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.data.xmldataprovider.aspx"&gt;MSDN
Documentation for XmlDataProvider&lt;/a&gt; and find this clever little note.
&lt;/p&gt;
&lt;blockquote&gt; The root node of the XML data has an xmlns attribute that sets the XML
namespace to an empty string. This is a requirement for applying XPath queries to
a data island that is inline within the XAML page. In this inline case, the XAML,
and thus the data island, inherits the System.Windows namespace. Because of this,
you need to set the namespace blank to keep XPath queries from being qualified by
the System.Windows namespace, which would misdirect the queries.&lt;br&gt;
&lt;/blockquote&gt;Is it possible that this is the explination to the Debug output I'm seeing
in Visual Studio at runtime?&lt;p&gt;
&lt;/p&gt;
&lt;pre&gt;System.Windows.Data Error: 47 : XmlDataProvider has inline XML that does not explicitly set its XmlNamespace (xmlns="").&lt;/pre&gt;
&lt;p&gt;
I add the missing &lt;code&gt;xmlns=""&lt;/code&gt; to the root xml element (as shown below),
and things begin to work at runtime.
&lt;/p&gt;
&lt;pre&gt;&amp;lt;XmlDataProvider x:Key="MoreColors" XPath="/colors"&amp;gt;&lt;br&gt;
&amp;lt;x:XData&amp;gt;&lt;br&gt;
&amp;lt;colors xmlns=""&amp;gt;&lt;br&gt;
&amp;lt;color name="pink"/&amp;gt;&lt;br&gt;
&amp;lt;color name="white"/&amp;gt;&lt;/pre&gt;
&lt;p&gt;
In searching for the answer to this solution, I also came across this post &lt;a href="http://blog.wouldbetheologian.com/2009/07/why-wpf-databinding-is-awful-technology.html"&gt;Why
WPF databinding is an awful technology&lt;/a&gt;. I hope this is not shared feeling of frustration
many of us receive by wasted hours of our day with the &lt;a href="http://www.google.fr/"&gt;Moteur
de recherche du jour&lt;/a&gt; (&lt;a href="http://www.google.com/"&gt;search engine of the day&lt;/a&gt;).
It's a really useful tool if you can convince it to give you the right answer, and
people have posted about the particular phrase you're looking for.
&lt;/p&gt;
&lt;p&gt;
Good luck reader, with your journey to WPF enlightenment. It has been worth my time.&lt;br&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=4335905a-431c-47b1-bb7f-696ebb4bf550" /&gt;</description>
      <comments>http://www.jeremysimmons.net/blog/CommentView,guid,4335905a-431c-47b1-bb7f-696ebb4bf550.aspx</comments>
      <category>All things Microsoft</category>
      <category>Code</category>
    </item>
    <item>
      <trackback:ping>http://www.jeremysimmons.net/blog/Trackback.aspx?guid=09e4e24b-0642-461d-bd8a-5eb537133f1d</trackback:ping>
      <pingback:server>http://www.jeremysimmons.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeremysimmons.net/blog/PermaLink,guid,09e4e24b-0642-461d-bd8a-5eb537133f1d.aspx</pingback:target>
      <dc:creator>Jeremy Simmons</dc:creator>
      <wfw:comment>http://www.jeremysimmons.net/blog/CommentView,guid,09e4e24b-0642-461d-bd8a-5eb537133f1d.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeremysimmons.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=09e4e24b-0642-461d-bd8a-5eb537133f1d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When you have a ListView control in Windows Forms, and you don't want the horizontal
scroll bar to show up, you need to set the size of the columns to be the same 'Width'
as the ListView.<br />
What exactly is the 'Width' you should use here? Yes careful reader, I've been quoting
Width because we need to take a closer look at what's going on.<br />
Lets look at a dead simple example to clearly illustrate why this is a concern.<br /><br />
When I create a new Windows Forms application in Visual Studio, I get a standard sized
form. Dragging a ListView control from the toolbox will create a control with the
dimensions defined as 
</p>
        <pre>private void InitializeComponent()
{
   ...
   this.listView1.Size = new System.Drawing.Size(121, 97);
   ...
}</pre>
I added the following code to constructor of the Form after the <code>InitializeComponent()</code> call; <pre>void Form1()
{
   InitializeComponent();
   
   listView1.Columns.Add("Name");
   for (int i = 0; i &lt; 20; i++)
   {
      listView1.Items.Add(new string((char)(i + 33), 10));
   }
   // Naive width
   listView1.Columns[0].Width = listView1.Width;
}</pre><p>
What we end up with looks like the image below.
</p><p><img src="http://www.jeremysimmons.net/blog/content/binary/listview1.PNG" border="0" /></p><p>
This is definately not what we wanted. Even though none of my data is forcing my scroll
behavior, my column definition definitely is. I could change this to use the ClientSize
of the Control, I know that's smaller. The documentation on MSDN for <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.clientsize.aspx"> Control.ClientSize</a> makes
these remarks about the property. 
</p><blockquote> "The client area of a control is the bounds of the control, minus the
nonclient elements such as scroll bars, borders, title bars, and menus..." </blockquote> This
will work right? I'll just make a quick change to the code, recompile and... drat.
No love. <pre>    // Setting the width on the Column
   //<strike>listView1.Columns[0].Width
= listView1.Width;</strike> //listView1.Columns[0].Width = listView1.ClientSize.Width; 
<br />
}</pre><p>
What is going on here?!<br />
As it turns out, the calculation for ClientSize in the constructor is completely correct.
The control hasn't been drawn yet, and it has no knowledge of whether it will need
to use a vertical scrollbar to display the data. That's just fine control. I know
how to fix you. I know how wide a scroll bar is, and I can figure that out on my own...
Just watch me.
</p><pre>   // Setting the width on the Column
   //<strike>listView1.Columns[0].Width
= listView1.Width;</strike> //<strike>listView1.Columns[0].Width = listView1.ClientSize.Width;</strike> listView1.Columns[0].Width
= listView1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth; }</pre><p>
Ha! Take that ListView. Now my column displays perfectly, and I have no Horizontal
ScrollBar to contend with. Just to prove it to you, I'm going to check how wide it
should be after the form shows up, to make sure I'm right...
</p><pre>      //<strike>listView1.Columns[0].Width = listView1.Width;</strike> //<strike>listView1.Columns[0].Width
= listView1.ClientSize.Width;</strike> Debug.Print("ListView is {0} px wide and has
a client width of {1} px", listView1.Width, listView1.ClientSize.Width); Debug.Print("VerticalScrollBarWidth:
{0}", SystemInformation.VerticalScrollBarWidth); Debug.Print("Calculated width: {0}",
listView1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth); listView1.Columns[0].Width
= listView1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth; this.Load
+= new EventHandler(Form1_Load); } void Form1_Load(object sender, EventArgs e) { Debug.Print("Loaded
width: {0}", listView1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth);
} </pre>
Debug Output: <pre>ListView is 121 px wide and has a client width of 117 px
VerticalScrollBarWidth: 17
Calculated width: 100
Loaded width: 100
</pre><p>
Uh-oh. I have a bad feeling. It seems like the Width of the ClientSize after the control
has been painted is different than before it was painted. Could it be that when the
VerticalScrollBar is painted, the control does in fact know how much room it has left
to use to paint on the screen?! Isn't that in fact, exactly what the Remarks of the
ClientSize property told me? Do the numbers above not in fact make perfect sense!?
Is 117 - 17 = 100?! Yes... sigh. Perhaps I should have meditated on the implications
that the remarks on MSDN were accurate from the begging. 
</p><blockquote> "The client area of a control is the bounds of the control, <strong>minus
the nonclient elements such as scroll bars, borders, title bars, and menus</strong>..." </blockquote><p>
As it turns out, the correct time to set the width of a ListView Column is AFTER you've
determined that there will be scrollbars painted on the control.
</p><img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=09e4e24b-0642-461d-bd8a-5eb537133f1d" /></body>
      <title>How to remove the horizontal scrollbar from a ListView</title>
      <guid isPermaLink="false">http://www.jeremysimmons.net/blog/PermaLink,guid,09e4e24b-0642-461d-bd8a-5eb537133f1d.aspx</guid>
      <link>http://www.jeremysimmons.net/blog/2009/10/01/HowToRemoveTheHorizontalScrollbarFromAListView.aspx</link>
      <pubDate>Thu, 01 Oct 2009 16:05:19 GMT</pubDate>
      <description>&lt;p&gt;
When you have a ListView control in Windows Forms, and you don't want the horizontal
scroll bar to show up, you need to set the size of the columns to be the same 'Width'
as the ListView.&lt;br&gt;
What exactly is the 'Width' you should use here? Yes careful reader, I've been quoting
Width because we need to take a closer look at what's going on.&lt;br&gt;
Lets look at a dead simple example to clearly illustrate why this is a concern.&lt;br&gt;
&lt;br&gt;
When I create a new Windows Forms application in Visual Studio, I get a standard sized
form. Dragging a ListView control from the toolbox will create a control with the
dimensions defined as 
&lt;/p&gt;
&lt;pre&gt;private void InitializeComponent()
{
   ...
   this.listView1.Size = new System.Drawing.Size(121, 97);
   ...
}&lt;/pre&gt;
I added the following code to constructor of the Form after the &lt;code&gt;InitializeComponent()&lt;/code&gt; call; &lt;pre&gt;void Form1()
{
   InitializeComponent();
   
   listView1.Columns.Add("Name");
   for (int i = 0; i &amp;lt; 20; i++)
   {
      listView1.Items.Add(new string((char)(i + 33), 10));
   }
   // Naive width
   listView1.Columns[0].Width = listView1.Width;
}&lt;/pre&gt;
&lt;p&gt;
What we end up with looks like the image below.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.jeremysimmons.net/blog/content/binary/listview1.PNG" border="0"&gt;
&lt;/p&gt;
&lt;p&gt;
This is definately not what we wanted. Even though none of my data is forcing my scroll
behavior, my column definition definitely is. I could change this to use the ClientSize
of the Control, I know that's smaller. The documentation on MSDN for &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.clientsize.aspx"&gt; Control.ClientSize&lt;/a&gt; makes
these remarks about the property. 
&lt;/p&gt;
&lt;blockquote&gt; "The client area of a control is the bounds of the control, minus the
nonclient elements such as scroll bars, borders, title bars, and menus..." &lt;/blockquote&gt; This
will work right? I'll just make a quick change to the code, recompile and... drat.
No love. &lt;pre&gt;    // Setting the width on the Column
   //&lt;strike&gt;listView1.Columns[0].Width
= listView1.Width;&lt;/strike&gt; //listView1.Columns[0].Width = listView1.ClientSize.Width; 
&lt;br&gt;
}&lt;/pre&gt;
&lt;p&gt;
What is going on here?!&lt;br&gt;
As it turns out, the calculation for ClientSize in the constructor is completely correct.
The control hasn't been drawn yet, and it has no knowledge of whether it will need
to use a vertical scrollbar to display the data. That's just fine control. I know
how to fix you. I know how wide a scroll bar is, and I can figure that out on my own...
Just watch me.
&lt;/p&gt;
&lt;pre&gt;   // Setting the width on the Column
   //&lt;strike&gt;listView1.Columns[0].Width
= listView1.Width;&lt;/strike&gt; //&lt;strike&gt;listView1.Columns[0].Width = listView1.ClientSize.Width;&lt;/strike&gt; listView1.Columns[0].Width
= listView1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth; }&lt;/pre&gt;
&lt;p&gt;
Ha! Take that ListView. Now my column displays perfectly, and I have no Horizontal
ScrollBar to contend with. Just to prove it to you, I'm going to check how wide it
should be after the form shows up, to make sure I'm right...
&lt;/p&gt;
&lt;pre&gt;      //&lt;strike&gt;listView1.Columns[0].Width = listView1.Width;&lt;/strike&gt; //&lt;strike&gt;listView1.Columns[0].Width
= listView1.ClientSize.Width;&lt;/strike&gt; Debug.Print("ListView is {0} px wide and has
a client width of {1} px", listView1.Width, listView1.ClientSize.Width); Debug.Print("VerticalScrollBarWidth:
{0}", SystemInformation.VerticalScrollBarWidth); Debug.Print("Calculated width: {0}",
listView1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth); listView1.Columns[0].Width
= listView1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth; this.Load
+= new EventHandler(Form1_Load); } void Form1_Load(object sender, EventArgs e) { Debug.Print("Loaded
width: {0}", listView1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth);
} &lt;/pre&gt;
Debug Output: &lt;pre&gt;ListView is 121 px wide and has a client width of 117 px
VerticalScrollBarWidth: 17
Calculated width: 100
Loaded width: 100
&lt;/pre&gt;
&lt;p&gt;
Uh-oh. I have a bad feeling. It seems like the Width of the ClientSize after the control
has been painted is different than before it was painted. Could it be that when the
VerticalScrollBar is painted, the control does in fact know how much room it has left
to use to paint on the screen?! Isn't that in fact, exactly what the Remarks of the
ClientSize property told me? Do the numbers above not in fact make perfect sense!?
Is 117 - 17 = 100?! Yes... sigh. Perhaps I should have meditated on the implications
that the remarks on MSDN were accurate from the begging. 
&lt;/p&gt;
&lt;blockquote&gt; "The client area of a control is the bounds of the control, &lt;strong&gt;minus
the nonclient elements such as scroll bars, borders, title bars, and menus&lt;/strong&gt;..." &lt;/blockquote&gt; 
&lt;p&gt;
As it turns out, the correct time to set the width of a ListView Column is AFTER you've
determined that there will be scrollbars painted on the control.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=09e4e24b-0642-461d-bd8a-5eb537133f1d" /&gt;</description>
      <comments>http://www.jeremysimmons.net/blog/CommentView,guid,09e4e24b-0642-461d-bd8a-5eb537133f1d.aspx</comments>
      <category>All things Microsoft</category>
      <category>Code</category>
    </item>
    <item>
      <trackback:ping>http://www.jeremysimmons.net/blog/Trackback.aspx?guid=e722bd9e-4e5b-40a8-9867-932002dac07b</trackback:ping>
      <pingback:server>http://www.jeremysimmons.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeremysimmons.net/blog/PermaLink,guid,e722bd9e-4e5b-40a8-9867-932002dac07b.aspx</pingback:target>
      <dc:creator>Jeremy Simmons</dc:creator>
      <wfw:comment>http://www.jeremysimmons.net/blog/CommentView,guid,e722bd9e-4e5b-40a8-9867-932002dac07b.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeremysimmons.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=e722bd9e-4e5b-40a8-9867-932002dac07b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
What was that format string again? You know, the one for the GUID where you only output
the guid, but with no formatting?
</p>
        <p>
          <span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;">
            <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">using</span> System;<br /><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">public</span><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">class</span> GuidTest<br />
{<br />
    <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">public</span><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">static</span><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">void</span> Main()<br />
    {<br />
        <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">string</span>[]
guidFormats <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span> {<span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);">"N"</span>, <span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);">"D"</span>, <span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);">"B"</span>, <span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);">"P"</span>};<br />
        Guid g <span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;">=</span><span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">new</span> Guid();<br />
        Array.ForEach(guidFormats, <span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">delegate</span>(<span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;">string</span> t)<br />
        {<br />
            Console.WriteLine(<span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);">"{0}:{1}"</span>,
t, g.ToString(t));<br />
        });<br />
        Console.ReadLine();<br />
    }<br />
}</span>
        </p>
        <pre>N:00000000000000000000000000000000
D:00000000-0000-0000-0000-000000000000
B:{00000000-0000-0000-0000-000000000000}
P:(00000000-0000-0000-0000-000000000000)</pre>
        <p>
That's right. Now I remember.
</p>
        <p>
Edit: 1/8/2010 - I've forgotten again... Darn.
</p>
        <p>
I should create a <a href="http://en.wikipedia.org/wiki/Mnemonic">mnemonic device</a> to
remember this
</p>
        <p>
N = Nothing
</p>
        <p>
D = Dashes
</p>
        <p>
B = Brackets
</p>
        <p>
P = Parenthesis
</p>
        <p>
http://msdn.microsoft.com/en-us/system.guid.tostring.aspx<br /></p>
        <p>
          <br />
        </p>
        <img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=e722bd9e-4e5b-40a8-9867-932002dac07b" />
      </body>
      <title>What Was That Format String Again You Know The One For The GUID Where You Only Output The Guid But With No Formatting</title>
      <guid isPermaLink="false">http://www.jeremysimmons.net/blog/PermaLink,guid,e722bd9e-4e5b-40a8-9867-932002dac07b.aspx</guid>
      <link>http://www.jeremysimmons.net/blog/2008/04/22/WhatWasThatFormatStringAgainYouKnowTheOneForTheGUIDWhereYouOnlyOutputTheGuidButWithNoFormatting.aspx</link>
      <pubDate>Tue, 22 Apr 2008 16:03:45 GMT</pubDate>
      <description>&lt;p&gt;
What was that format string again? You know, the one for the GUID where you only output
the guid, but with no formatting?
&lt;/p&gt;
&lt;p&gt;
&lt;span style="font-size: 11px; color: black; font-family: Courier New; background-color: transparent;"&gt;&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"&gt;using&lt;/span&gt; System;&lt;br&gt;
&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"&gt;public&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"&gt;class&lt;/span&gt; GuidTest&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"&gt;public&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"&gt;static&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"&gt;void&lt;/span&gt; Main()&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"&gt;string&lt;/span&gt;[]
guidFormats &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;"&gt;=&lt;/span&gt; {&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);"&gt;"N"&lt;/span&gt;, &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);"&gt;"D"&lt;/span&gt;, &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);"&gt;"B"&lt;/span&gt;, &lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);"&gt;"P"&lt;/span&gt;};&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Guid g &lt;span style="font-size: 11px; color: red; font-family: Courier New; background-color: transparent;"&gt;=&lt;/span&gt; &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"&gt;new&lt;/span&gt; Guid();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Array.ForEach(guidFormats, &lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"&gt;delegate&lt;/span&gt;(&lt;span style="font-size: 11px; color: blue; font-family: Courier New; background-color: transparent;"&gt;string&lt;/span&gt; t)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(&lt;span style="font-size: 11px; color: rgb(102, 102, 102); font-family: Courier New; background-color: rgb(228, 228, 228);"&gt;"{0}:{1}"&lt;/span&gt;,
t, g.ToString(t));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;});&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.ReadLine();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
}&lt;/span&gt;
&lt;/p&gt;
&lt;pre&gt;N:00000000000000000000000000000000
D:00000000-0000-0000-0000-000000000000
B:{00000000-0000-0000-0000-000000000000}
P:(00000000-0000-0000-0000-000000000000)&lt;/pre&gt;
&lt;p&gt;
That's right. Now I remember.
&lt;/p&gt;
&lt;p&gt;
Edit: 1/8/2010 - I've forgotten again... Darn.
&lt;/p&gt;
&lt;p&gt;
I should create a &lt;a href="http://en.wikipedia.org/wiki/Mnemonic"&gt;mnemonic device&lt;/a&gt; to
remember this
&lt;/p&gt;
&lt;p&gt;
N = Nothing
&lt;/p&gt;
&lt;p&gt;
D = Dashes
&lt;/p&gt;
&lt;p&gt;
B = Brackets
&lt;/p&gt;
&lt;p&gt;
P = Parenthesis
&lt;/p&gt;
&lt;p&gt;
http://msdn.microsoft.com/en-us/system.guid.tostring.aspx&lt;br&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;br&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=e722bd9e-4e5b-40a8-9867-932002dac07b" /&gt;</description>
      <comments>http://www.jeremysimmons.net/blog/CommentView,guid,e722bd9e-4e5b-40a8-9867-932002dac07b.aspx</comments>
      <category>All things Microsoft</category>
      <category>Code</category>
    </item>
    <item>
      <trackback:ping>http://www.jeremysimmons.net/blog/Trackback.aspx?guid=2069519e-6de1-46a9-b906-7fd9cd83d11e</trackback:ping>
      <pingback:server>http://www.jeremysimmons.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeremysimmons.net/blog/PermaLink,guid,2069519e-6de1-46a9-b906-7fd9cd83d11e.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.jeremysimmons.net/blog/CommentView,guid,2069519e-6de1-46a9-b906-7fd9cd83d11e.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeremysimmons.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=2069519e-6de1-46a9-b906-7fd9cd83d11e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Sometimes you just need a file with nothing particular in it so you can test a web-page
with a &lt;input type="file"/&gt;
</p>
        <p>
Rather than write a snippet of code, or use a utility, you can use the operating system
to do this.
</p>
        <p>
On WindowsXP, you can use FSUTIL to do this.
</p>
        <p>
FSUTIL FILE CREATENEWFILE &lt;path&gt; &lt;sizeInBytes&gt;
</p>
        <p>
FSUTIL FILE CREATENEWFILE C:\20mbfile.txt 20971520
</p>
        <img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=2069519e-6de1-46a9-b906-7fd9cd83d11e" />
      </body>
      <title>Making blank files of a certain size</title>
      <guid isPermaLink="false">http://www.jeremysimmons.net/blog/PermaLink,guid,2069519e-6de1-46a9-b906-7fd9cd83d11e.aspx</guid>
      <link>http://www.jeremysimmons.net/blog/2008/04/21/MakingBlankFilesOfACertainSize.aspx</link>
      <pubDate>Mon, 21 Apr 2008 16:36:55 GMT</pubDate>
      <description>&lt;p&gt;
Sometimes you just need a file with nothing particular in it so you can test a web-page
with a &amp;lt;input type="file"/&amp;gt;
&lt;/p&gt;
&lt;p&gt;
Rather than write a snippet of code, or use a utility, you can use the operating system
to do this.
&lt;/p&gt;
&lt;p&gt;
On WindowsXP, you can use FSUTIL to do this.
&lt;/p&gt;
&lt;p&gt;
FSUTIL FILE CREATENEWFILE &amp;lt;path&amp;gt; &amp;lt;sizeInBytes&amp;gt;
&lt;/p&gt;
&lt;p&gt;
FSUTIL FILE CREATENEWFILE C:\20mbfile.txt 20971520
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=2069519e-6de1-46a9-b906-7fd9cd83d11e" /&gt;</description>
      <comments>http://www.jeremysimmons.net/blog/CommentView,guid,2069519e-6de1-46a9-b906-7fd9cd83d11e.aspx</comments>
      <category>Code</category>
    </item>
    <item>
      <trackback:ping>http://www.jeremysimmons.net/blog/Trackback.aspx?guid=6e5ad51e-d972-43f2-b4ed-cf07260fe1f0</trackback:ping>
      <pingback:server>http://www.jeremysimmons.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeremysimmons.net/blog/PermaLink,guid,6e5ad51e-d972-43f2-b4ed-cf07260fe1f0.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.jeremysimmons.net/blog/CommentView,guid,6e5ad51e-d972-43f2-b4ed-cf07260fe1f0.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeremysimmons.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=6e5ad51e-d972-43f2-b4ed-cf07260fe1f0</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I want to get a Process ID for the current Application in C#, so which is faster (and
does it really matter).
</p>
        <p>
Lets find out!
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> ProcessIDFaceoff<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Program<br />
{<br />
[System.Runtime.InteropServices.DllImport(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"kernel32.dll"</span>,
SetLastError <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">true</span>)]<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">extern</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> GetCurrentProcessId();<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">static</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Main(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span>[]
args)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
i &lt; 3; i++)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Program().RunTest();<br />
}<br />
}<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> RunTest()<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">const</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> times <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 1000;<br />
System.Console.WriteLine(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"PInvoke,Native"</span>);<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> j <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
j &lt; 20; j++)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> DisposableStopwatch(System.Console.Out))<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
i &lt; times; i++)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> pid <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> GetCurrentProcessId();<br />
}<br />
}<br />
System.Console.Write(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">","</span>);<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> DisposableStopwatch(System.Console.Out))<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">for</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> i <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 0;
i &lt; times; i++)<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> pid <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> System.Diagnostics.Process.GetCurrentProcess().Id;<br />
}<br />
}<br />
System.Console.WriteLine();<br />
}<br />
}<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> DisposableStopwatch
: System.IDisposable<br />
{<br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span> System.Diagnostics.Stopwatch
watch;<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> DisposableStopwatch(System.IO.TextWriter
logger)<br />
{<br />
watch <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> System.Diagnostics.Stopwatch.StartNew();<br />
}<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IDisposable
Members<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Dispose()<br />
{<br />
watch.Stop();<br />
System.Console.Write(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"{0}"</span>,
watch.Elapsed);<br />
}<br /><br /><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span><br />
}<br />
}<br />
}<br /></span>
        </p>
        <p>
Yields something like this.
</p>
        <pre>PInvoke,Native
00:00:00.0003477,00:00:00.0004451
00:00:00.0000705,00:00:00.0003653
00:00:00.0000675,00:00:00.0003550
00:00:00.0000677,00:00:00.0003782
00:00:00.0000679,00:00:00.0003586
00:00:00.0000674,00:00:00.0003772
00:00:00.0000677,00:00:00.0017131
00:00:00.0000706,00:00:00.0004838
00:00:00.0000689,00:00:00.0053451
00:00:00.0000676,00:00:00.0003648
00:00:00.0000677,00:00:00.0003676
00:00:00.0000677,00:00:00.0003659
00:00:00.0000677,00:00:00.0017793
00:00:00.0000766,00:00:00.0004811
00:00:00.0000694,00:00:00.0004966
00:00:00.0000690,00:00:00.0004918
00:00:00.0000704,00:00:00.0003739
00:00:00.0000677,00:00:00.0003609
00:00:00.0000769,00:00:00.0003749
00:00:00.0000677,00:00:00.0034804
PInvoke,Native
00:00:00.0000704,00:00:00.0004326
00:00:00.0000695,00:00:00.0014194
00:00:00.0000770,00:00:00.0002896
00:00:00.0000681,00:00:00.0003052
00:00:00.0000705,00:00:00.0003009
00:00:00.0000711,00:00:00.0003472
00:00:00.0000708,00:00:00.0038629
00:00:00.0000681,00:00:00.0003193
00:00:00.0000678,00:00:00.0003672
00:00:00.0000679,00:00:00.0003150
00:00:00.0000675,00:00:00.0003610
00:00:00.0000679,00:00:00.0003227
00:00:00.0000674,00:00:00.0061228
00:00:00.0000704,00:00:00.0004289
00:00:00.0000701,00:00:00.0004732
00:00:00.0000696,00:00:00.0004107
00:00:00.0000697,00:00:00.0003274
00:00:00.0000678,00:00:00.0003034
00:00:00.0000679,00:00:00.0004169
00:00:00.0000688,00:00:00.0047879
PInvoke,Native
00:00:00.0000703,00:00:00.0004419
00:00:00.0000697,00:00:00.0004428
00:00:00.0000699,00:00:00.0003065
00:00:00.0000678,00:00:00.0003064
00:00:00.0000674,00:00:00.0003073
00:00:00.0000674,00:00:00.0015432
00:00:00.0000694,00:00:00.0004473
00:00:00.0000704,00:00:00.0053116
00:00:00.0000680,00:00:00.0003242
00:00:00.0000698,00:00:00.0003293
00:00:00.0000721,00:00:00.0003163
00:00:00.0000720,00:00:00.0003264
00:00:00.0000716,00:00:00.0039090
00:00:00.0000700,00:00:00.0004544
00:00:00.0000703,00:00:00.0003519
00:00:00.0000678,00:00:00.0003039
00:00:00.0000677,00:00:00.0003300
00:00:00.0000678,00:00:00.0003068
00:00:00.0000677,00:00:00.0070403
00:00:00.0000727,00:00:00.0003814</pre>
        <p>
Looks faster to me. By at least 3x-5x in most cases. That's maybe worth the consideration
to PInvoke it.
</p>
        <img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=6e5ad51e-d972-43f2-b4ed-cf07260fe1f0" />
      </body>
      <title>To PInvoke or To Native code, that is the question.</title>
      <guid isPermaLink="false">http://www.jeremysimmons.net/blog/PermaLink,guid,6e5ad51e-d972-43f2-b4ed-cf07260fe1f0.aspx</guid>
      <link>http://www.jeremysimmons.net/blog/2008/01/23/ToPInvokeOrToNativeCodeThatIsTheQuestion.aspx</link>
      <pubDate>Wed, 23 Jan 2008 23:49:32 GMT</pubDate>
      <description>&lt;p&gt;
I want to get a Process ID for the current Application in C#, so which is faster (and
does it really matter).
&lt;/p&gt;
&lt;p&gt;
Lets find out!
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; ProcessIDFaceoff&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Program&lt;br&gt;
{&lt;br&gt;
[System.Runtime.InteropServices.DllImport(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"kernel32.dll"&lt;/span&gt;,
SetLastError &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;true&lt;/span&gt;)]&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;extern&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; GetCurrentProcessId();&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;static&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Main(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt;[]
args)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
i &amp;lt; 3; i++)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Program().RunTest();&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; RunTest()&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;const&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; times &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 1000;&lt;br&gt;
System.Console.WriteLine(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"PInvoke,Native"&lt;/span&gt;);&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; j &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
j &amp;lt; 20; j++)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; DisposableStopwatch(System.Console.Out))&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
i &amp;lt; times; i++)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; pid &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; GetCurrentProcessId();&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
System.Console.Write(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;","&lt;/span&gt;);&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; DisposableStopwatch(System.Console.Out))&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;for&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; i &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 0;
i &amp;lt; times; i++)&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; pid &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; System.Diagnostics.Process.GetCurrentProcess().Id;&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
System.Console.WriteLine();&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; DisposableStopwatch
: System.IDisposable&lt;br&gt;
{&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; System.Diagnostics.Stopwatch
watch;&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; DisposableStopwatch(System.IO.TextWriter
logger)&lt;br&gt;
{&lt;br&gt;
watch &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; System.Diagnostics.Stopwatch.StartNew();&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IDisposable
Members&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Dispose()&lt;br&gt;
{&lt;br&gt;
watch.Stop();&lt;br&gt;
System.Console.Write(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"{0}"&lt;/span&gt;,
watch.Elapsed);&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt;
&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
Yields something like this.
&lt;/p&gt;
&lt;pre&gt;PInvoke,Native
00:00:00.0003477,00:00:00.0004451
00:00:00.0000705,00:00:00.0003653
00:00:00.0000675,00:00:00.0003550
00:00:00.0000677,00:00:00.0003782
00:00:00.0000679,00:00:00.0003586
00:00:00.0000674,00:00:00.0003772
00:00:00.0000677,00:00:00.0017131
00:00:00.0000706,00:00:00.0004838
00:00:00.0000689,00:00:00.0053451
00:00:00.0000676,00:00:00.0003648
00:00:00.0000677,00:00:00.0003676
00:00:00.0000677,00:00:00.0003659
00:00:00.0000677,00:00:00.0017793
00:00:00.0000766,00:00:00.0004811
00:00:00.0000694,00:00:00.0004966
00:00:00.0000690,00:00:00.0004918
00:00:00.0000704,00:00:00.0003739
00:00:00.0000677,00:00:00.0003609
00:00:00.0000769,00:00:00.0003749
00:00:00.0000677,00:00:00.0034804
PInvoke,Native
00:00:00.0000704,00:00:00.0004326
00:00:00.0000695,00:00:00.0014194
00:00:00.0000770,00:00:00.0002896
00:00:00.0000681,00:00:00.0003052
00:00:00.0000705,00:00:00.0003009
00:00:00.0000711,00:00:00.0003472
00:00:00.0000708,00:00:00.0038629
00:00:00.0000681,00:00:00.0003193
00:00:00.0000678,00:00:00.0003672
00:00:00.0000679,00:00:00.0003150
00:00:00.0000675,00:00:00.0003610
00:00:00.0000679,00:00:00.0003227
00:00:00.0000674,00:00:00.0061228
00:00:00.0000704,00:00:00.0004289
00:00:00.0000701,00:00:00.0004732
00:00:00.0000696,00:00:00.0004107
00:00:00.0000697,00:00:00.0003274
00:00:00.0000678,00:00:00.0003034
00:00:00.0000679,00:00:00.0004169
00:00:00.0000688,00:00:00.0047879
PInvoke,Native
00:00:00.0000703,00:00:00.0004419
00:00:00.0000697,00:00:00.0004428
00:00:00.0000699,00:00:00.0003065
00:00:00.0000678,00:00:00.0003064
00:00:00.0000674,00:00:00.0003073
00:00:00.0000674,00:00:00.0015432
00:00:00.0000694,00:00:00.0004473
00:00:00.0000704,00:00:00.0053116
00:00:00.0000680,00:00:00.0003242
00:00:00.0000698,00:00:00.0003293
00:00:00.0000721,00:00:00.0003163
00:00:00.0000720,00:00:00.0003264
00:00:00.0000716,00:00:00.0039090
00:00:00.0000700,00:00:00.0004544
00:00:00.0000703,00:00:00.0003519
00:00:00.0000678,00:00:00.0003039
00:00:00.0000677,00:00:00.0003300
00:00:00.0000678,00:00:00.0003068
00:00:00.0000677,00:00:00.0070403
00:00:00.0000727,00:00:00.0003814&lt;/pre&gt;
&lt;p&gt;
Looks faster to me. By at least 3x-5x in most cases. That's maybe worth the consideration
to PInvoke it.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=6e5ad51e-d972-43f2-b4ed-cf07260fe1f0" /&gt;</description>
      <comments>http://www.jeremysimmons.net/blog/CommentView,guid,6e5ad51e-d972-43f2-b4ed-cf07260fe1f0.aspx</comments>
      <category>Code</category>
      <category>CSharp</category>
    </item>
    <item>
      <trackback:ping>http://www.jeremysimmons.net/blog/Trackback.aspx?guid=6829bc79-baab-4dcc-a6e6-02c2b034347d</trackback:ping>
      <pingback:server>http://www.jeremysimmons.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeremysimmons.net/blog/PermaLink,guid,6829bc79-baab-4dcc-a6e6-02c2b034347d.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.jeremysimmons.net/blog/CommentView,guid,6829bc79-baab-4dcc-a6e6-02c2b034347d.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeremysimmons.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=6829bc79-baab-4dcc-a6e6-02c2b034347d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
A colleage approached me and asked how he could convert some ASP Classic code to ASP.Net
2.
</p>
        <p>
Here's the snippet
</p>
        <p>
Dim user 'DOMAIN\USERNAME
</p>
        <p>
Const DomainAuthority = "dc.hq.domain.com"
</p>
        <p>
user = Request.ServerVariables("AUTH_USER") ' BASIC Authentication
</p>
        <p>
user = Mid(user, InStr(user, "\") + 1)
</p>
        <p>
Set user = GetObject("WINNT://" &amp; DomainAuthority &amp; "/" &amp; user)
</p>
        <p>
For Each grp in user.Groups
</p>
        <p>
   If grp.Name = "GroupName1" Then GroupFlag1 = True
</p>
        <p>
   If grp.Name = "GroupName2" Then GroupFlag2 = True
</p>
        <p>
Next
</p>
        <p>
This doesn't compile in a VB.Net web-app.
</p>
        <p>
In order to fix this, lets first add a reference to the types we're using.
</p>
        <p>
At the project level in the Solution Explorer, right click and select Add Reference
</p>
        <p>
          <img src="http://www.jeremysimmons.net/blog/content/binary/ADSIWinNTSolutionExplorer.png" border="0" />
        </p>
        <p>
Switch to the COM tab, and Choose the "Active DS Type Library"
</p>
        <p>
          <img src="http://www.jeremysimmons.net/blog/content/binary/ADSIWinNTLibraryBrowser.png" border="0" />
        </p>
        <p>
This will automagically add some things to your project.
</p>
        <p>
          <img src="http://www.jeremysimmons.net/blog/content/binary/ADSIWinNTSolutionExplorerBin.png" border="0" />
        </p>
        <p>
These are the magical COM Interop DLLs that are generated for you, and allow you to
use the objects inside that library. 
<br />
Dim up a few objects, to use a bit later. 
</p>
        <p>
          <code class="vbnet">Dim usr As IADsUser<br />
Dim grp As IADsGroup </code>
        </p>
        <p>
Set a reference to your user the same as always, using the WinNT ADSI provider.
</p>
        <p>
          <code class="vbnet">usr = GetObject("WINNT://" &amp; TheDC &amp; "/" &amp; UsrName) </code>
        </p>
        <p>
          <span class="686220716-24052007">Now that the compiler knows what the grp object's
type is, it won't puke on you</span>
        </p>
        <p>
          <code class="vbnet">For Each grp In usr.Groups()<br />
  If (grp.Name = "foo" Or grp.Name = "bar") Then<br />
    ' Do something with it<br />
  End If<br />
Next<br /></code>
        </p>
        <p>
There are better ways to do this though, Stay tuned for how to totally pimp this out,
the ASP.Net 2.0 way.
</p>
        <img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=6829bc79-baab-4dcc-a6e6-02c2b034347d" />
      </body>
      <title>Group Membership in .net</title>
      <guid isPermaLink="false">http://www.jeremysimmons.net/blog/PermaLink,guid,6829bc79-baab-4dcc-a6e6-02c2b034347d.aspx</guid>
      <link>http://www.jeremysimmons.net/blog/2007/05/24/GroupMembershipInNet.aspx</link>
      <pubDate>Thu, 24 May 2007 17:18:41 GMT</pubDate>
      <description>&lt;p&gt;
A colleage approached me and asked how he could convert some ASP Classic code to ASP.Net
2.
&lt;/p&gt;
&lt;p&gt;
Here's the snippet
&lt;/p&gt;
&lt;p&gt;
Dim user 'DOMAIN\USERNAME
&lt;/p&gt;
&lt;p&gt;
Const DomainAuthority = "dc.hq.domain.com"
&lt;/p&gt;
&lt;p&gt;
user = Request.ServerVariables("AUTH_USER") ' BASIC Authentication
&lt;/p&gt;
&lt;p&gt;
user = Mid(user, InStr(user, "\") + 1)
&lt;/p&gt;
&lt;p&gt;
Set user = GetObject("WINNT://" &amp;amp; DomainAuthority &amp;amp; "/" &amp;amp; user)
&lt;/p&gt;
&lt;p&gt;
For Each grp in user.Groups
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;If grp.Name = "GroupName1" Then GroupFlag1 = True
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;&amp;nbsp; If grp.Name = "GroupName2" Then GroupFlag2 = True
&lt;/p&gt;
&lt;p&gt;
Next
&lt;/p&gt;
&lt;p&gt;
This doesn't compile in a VB.Net web-app.
&lt;/p&gt;
&lt;p&gt;
In order to fix this, lets first add a reference to the types we're using.
&lt;/p&gt;
&lt;p&gt;
At the project level in the Solution Explorer, right click and select Add Reference
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.jeremysimmons.net/blog/content/binary/ADSIWinNTSolutionExplorer.png" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
Switch to the COM tab, and Choose the "Active DS Type Library"
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.jeremysimmons.net/blog/content/binary/ADSIWinNTLibraryBrowser.png" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
This will automagically add some things to your project.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.jeremysimmons.net/blog/content/binary/ADSIWinNTSolutionExplorerBin.png" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
These are the magical COM Interop DLLs that are generated for you, and allow you to
use the objects inside that library. 
&lt;br&gt;
Dim up a few objects, to use a bit later. 
&lt;/p&gt;
&lt;p&gt;
&lt;code class=vbnet&gt;Dim usr As IADsUser&lt;br&gt;
Dim grp As IADsGroup &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Set a reference to your user the same as always, using the WinNT ADSI provider.
&lt;/p&gt;
&lt;p&gt;
&lt;code class=vbnet&gt;usr = GetObject("WINNT://" &amp;amp; TheDC &amp;amp; "/" &amp;amp; UsrName) &lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;span class=686220716-24052007&gt;Now that the compiler knows what the grp object's type
is, it won't puke on you&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;code class=vbnet&gt;For Each grp In usr.Groups()&lt;br&gt;
&amp;nbsp; If (grp.Name = "foo" Or grp.Name = "bar") Then&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ' Do something with it&lt;br&gt;
&amp;nbsp; End If&lt;br&gt;
Next&lt;br&gt;
&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
There are better ways to do this though, Stay tuned for how to totally pimp this out,
the ASP.Net 2.0 way.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=6829bc79-baab-4dcc-a6e6-02c2b034347d" /&gt;</description>
      <comments>http://www.jeremysimmons.net/blog/CommentView,guid,6829bc79-baab-4dcc-a6e6-02c2b034347d.aspx</comments>
      <category>All things Microsoft</category>
      <category>Code</category>
    </item>
    <item>
      <trackback:ping>http://www.jeremysimmons.net/blog/Trackback.aspx?guid=f81aa24e-b895-4478-b6cd-94833cd140bb</trackback:ping>
      <pingback:server>http://www.jeremysimmons.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeremysimmons.net/blog/PermaLink,guid,f81aa24e-b895-4478-b6cd-94833cd140bb.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.jeremysimmons.net/blog/CommentView,guid,f81aa24e-b895-4478-b6cd-94833cd140bb.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeremysimmons.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=f81aa24e-b895-4478-b6cd-94833cd140bb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've had to do this way too many times, and keep forgetting to document how here.
</p>
        <p>
Referencing an article <a href="http://www.activedir.org/article.aspx?aid=110">http://www.activedir.org/article.aspx?aid=110</a>.
It occurred to me that you can impliment the snippet very easily in VBS.
</p>
        <ol>
          <li>
Connect to the of the current workstation rootDSE 
</li>
          <li>
query for all User objects using an indexed attribute to get results as quickly as
possible, also with little load on the server 
</li>
          <li>
get a full IADsUser object using the LDAP ADSI provider 
</li>
          <li>
do something with each user.</li>
        </ol>
        <p>
Here's what I wound up with.
</p>
        <p>
          <font face="Courier New">Option Explicit ' Enough said. Don't write production code
without it.</font>
        </p>
        <p>
          <font face="Courier New">Dim objConnection, objCommand, objRecordSet, objRootDSE,
objUser</font>
        </p>
        <p>
          <font face="Courier New">Dim start, strDomain<br />
start = Now</font>
        </p>
        <p>
          <font face="Courier New">' AD (NTDS) is, afterall, just a service that sits on
top of a </font>
          <br />
          <font face="Courier New">' <a title="Microsoft JET Blue" href="http://en.wikipedia.org/wiki/Microsoft_JET_Blue">JET
Blue-based</a><a title="Extensible Storage Engine" href="http://en.wikipedia.org/wiki/Extensible_Storage_Engine">Extensible
Storage Engine</a> (<a href="http://en.wikipedia.org/wiki/Active_directory">WikiPedia
Active_Directory</a>)</font>
          <br />
          <font face="Courier New">' So why not just treat it as such and use a Database-esque
provider to access the data?</font>
        </p>
        <p>
          <font face="Courier New">Set objConnection = CreateObject("ADODB.Connection")<br />
objConnection.Open "Provider=ADsDSOObject;"</font>
        </p>
        <p>
          <font face="Courier New">Set objCommand = CreateObject("ADODB.Command")<br />
objCommand.ActiveConnection = objConnection</font>
        </p>
        <p>
          <font face="Courier New">' Get Domain name from RootDSE object.<br />
Set objRootDSE = GetObject("</font>
          <a href="ldap://RootDSE">
            <font face="Courier New">LDAP://RootDSE</font>
          </a>
          <font face="Courier New">")<br />
strDomain = objRootDSE.Get("DefaultNamingContext")</font>
        </p>
        <p>
          <font face="Courier New">' One could just as easily do (&amp;(objectClass=User)(objectCategory=Person))</font>
        </p>
        <p>
          <font face="Courier New">' I found this approach using indexed attributes to produce
a speed gain of several magnitudes.</font>
        </p>
        <p>
          <font face="Courier New">objCommand.CommandText = _<br />
"&lt;LDAP://" &amp; strDomain &amp; "&gt;;" &amp; _<br />
 "(&amp;(sAMAccountType=805306368)(!(objectClass=inetOrgPerson)));" &amp; _<br />
 "adspath;subtree"</font>
        </p>
        <p>
          <font face="Courier New">Set objRecordSet = objCommand.Execute</font>
        </p>
        <p>
          <font face="Courier New">While Not objRecordset.EOF</font>
        </p>
        <p>
          <font face="Courier New">  ' Here is an IADsUser object using the ADSI
LDAP provider.</font>
        </p>
        <p>
          <font face="Courier New"> Set objUser = GetObject(objRecordset.Fields("adspath"))<br />
 <br />
 WScript.Echo objUser.name ' Do your work here! woohoo!<br />
 <br />
 objRecordset.MoveNext<br />
 <br />
Wend</font>
        </p>
        <p>
          <font face="Courier New">WScript.Echo "Retrieved (" &amp; objRecordSet.RecordCount
&amp; ") records in (" &amp; DateDiff("s", start, Now) &amp; ") seconds"</font>
        </p>
        <p>
          <font face="Courier New">objConnection.Close<br /></font>
        </p>
        <img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=f81aa24e-b895-4478-b6cd-94833cd140bb" />
      </body>
      <title>User Objects in AD </title>
      <guid isPermaLink="false">http://www.jeremysimmons.net/blog/PermaLink,guid,f81aa24e-b895-4478-b6cd-94833cd140bb.aspx</guid>
      <link>http://www.jeremysimmons.net/blog/2007/05/14/UserObjectsInAD.aspx</link>
      <pubDate>Mon, 14 May 2007 19:33:55 GMT</pubDate>
      <description>&lt;p&gt;
I've had to do this way too many times, and keep forgetting to document how here.
&lt;/p&gt;
&lt;p&gt;
Referencing an article &lt;a href="http://www.activedir.org/article.aspx?aid=110"&gt;http://www.activedir.org/article.aspx?aid=110&lt;/a&gt;.
It occurred to me that you can impliment the snippet very easily in VBS.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Connect to the of the current workstation&amp;nbsp;rootDSE 
&lt;li&gt;
query for all User objects using an indexed attribute to get results as quickly as
possible, also with little load on the server 
&lt;li&gt;
get a full IADsUser object using the LDAP ADSI provider 
&lt;li&gt;
do something with each user.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Here's what I wound up with.
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;Option Explicit ' Enough said. Don't write production code
without it.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;Dim objConnection, objCommand, objRecordSet, objRootDSE,
objUser&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;Dim start, strDomain&lt;br&gt;
start = Now&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;' AD (NTDS)&amp;nbsp;is, afterall, just a service that sits on
top of a &lt;/font&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;' &lt;a title="Microsoft JET Blue" href="http://en.wikipedia.org/wiki/Microsoft_JET_Blue"&gt;JET
Blue-based&lt;/a&gt; &lt;a title="Extensible Storage Engine" href="http://en.wikipedia.org/wiki/Extensible_Storage_Engine"&gt;Extensible
Storage Engine&lt;/a&gt;&amp;nbsp;(&lt;a href="http://en.wikipedia.org/wiki/Active_directory"&gt;WikiPedia
Active_Directory&lt;/a&gt;)&lt;/font&gt;
&lt;br&gt;
&lt;font face="Courier New"&gt;' So why not just&amp;nbsp;treat it as such and use a Database-esque
provider to access the data?&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;Set objConnection = CreateObject("ADODB.Connection")&lt;br&gt;
objConnection.Open "Provider=ADsDSOObject;"&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;Set objCommand = CreateObject("ADODB.Command")&lt;br&gt;
objCommand.ActiveConnection = objConnection&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;' Get Domain name from RootDSE object.&lt;br&gt;
Set objRootDSE = GetObject("&lt;/font&gt;&lt;a href="ldap://RootDSE"&gt;&lt;font face="Courier New"&gt;LDAP://RootDSE&lt;/font&gt;&lt;/a&gt;&lt;font face="Courier New"&gt;")&lt;br&gt;
strDomain = objRootDSE.Get("DefaultNamingContext")&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;' One could just as easily do (&amp;amp;(objectClass=User)(objectCategory=Person))&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;' I found this approach using indexed attributes to produce
a speed gain of several magnitudes.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;objCommand.CommandText = _&lt;br&gt;
"&amp;lt;LDAP://" &amp;amp; strDomain &amp;amp; "&amp;gt;;" &amp;amp; _&lt;br&gt;
&amp;nbsp;"(&amp;amp;(sAMAccountType=805306368)(!(objectClass=inetOrgPerson)));" &amp;amp; _&lt;br&gt;
&amp;nbsp;"adspath;subtree"&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;Set objRecordSet = objCommand.Execute&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;While Not objRecordset.EOF&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;&amp;nbsp; ' Here is&amp;nbsp;an IADsUser object using&amp;nbsp;the ADSI
LDAP provider.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;&amp;nbsp;Set objUser = GetObject(objRecordset.Fields("adspath"))&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;nbsp;WScript.Echo objUser.name ' Do your work here! woohoo!&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;nbsp;objRecordset.MoveNext&lt;br&gt;
&amp;nbsp;&lt;br&gt;
Wend&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;WScript.Echo "Retrieved (" &amp;amp; objRecordSet.RecordCount
&amp;amp; ") records in (" &amp;amp; DateDiff("s", start, Now) &amp;amp; ") seconds"&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New"&gt;objConnection.Close&lt;br&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=f81aa24e-b895-4478-b6cd-94833cd140bb" /&gt;</description>
      <comments>http://www.jeremysimmons.net/blog/CommentView,guid,f81aa24e-b895-4478-b6cd-94833cd140bb.aspx</comments>
      <category>Code</category>
    </item>
    <item>
      <trackback:ping>http://www.jeremysimmons.net/blog/Trackback.aspx?guid=64a1468a-2e7a-4449-bb4b-4db6957699f5</trackback:ping>
      <pingback:server>http://www.jeremysimmons.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeremysimmons.net/blog/PermaLink,guid,64a1468a-2e7a-4449-bb4b-4db6957699f5.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.jeremysimmons.net/blog/CommentView,guid,64a1468a-2e7a-4449-bb4b-4db6957699f5.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeremysimmons.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=64a1468a-2e7a-4449-bb4b-4db6957699f5</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Javascript closures
</p>
        <p>
          <code class="javascript">&lt;SCRIPT type="text/javascript"&gt;<br />
function CreateRandomNumbers(count, max)<br />
{<br />
 var a = new Array();<br />
 for(var i = 0; i &lt; count; i++)<br />
 {<br />
   a.push(Math.floor(Math.random()*max)+1);<br />
 }<br />
 return a;<br />
}</code>
        </p>
        <p>
          <code class="javascript">var smallRandomNumbers = CreateRandomNumbers(20, 20);<br />
var largeRandomNumbers = CreateRandomNumbers(20, 500);</code>
        </p>
        <p>
          <code class="javascript">function makeGreaterThanPredicate(lowerBound) {<br />
    return function(numberToCheck) { 
<br />
    return (numberToCheck &gt; lowerBound) ? true : false;<br />
  };<br />
}</code>
        </p>
        <p>
          <code class="javascript">function filter(sortby, arr)<br />
{<br />
  var l = arr.length;<br />
  var a = new Array();<br />
  
<br />
  for(var i = 0; i &lt; l; i++)<br />
  {<br />
    var currentElement = arr[i];<br />
    if(sortby(currentElement)) a.push(currentElement);<br />
  }<br />
  return a;<br />
}</code>
        </p>
        <p>
          <code class="javascript">function dumpArray(obj)<br />
{<br />
  var l = obj.length;<br />
  document.write('[');<br />
  for(var i = 0; i &lt; l; i++)<br />
  {<br />
    document.write(obj[i]);<br />
    if(i != l-1)<br />
    {<br />
    document.write(',');<br />
    }<br />
  }<br />
 document.write(']');<br />
}<br />
function dump(obj)<br />
{<br />
  var switchToken = typeof(obj);</code>
        </p>
        <p>
          <code class="javascript">  switch (switchToken)<br />
  {<br />
    case 'object':<br />
      dumpArray(obj);<br />
      break;<br />
    default:<br />
      document.writeln("Unknown type:");<br />
      document.writeln(obj.toString());<br />
  }<br />
  document.writeln("&lt;br/&gt;");<br />
}<br />
var greaterThan10 = makeGreaterThanPredicate(10);<br />
var greaterThan100 = makeGreaterThanPredicate(100);<br />
dump(smallRandomNumbers);<br />
dump(filter(greaterThan10, smallRandomNumbers));<br />
dump(largeRandomNumbers);<br />
dump(filter(greaterThan100, largeRandomNumbers));<br />
dump(filter(makeGreaterThanPredicate(250), largeRandomNumbers));<br />
&lt;/SCRIPT&gt;</code>
        </p>
        <img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=64a1468a-2e7a-4449-bb4b-4db6957699f5" />
      </body>
      <title>Javascript Closures</title>
      <guid isPermaLink="false">http://www.jeremysimmons.net/blog/PermaLink,guid,64a1468a-2e7a-4449-bb4b-4db6957699f5.aspx</guid>
      <link>http://www.jeremysimmons.net/blog/2007/05/14/JavascriptClosures.aspx</link>
      <pubDate>Mon, 14 May 2007 14:52:20 GMT</pubDate>
      <description>&lt;p&gt;
Javascript closures
&lt;/p&gt;
&lt;p&gt;
&lt;code class=javascript&gt;&amp;lt;SCRIPT type="text/javascript"&amp;gt;&lt;br&gt;
function CreateRandomNumbers(count, max)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp;var a = new Array();&lt;br&gt;
&amp;nbsp;for(var i = 0; i &amp;lt; count; i++)&lt;br&gt;
&amp;nbsp;{&lt;br&gt;
&amp;nbsp;&amp;nbsp; a.push(Math.floor(Math.random()*max)+1);&lt;br&gt;
&amp;nbsp;}&lt;br&gt;
&amp;nbsp;return a;&lt;br&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;code class=javascript&gt;var smallRandomNumbers = CreateRandomNumbers(20, 20);&lt;br&gt;
var largeRandomNumbers = CreateRandomNumbers(20, 500);&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;code class=javascript&gt;function makeGreaterThanPredicate(lowerBound) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return function(numberToCheck) { 
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return (numberToCheck &amp;gt; lowerBound) ? true : false;&lt;br&gt;
&amp;nbsp; };&lt;br&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;code class=javascript&gt;function filter(sortby, arr)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp; var l = arr.length;&lt;br&gt;
&amp;nbsp; var a = new Array();&lt;br&gt;
&amp;nbsp; 
&lt;br&gt;
&amp;nbsp; for(var i = 0; i &amp;lt; l; i++)&lt;br&gt;
&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; var currentElement = arr[i];&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(sortby(currentElement)) a.push(currentElement);&lt;br&gt;
&amp;nbsp; }&lt;br&gt;
&amp;nbsp; return a;&lt;br&gt;
}&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;code class=javascript&gt;function dumpArray(obj)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp; var l = obj.length;&lt;br&gt;
&amp;nbsp; document.write('[');&lt;br&gt;
&amp;nbsp; for(var i = 0; i &amp;lt; l; i++)&lt;br&gt;
&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; document.write(obj[i]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if(i != l-1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; document.write(',');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;
&amp;nbsp; }&lt;br&gt;
&amp;nbsp;document.write(']');&lt;br&gt;
}&lt;br&gt;
function dump(obj)&lt;br&gt;
{&lt;br&gt;
&amp;nbsp; var switchToken = typeof(obj);&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;code class=javascript&gt;&amp;nbsp; switch (switchToken)&lt;br&gt;
&amp;nbsp; {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; case 'object':&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dumpArray(obj);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; default:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; document.writeln("Unknown type:");&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; document.writeln(obj.toString());&lt;br&gt;
&amp;nbsp; }&lt;br&gt;
&amp;nbsp; document.writeln("&amp;lt;br/&amp;gt;");&lt;br&gt;
}&lt;br&gt;
var greaterThan10 = makeGreaterThanPredicate(10);&lt;br&gt;
var greaterThan100 = makeGreaterThanPredicate(100);&lt;br&gt;
dump(smallRandomNumbers);&lt;br&gt;
dump(filter(greaterThan10, smallRandomNumbers));&lt;br&gt;
dump(largeRandomNumbers);&lt;br&gt;
dump(filter(greaterThan100, largeRandomNumbers));&lt;br&gt;
dump(filter(makeGreaterThanPredicate(250), largeRandomNumbers));&lt;br&gt;
&amp;lt;/SCRIPT&amp;gt;&lt;/code&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=64a1468a-2e7a-4449-bb4b-4db6957699f5" /&gt;</description>
      <comments>http://www.jeremysimmons.net/blog/CommentView,guid,64a1468a-2e7a-4449-bb4b-4db6957699f5.aspx</comments>
      <category>Code</category>
    </item>
    <item>
      <trackback:ping>http://www.jeremysimmons.net/blog/Trackback.aspx?guid=847eb8b9-e23f-4bb4-a7d6-45c23f269356</trackback:ping>
      <pingback:server>http://www.jeremysimmons.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeremysimmons.net/blog/PermaLink,guid,847eb8b9-e23f-4bb4-a7d6-45c23f269356.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.jeremysimmons.net/blog/CommentView,guid,847eb8b9-e23f-4bb4-a7d6-45c23f269356.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeremysimmons.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=847eb8b9-e23f-4bb4-a7d6-45c23f269356</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
More DOS Scripting goodness. This snippet appends some arbitrary location to your
PATH variable, so you can call the executables inside of it.
</p>
        <pre>@ECHO OFF
ECHO %PATH% | FIND /I "C:\Folder1\Folder2\Folder with Spaces\bin\Release"
IF ERRORLEVEL 1 SET PATH=%PATH%;C:\Folder1\Folder2\Folder with Spaces\bin\Release</pre>
        <p>
That, ladies and gentleman, will save you minutes of grief. Isn't that what it's all
about anyway? makes it all worth while.
</p>
        <img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=847eb8b9-e23f-4bb4-a7d6-45c23f269356" />
      </body>
      <title>Append Location to Path Environment Variable</title>
      <guid isPermaLink="false">http://www.jeremysimmons.net/blog/PermaLink,guid,847eb8b9-e23f-4bb4-a7d6-45c23f269356.aspx</guid>
      <link>http://www.jeremysimmons.net/blog/2006/12/18/AppendLocationToPathEnvironmentVariable.aspx</link>
      <pubDate>Mon, 18 Dec 2006 19:43:28 GMT</pubDate>
      <description>&lt;p&gt;
More DOS Scripting goodness. This snippet appends some arbitrary location to your
PATH variable, so you can call the executables inside of it.
&lt;/p&gt;
&lt;pre&gt;@ECHO OFF
ECHO %PATH% | FIND /I "C:\Folder1\Folder2\Folder with Spaces\bin\Release"
IF ERRORLEVEL 1 SET PATH=%PATH%;C:\Folder1\Folder2\Folder with Spaces\bin\Release&lt;/pre&gt;
&lt;p&gt;
That, ladies and gentleman, will save you minutes of grief. Isn't that what it's all
about anyway?&amp;nbsp;makes it all worth while.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=847eb8b9-e23f-4bb4-a7d6-45c23f269356" /&gt;</description>
      <comments>http://www.jeremysimmons.net/blog/CommentView,guid,847eb8b9-e23f-4bb4-a7d6-45c23f269356.aspx</comments>
      <category>All things Microsoft</category>
      <category>Code</category>
    </item>
    <item>
      <trackback:ping>http://www.jeremysimmons.net/blog/Trackback.aspx?guid=432ca58e-08bc-4d22-b933-74b87c827fb7</trackback:ping>
      <pingback:server>http://www.jeremysimmons.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeremysimmons.net/blog/PermaLink,guid,432ca58e-08bc-4d22-b933-74b87c827fb7.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.jeremysimmons.net/blog/CommentView,guid,432ca58e-08bc-4d22-b933-74b87c827fb7.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeremysimmons.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=432ca58e-08bc-4d22-b933-74b87c827fb7</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've found myself using this pattern alot lately. I figured I'd post it for posterity.
</p>
        <p>
          <hr />
        </p>
        <p>
@ECHO OFF
</p>
        <p>
SET NOWD=%DATE%<br />
SET mm=%NOWD:~4,2%<br />
SET dd=%NOWD:~7,2%<br />
SET yyyy=%NOWD:~10,4%
</p>
        <p>
:: ECHO mm=%mm%<br />
:: ECHO dd=%dd%<br />
:: ECHO yyyy=%yyyy%
</p>
        <p>
SET NOWT=%TIME%<br />
SET hour=%TIME:~0,2%<br />
SET min=%TIME:~3,2%<br />
SET sec=%TIME:~6,2%<br />
SET ms=%TIME:~9,2%
</p>
        <p>
:: ECHO NOWT=%NOWT%<br />
:: ECHO hour=%hour%<br />
:: ECHO min=%min%<br />
:: ECHO sec=%sec%<br />
:: ECHO ms=%ms%
</p>
        <p>
SET LOGFILENAME=%yyyy%%mm%%dd%_%hour%%min%%sec%%ms%.txt
</p>
        <p>
:: ECHO %LOGFILENAME%
</p>
        <p>
:: make sure to use a double arrow to append, not 'create'
</p>
        <p>
ECHO %DATE% - %TIME% &gt;&gt; %LOGFILENAME%
</p>
        <p>
ECHO Whatever your program should do &gt;&gt; %LOGFILENAME%
</p>
        <p>
 
</p>
        <img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=432ca58e-08bc-4d22-b933-74b87c827fb7" />
      </body>
      <title>Setting up a filename for with the current time in a batch script</title>
      <guid isPermaLink="false">http://www.jeremysimmons.net/blog/PermaLink,guid,432ca58e-08bc-4d22-b933-74b87c827fb7.aspx</guid>
      <link>http://www.jeremysimmons.net/blog/2006/12/11/SettingUpAFilenameForWithTheCurrentTimeInABatchScript.aspx</link>
      <pubDate>Mon, 11 Dec 2006 20:01:53 GMT</pubDate>
      <description>&lt;p&gt;
I've found myself using this pattern alot lately. I figured I'd post it for posterity.
&lt;/p&gt;
&lt;p&gt;
&lt;hr&gt;
&lt;/p&gt;
&lt;p&gt;
@ECHO OFF
&lt;/p&gt;
&lt;p&gt;
SET NOWD=%DATE%&lt;br&gt;
SET mm=%NOWD:~4,2%&lt;br&gt;
SET dd=%NOWD:~7,2%&lt;br&gt;
SET yyyy=%NOWD:~10,4%
&lt;/p&gt;
&lt;p&gt;
:: ECHO mm=%mm%&lt;br&gt;
:: ECHO dd=%dd%&lt;br&gt;
:: ECHO yyyy=%yyyy%
&lt;/p&gt;
&lt;p&gt;
SET NOWT=%TIME%&lt;br&gt;
SET hour=%TIME:~0,2%&lt;br&gt;
SET min=%TIME:~3,2%&lt;br&gt;
SET sec=%TIME:~6,2%&lt;br&gt;
SET ms=%TIME:~9,2%
&lt;/p&gt;
&lt;p&gt;
:: ECHO NOWT=%NOWT%&lt;br&gt;
:: ECHO hour=%hour%&lt;br&gt;
:: ECHO min=%min%&lt;br&gt;
:: ECHO sec=%sec%&lt;br&gt;
:: ECHO ms=%ms%
&lt;/p&gt;
&lt;p&gt;
SET LOGFILENAME=%yyyy%%mm%%dd%_%hour%%min%%sec%%ms%.txt
&lt;/p&gt;
&lt;p&gt;
:: ECHO %LOGFILENAME%
&lt;/p&gt;
&lt;p&gt;
:: make sure to use a double arrow to append, not 'create'
&lt;/p&gt;
&lt;p&gt;
ECHO %DATE% - %TIME% &amp;gt;&amp;gt; %LOGFILENAME%
&lt;/p&gt;
&lt;p&gt;
ECHO Whatever your program should do &amp;gt;&amp;gt; %LOGFILENAME%
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=432ca58e-08bc-4d22-b933-74b87c827fb7" /&gt;</description>
      <comments>http://www.jeremysimmons.net/blog/CommentView,guid,432ca58e-08bc-4d22-b933-74b87c827fb7.aspx</comments>
      <category>Code</category>
    </item>
    <item>
      <trackback:ping>http://www.jeremysimmons.net/blog/Trackback.aspx?guid=0ba2fcd7-c403-47f5-9cb1-85920f0c4763</trackback:ping>
      <pingback:server>http://www.jeremysimmons.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeremysimmons.net/blog/PermaLink,guid,0ba2fcd7-c403-47f5-9cb1-85920f0c4763.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.jeremysimmons.net/blog/CommentView,guid,0ba2fcd7-c403-47f5-9cb1-85920f0c4763.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeremysimmons.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=0ba2fcd7-c403-47f5-9cb1-85920f0c4763</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Some people (like myself) should learn not to touch a compiler on Monday mornings
(before the third cup of coffee at least)
</p>
        <p>
I don't know why I couldn't remember how to do this, but I feel like a noob.
</p>
        <p>
I was just trying to build a Xml Document in memory with a XmlWriter and a MemoryStream
simple stuff right?
</p>
        <font size="2">
          <p>
          </p>
        </font>
        <font color="#008080" size="2">XmlWriterSettings</font>
        <font size="2"> settings
= </font>
        <font color="#0000ff" size="2">new</font>
        <font size="2">
        </font>
        <font color="#008080" size="2">XmlWriterSettings</font>
        <font size="2">();
<p>
settings.Indent = 
</p></font>
        <font color="#0000ff" size="2">true</font>
        <font size="2">;
<p>
settings.OmitXmlDeclaration = 
</p></font>
        <font color="#0000ff" size="2">false</font>
        <font size="2">; 
<p></p></font>
        <font color="#008080" size="2">MemoryStream</font>
        <font size="2"> strm
= </font>
        <font color="#0000ff" size="2">new</font>
        <font size="2">
        </font>
        <font color="#008080" size="2">MemoryStream</font>
        <font size="2">();
<p></p></font>
        <font color="#0000ff" size="2">using</font>
        <font size="2"> (</font>
        <font color="#008080" size="2">XmlWriter</font>
        <font size="2"> w
= </font>
        <font color="#008080" size="2">XmlWriter</font>
        <font size="2">.Create(strm,
settings)) {</font>
        <p>
          <font size="2">...</font>
        </p>
        <p>
          <font size="2">}
</font>
        </p>
        <p>
Now what if you want to get that back into a string? Say for use in a web-service
query or something. like, I don't know, the SharePoint 2003 Search Web Service.
</p>
        <p>
That conversion back to a string might look something like this.
</p>
        <p>
string s;<br />
System.O.MemoryStream ms;<br />
System.IO.StreamReader sr = new System.IO.StreamReader(ms));<br />
s = sr.ReadToEnd();<br />
sr.Close();<br />
ms.Close(); 
</p>
        <p>
Of course later in the day, I decided to actually download the Research Service SDK,
grab a copy of the QueryPacket Schema I was working against, generated a class against
it with the XSD tool, and subclassed it to make sure I was submitting a valid search
request. All was well with the world again, and I didn't feel so horribly inept for
a Monday morning. Yeah me!
</p>
        <p>
        </p>
        <img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=0ba2fcd7-c403-47f5-9cb1-85920f0c4763" />
      </body>
      <title>Dont touch a compiler on monday</title>
      <guid isPermaLink="false">http://www.jeremysimmons.net/blog/PermaLink,guid,0ba2fcd7-c403-47f5-9cb1-85920f0c4763.aspx</guid>
      <link>http://www.jeremysimmons.net/blog/2006/08/07/DontTouchACompilerOnMonday.aspx</link>
      <pubDate>Mon, 07 Aug 2006 16:11:45 GMT</pubDate>
      <description>&lt;p&gt;
Some people (like myself) should learn not to touch a compiler on Monday mornings
(before the third cup of coffee at least)
&lt;/p&gt;
&lt;p&gt;
I don't know why I couldn't remember how to do this, but I feel like a noob.
&lt;/p&gt;
&lt;p&gt;
I was just trying to build a Xml Document in memory with a XmlWriter and a MemoryStream
simple stuff right?
&lt;/p&gt;
&lt;font size=2&gt; 
&lt;p&gt;
&lt;/font&gt;&lt;font color=#008080 size=2&gt;XmlWriterSettings&lt;/font&gt;&lt;font size=2&gt; settings = &lt;/font&gt;&lt;font color=#0000ff size=2&gt;new&lt;/font&gt;&lt;font size=2&gt; &lt;/font&gt;&lt;font color=#008080 size=2&gt;XmlWriterSettings&lt;/font&gt;&lt;font size=2&gt;();&gt;
&lt;p&gt;
settings.Indent = 
&lt;/font&gt;&lt;font color=#0000ff size=2&gt;true&lt;/font&gt;&lt;font size=2&gt;;&gt;
&lt;p&gt;
settings.OmitXmlDeclaration = 
&lt;/font&gt;&lt;font color=#0000ff size=2&gt;false&lt;/font&gt;&lt;font size=2&gt;; &gt;
&lt;p&gt;
&lt;/font&gt;&lt;font color=#008080 size=2&gt;MemoryStream&lt;/font&gt;&lt;font size=2&gt; strm = &lt;/font&gt;&lt;font color=#0000ff size=2&gt;new&lt;/font&gt;&lt;font size=2&gt; &lt;/font&gt;&lt;font color=#008080 size=2&gt;MemoryStream&lt;/font&gt;&lt;font size=2&gt;();&gt;
&lt;p&gt;
&lt;/font&gt;&lt;font color=#0000ff size=2&gt;using&lt;/font&gt;&lt;font size=2&gt; (&lt;/font&gt;&lt;font color=#008080 size=2&gt;XmlWriter&lt;/font&gt;&lt;font size=2&gt; w
= &lt;/font&gt;&lt;font color=#008080 size=2&gt;XmlWriter&lt;/font&gt;&lt;font size=2&gt;.Create(strm, settings))
{&lt;/font&gt;&gt;
&lt;p&gt;
&lt;font size=2&gt;...&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=2&gt;}
&lt;/p&gt;
&gt; 
&lt;p&gt;
Now what if you want to get that back into a string? Say for use in a web-service
query or something. like, I don't know, the SharePoint 2003 Search Web Service.
&lt;/p&gt;
&lt;p&gt;
That conversion back to a string might look something like this.
&lt;/p&gt;
&lt;p&gt;
string s;&lt;br&gt;
System.O.MemoryStream ms;&lt;br&gt;
System.IO.StreamReader&amp;nbsp;sr = new System.IO.StreamReader(ms));&lt;br&gt;
s&amp;nbsp;= sr.ReadToEnd();&lt;br&gt;
sr.Close();&lt;br&gt;
ms.Close(); 
&lt;/p&gt;
&lt;p&gt;
Of course later in the day, I decided to actually download the Research Service SDK,
grab a copy of the QueryPacket Schema I was working against, generated a class against
it with the XSD tool, and subclassed it to make sure I was submitting a valid search
request. All was well with the world again, and I didn't feel so horribly inept for
a Monday morning. Yeah me!
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=0ba2fcd7-c403-47f5-9cb1-85920f0c4763" /&gt;</description>
      <comments>http://www.jeremysimmons.net/blog/CommentView,guid,0ba2fcd7-c403-47f5-9cb1-85920f0c4763.aspx</comments>
      <category>All things Microsoft</category>
      <category>Code</category>
    </item>
    <item>
      <trackback:ping>http://www.jeremysimmons.net/blog/Trackback.aspx?guid=932ce6f6-499f-49e9-aedd-2331000032cf</trackback:ping>
      <pingback:server>http://www.jeremysimmons.net/blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeremysimmons.net/blog/PermaLink,guid,932ce6f6-499f-49e9-aedd-2331000032cf.aspx</pingback:target>
      <dc:creator />
      <wfw:comment>http://www.jeremysimmons.net/blog/CommentView,guid,932ce6f6-499f-49e9-aedd-2331000032cf.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeremysimmons.net/blog/SyndicationService.asmx/GetEntryCommentsRss?guid=932ce6f6-499f-49e9-aedd-2331000032cf</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I realized I haven't blogged in 5 months. How pathetic is that.
</p>
        <p>
Here's some quick coding goodness. This will insert the current date formatted per
the user's Locale into the div. One of them is IE specific, the other is a more standards-happy
way of doing it. As always, code should be valid XHTML!
</p>
        <p>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">&lt;div
id=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"CurrentDate"</span>&gt;&lt;/div&gt;<br />
&lt;hr /&gt;<br />
&lt;div id=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"CurrentDate2"</span>&gt;&lt;/div&gt;<br /><br />
&lt;script type=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"text/vbscript"</span> language=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"vbscript"</span>&gt;<br />
&lt;!-- 
<br />
&lt;![CDATA[<br />
    Document.all(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"CurrentDate"</span>).innerText <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> FormatDateTime(Now(),1)<br />
]]&gt;<br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//--&gt;</span><br />
&lt;/script&gt;<br /><br />
&lt;script type=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"text/javascript"</span> language=<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"javascript"</span>&gt;<br />
&lt;!-- 
<br />
&lt;![CDATA[<br />
    var now <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Date();<br />
    now.getDate();<br />
    document.getElementById(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"CurrentDate2"</span>).innerText <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> now.toLocaleDateString();<br /><br />
]]&gt;<br /><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">//--&gt;</span><br />
&lt;/script&gt;<br /></span>
        </p>
        <img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=932ce6f6-499f-49e9-aedd-2331000032cf" />
      </body>
      <title>Sharepoint - Current Date</title>
      <guid isPermaLink="false">http://www.jeremysimmons.net/blog/PermaLink,guid,932ce6f6-499f-49e9-aedd-2331000032cf.aspx</guid>
      <link>http://www.jeremysimmons.net/blog/2006/05/09/SharepointCurrentDate.aspx</link>
      <pubDate>Tue, 09 May 2006 15:20:34 GMT</pubDate>
      <description>&lt;p&gt;
I realized I haven't blogged in 5 months. How pathetic is that.
&lt;/p&gt;
&lt;p&gt;
Here's some quick coding goodness. This will insert the current date formatted per
the user's Locale into the div. One of them is IE specific, the other is a more standards-happy
way of doing it. As always, code should be valid XHTML!
&lt;/p&gt;
&lt;p&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&amp;lt;div
id=&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"CurrentDate"&lt;/span&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;br&gt;
&amp;lt;hr /&amp;gt;&lt;br&gt;
&amp;lt;div id=&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"CurrentDate2"&lt;/span&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;br&gt;
&lt;br&gt;
&amp;lt;script type=&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"text/vbscript"&lt;/span&gt; language=&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"vbscript"&lt;/span&gt;&amp;gt;&lt;br&gt;
&amp;lt;!-- 
&lt;br&gt;
&amp;lt;![CDATA[&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Document.all(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"CurrentDate"&lt;/span&gt;).innerText &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; FormatDateTime(Now(),1)&lt;br&gt;
]]&amp;gt;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//--&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;lt;/script&amp;gt;&lt;br&gt;
&lt;br&gt;
&amp;lt;script type=&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"text/javascript"&lt;/span&gt; language=&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"javascript"&lt;/span&gt;&amp;gt;&lt;br&gt;
&amp;lt;!-- 
&lt;br&gt;
&amp;lt;![CDATA[&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;var now &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Date();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;now.getDate();&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;document.getElementById(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"CurrentDate2"&lt;/span&gt;).innerText &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; now.toLocaleDateString();&lt;br&gt;
&lt;br&gt;
]]&amp;gt;&lt;br&gt;
&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;//--&amp;gt;&lt;/span&gt;
&lt;br&gt;
&amp;lt;/script&amp;gt;&lt;br&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeremysimmons.net/blog/aggbug.ashx?id=932ce6f6-499f-49e9-aedd-2331000032cf" /&gt;</description>
      <comments>http://www.jeremysimmons.net/blog/CommentView,guid,932ce6f6-499f-49e9-aedd-2331000032cf.aspx</comments>
      <category>Code</category>
    </item>
  </channel>
</rss>