<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">You had me at "Hello World"</title><subtitle type="html">Programming, Love at First Sight</subtitle><id>http://blogs.msdn.com/helloworld/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/helloworld/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2008-10-31T06:03:41Z</updated><entry><title>Attaching an Event Handler</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2009/09/30/attaching-an-event-handler.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2009/09/30/attaching-an-event-handler.aspx</id><published>2009-10-01T01:25:00Z</published><updated>2009-10-01T01:25:00Z</updated><content type="html">&lt;p&gt;Whenever I need to attach an event handler, usually I use one of these methods, a delegate, anonymous method, or lambda expression.&lt;/p&gt;  &lt;p&gt;For complex events (more than 3 lines of code), I create a separate method. If the method is short, I use anonymous method. But if the event is just one line, I use lambda expression.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private void &lt;/span&gt;button1_Click(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;e)
{
    &lt;span style="color: #2b91af"&gt;MessageBox&lt;/span&gt;.Show(&lt;span style="color: #a31515"&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;);
}

&lt;span style="color: blue"&gt;private void &lt;/span&gt;Form1_Load(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;e)
{
    &lt;span style="color: green"&gt;//This is good for complex events.
    &lt;/span&gt;button1.Click += button1_Click;
    &lt;span style="color: green"&gt;//Anonymous method, suitable for short event.
    &lt;/span&gt;button2.Click += &lt;span style="color: blue"&gt;delegate&lt;/span&gt;(&lt;span style="color: blue"&gt;object &lt;/span&gt;obj, &lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;arg)
    {
        &lt;span style="color: #2b91af"&gt;MessageBox&lt;/span&gt;.Show(&lt;span style="color: #a31515"&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;);
    };
    &lt;span style="color: green"&gt;//Lambda expression, great for one line event.
    &lt;/span&gt;button3.Click += ((x, y) =&amp;gt; &lt;span style="color: #2b91af"&gt;MessageBox&lt;/span&gt;.Show(&lt;span style="color: #a31515"&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;));
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9901549" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author><category term=".Net Framework" scheme="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx" /></entry><entry><title>How to Give Authenticated Users or Everyone Access to Your Share Programmatically</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2009/07/13/how-to-give-authenticated-users-or-everyone-access-to-your-share-programmatically.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2009/07/13/how-to-give-authenticated-users-or-everyone-access-to-your-share-programmatically.aspx</id><published>2009-07-13T19:41:40Z</published><updated>2009-07-13T19:41:40Z</updated><content type="html">&lt;p&gt;Another follow-up from my previous article, &lt;a href="http://blogs.msdn.com/helloworld/archive/2008/06/06/programmatically-configuring-permissions-on-a-share-in-c.aspx"&gt;Programmatically Configuring Permissions on a Share&lt;/a&gt;, David B asked a question, how to share a folder to Everyone, instead of to a specific users. This article will answer that question, based on the code on my previous article.&lt;/p&gt;  &lt;p&gt;That is an interesting question, since ‘Everyone’ can be replaced with ‘Authenticated Users’, ‘Network Service’, etc.&lt;/p&gt;  &lt;p&gt;First, if you need only to give Everyone read-only access permission, the easiest thing is to set the DACL property of Win32_SecurityDescriptor to null. This is not equal with an array of null. An array of null will lock everyone out from this share.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;ManagementObject &lt;/span&gt;secDescriptor = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ManagementClass&lt;/span&gt;(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ManagementPath&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;Win32_SecurityDescriptor&amp;quot;&lt;/span&gt;), &lt;span style="color: blue"&gt;null&lt;/span&gt;);
secDescriptor[&lt;span style="color: #a31515"&gt;&amp;quot;ControlFlags&amp;quot;&lt;/span&gt;] = 4; &lt;span style="color: green"&gt;//SE_DACL_PRESENT 
&lt;/span&gt;secDescriptor[&lt;span style="color: #a31515"&gt;&amp;quot;DACL&amp;quot;&lt;/span&gt;] = &lt;span style="color: blue"&gt;null&lt;/span&gt;; &lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If you need to be more explicit, or you need to assign other security principal different access, that method above will not work. As soon as you assign someone access to the share, ‘Everyone’ will lose its read access.&lt;/p&gt;

&lt;p&gt;To assign the permission explicitly, the key is to form the correct Win32_Trustee to represent that special account (Network Service, Everyone, Authenticated Users, etc.). Take a look at &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.principal.genericprincipal.aspx"&gt;System.Security.Principal.WellKnownSidType&lt;/a&gt; enum. It has a number of well known sid that you might be interested with.&lt;/p&gt;

&lt;p&gt;What needs to be done is to assign the SID property of the Win32_Trustee object with the security identifier derived from the well known sid.&lt;/p&gt;

&lt;p&gt;Let assume you have this method:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private byte&lt;/span&gt;[] GetWellKnwonSid(&lt;span style="color: #2b91af"&gt;WellKnownSidType &lt;/span&gt;SidType)
{
    &lt;span style="color: #2b91af"&gt;SecurityIdentifier &lt;/span&gt;Result = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SecurityIdentifier&lt;/span&gt;(SidType, &lt;span style="color: blue"&gt;null&lt;/span&gt;);
    &lt;span style="color: blue"&gt;byte&lt;/span&gt;[] sidArray = &lt;span style="color: blue"&gt;new byte&lt;/span&gt;[Result.BinaryLength];
    Result.GetBinaryForm(sidArray, 0);

    &lt;span style="color: blue"&gt;return &lt;/span&gt;sidArray;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Then when Win32_Trustee object is created, assign the SID property as follow:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;ManagementObject &lt;/span&gt;Trustee = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ManagementClass&lt;/span&gt;(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ManagementPath&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;Win32_Trustee&amp;quot;&lt;/span&gt;), &lt;span style="color: blue"&gt;null&lt;/span&gt;);
Trustee[&lt;span style="color: #a31515"&gt;&amp;quot;SID&amp;quot;&lt;/span&gt;] = GetWellKnwonSid(&lt;span style="color: #2b91af"&gt;WellKnownSidType&lt;/span&gt;.WorldSid); &lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That code above will create Win32_Trustee for ‘Everyone’. Use this Win32_Trustee to form the Win32_Ace, and you now explicitly assign ‘Everyone’ access to your share.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9831920" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author><category term="Programming" scheme="http://blogs.msdn.com/helloworld/archive/tags/Programming/default.aspx" /><category term=".Net Framework" scheme="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx" /><category term="WMI" scheme="http://blogs.msdn.com/helloworld/archive/tags/WMI/default.aspx" /></entry><entry><title>How to Programmatically Modify the IIS Virtual Directory</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2009/05/29/how-to-programmatically-modify-the-iis-virtual-directory.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2009/05/29/how-to-programmatically-modify-the-iis-virtual-directory.aspx</id><published>2009-05-29T19:16:45Z</published><updated>2009-05-29T19:16:45Z</updated><content type="html">&lt;p&gt;If you have more than a dozen web servers in a cluster with identical setting, and you need to update the virtual directory, it is much more convenient to write a simple program to update it. This example shows how to do it. In this case, this snippet will change the physical path of a virtual directory.&lt;/p&gt;  &lt;p&gt;This snippet works on IIS 6 and IIS 7. PowerShell may not be installed in Server 2003, to make this snippet more generic, this snippet is written in C#, otherwise, PowerShell script may be a better option.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private const string &lt;/span&gt;MetabasePath = &lt;span style="color: #a31515"&gt;&amp;quot;IIS://Localhost/W3SVC/1/Root&amp;quot;&lt;/span&gt;;

&lt;span style="color: green"&gt;//Get the virtual directory of IIS.
&lt;/span&gt;&lt;span style="color: blue"&gt;private static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DirectoryEntry &lt;/span&gt;GetVirtualDirectory(&lt;span style="color: #2b91af"&gt;DirectoryEntry &lt;/span&gt;IISDirectory, &lt;span style="color: blue"&gt;string &lt;/span&gt;vDirName)
{
    &lt;span style="color: blue"&gt;string &lt;/span&gt;ClassName = IISDirectory.SchemaClassName.ToString();

    &lt;span style="color: blue"&gt;if &lt;/span&gt;(!((ClassName.EndsWith(&lt;span style="color: #a31515"&gt;&amp;quot;Server&amp;quot;&lt;/span&gt;)) || (ClassName.EndsWith(&lt;span style="color: #a31515"&gt;&amp;quot;VirtualDir&amp;quot;&lt;/span&gt;))))
    {
        &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;&amp;quot;Virtual Directory can be updated under site or virual directory node. Metabase:{0}, SchemaClassName={1}&amp;quot;&lt;/span&gt;, MetabasePath, ClassName));
    }

    &lt;span style="color: green"&gt;//Iterate through the children, as Find() method is having an issue with IIS 6, if more than one thread is accessing IIS, an AccessViolationException is thrown.
    //The fix is applying the a hot fix http://support.microsoft.com/kb/946517. Either use Find(), or use this method below.
    //Since IIS may not have the hot fix installed, this sample iterates the children.
    &lt;/span&gt;&lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;DirectoryEntry &lt;/span&gt;VirtualDir &lt;span style="color: blue"&gt;in &lt;/span&gt;IISDirectory.Children)
        &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;.Compare(VirtualDir.Name, vDirName, &lt;span style="color: #2b91af"&gt;StringComparison&lt;/span&gt;.OrdinalIgnoreCase) == 0)
            &lt;span style="color: blue"&gt;return &lt;/span&gt;VirtualDir;

    &lt;span style="color: blue"&gt;return null&lt;/span&gt;;
}

&lt;span style="color: green"&gt;//Update the physical path of the site.
&lt;/span&gt;&lt;span style="color: blue"&gt;private static void &lt;/span&gt;UpdatePhysicalPath(&lt;span style="color: blue"&gt;string &lt;/span&gt;virtualDirectoryName, &lt;span style="color: blue"&gt;string &lt;/span&gt;newPath)
{
    &lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;DirectoryEntry &lt;/span&gt;Site = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DirectoryEntry&lt;/span&gt;(MetabasePath))
    {
        &lt;span style="color: #2b91af"&gt;DirectoryEntry &lt;/span&gt;StatsNet = GetVirtualDirectory(Site, virtualDirectoryName);

        &lt;span style="color: blue"&gt;if &lt;/span&gt;(StatsNet == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
            &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Exception&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;&amp;quot;Site not found. Metabase={0}, virtualDirectory={1}&amp;quot;&lt;/span&gt;, MetabasePath, virtualDirectoryName));

        StatsNet.Properties[&lt;span style="color: #a31515"&gt;&amp;quot;Path&amp;quot;&lt;/span&gt;][0] = newPath;
        StatsNet.CommitChanges();
    }
}

&lt;span style="color: blue"&gt;static void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)
{
    UpdatePhysicalPath(&lt;span style="color: #a31515"&gt;&amp;quot;MySite&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;@&amp;quot;d:\http\MyNewSite&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;That snippet above shows how to change the path, there are a lot of properties that can be changed, at least this shows the basic idea. 

&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9653837" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author><category term="Programming" scheme="http://blogs.msdn.com/helloworld/archive/tags/Programming/default.aspx" /><category term=".Net Framework" scheme="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx" /><category term="How to" scheme="http://blogs.msdn.com/helloworld/archive/tags/How+to/default.aspx" /></entry><entry><title>Concatenating A List of Elements into A String, From [A] [B] [C] into "A, B, C"</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2009/05/12/concatenating-a-list-of-elements-into-a-string.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2009/05/12/concatenating-a-list-of-elements-into-a-string.aspx</id><published>2009-05-12T07:19:58Z</published><updated>2009-05-12T07:19:58Z</updated><content type="html">&lt;p&gt;This is a quite common problem, there is a list of strings, and you need to concatenate them with a delimiter, let says a comma. There are several different approaches to this problem. &lt;/p&gt;  &lt;p&gt;Before .Net era, I use for loop, and check if the loop counter is not pointing to the last element in the list. If it is not pointing to the last element, add the delimiter. This approach has one problem, if the number of elements are not known. The other way is by concatenating the list and the delimiter, and then truncate the last delimiter after assembling the whole list.&lt;/p&gt;  &lt;p&gt;After .Net becoming more popular, the framework utilizes iterator pattern. Using foreach is much more convenient way to iterate and also to concatenate a list. The solution I had before is much more problematic, as I do not know if I am reading at the last element.&lt;/p&gt;  &lt;p&gt;Currently, I have two favorite methods to concatenate a list of elements into a single string with delimiter in between.&lt;/p&gt;  &lt;p&gt;The first one is using LINQ.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public string &lt;/span&gt;Concatenate(&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; list, &lt;span style="color: blue"&gt;string &lt;/span&gt;delimiter)
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;list.Aggregate((First, Second) =&amp;gt; First + delimiter + Second);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That method above, probably does not have the greatest performance, as it will create too many strings, especially if the list has a lot of elements. The second one is using extension method, this code has better performance.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ConcatExtension
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public static string &lt;/span&gt;Concatenate&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; list, &lt;span style="color: blue"&gt;string &lt;/span&gt;delimiter) 
    {
        &lt;span style="color: #2b91af"&gt;StringBuilder &lt;/span&gt;Builder = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;StringBuilder&lt;/span&gt;();
        
        &lt;span style="color: blue"&gt;using &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IEnumerator&lt;/span&gt;&amp;lt;T&amp;gt; Reader = list.GetEnumerator())
        {
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(Reader.MoveNext()) Builder.Append(Reader.Current.ToString());
            &lt;span style="color: blue"&gt;while &lt;/span&gt;(Reader.MoveNext()) Builder.Append(delimiter).Append(Reader.Current.ToString());
        }

        &lt;span style="color: blue"&gt;return &lt;/span&gt;Builder.ToString();
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now let see, the second approach uses StringBuilder, thus reducing the memory pressure. The idea is not to add delimiter after processing the element, but to add the delimiter &lt;em&gt;before&lt;/em&gt; every element, except the first one.&lt;/p&gt;

&lt;p&gt;If I have these array of values:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; ListOfValues = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;()  { &lt;span style="color: #a31515"&gt;&amp;quot;Orange&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Apple&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Watermelon&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Grape&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Peach&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;Banana&amp;quot; &lt;/span&gt;};&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Then these two calls will give you identical return.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;string &lt;/span&gt;s1 = Concatenate(ListOfValues, &lt;span style="color: #a31515"&gt;&amp;quot;, &amp;quot;&lt;/span&gt;);
&lt;span style="color: blue"&gt;string &lt;/span&gt;s2 = ListOfValues.Concatenate(&lt;span style="color: #a31515"&gt;&amp;quot;, &amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The results will be:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Orange, Apple, Watermelon, Grape, Peach, Banana.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So far, these are the simplest solution to concatenate the elements inside a collection into a delimited string. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9605956" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author></entry><entry><title>Generating P/Invoke Signature</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2009/04/15/generating-p-invoke-signature.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2009/04/15/generating-p-invoke-signature.aspx</id><published>2009-04-15T20:12:56Z</published><updated>2009-04-15T20:12:56Z</updated><content type="html">&lt;p&gt;I have been using &lt;a href="http://pinvoke.net"&gt;PInvoke.Net&lt;/a&gt; for long time. I discovered a tool, P/Invoke Interop Assistant. It is very cool. Not only it can gives you the managed wrapper around the native Windows API, it also gives you the ability to generate the unmanaged signature for your managed assemblies. The best thing, the source code is available.&lt;/p&gt;  &lt;p&gt;Get P/Invoke Interop Assistant &lt;a href="http://www.codeplex.com/clrinterop/release/projectreleases.aspx?ReleaseId=14120"&gt;here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/helloworld/WindowsLiveWriter/GeneratingPInvokeSignature_8FA5/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/helloworld/WindowsLiveWriter/GeneratingPInvokeSignature_8FA5/image_thumb.png" width="621" height="397" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9551118" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author></entry><entry><title>Workaround to deserialize ‘True’, ‘False’ using XmlSerializer</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2009/04/03/workaround-to-deserialize-true-false-using-xmlserializer.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2009/04/03/workaround-to-deserialize-true-false-using-xmlserializer.aspx</id><published>2009-04-03T18:11:00Z</published><updated>2009-04-03T18:11:00Z</updated><content type="html">&lt;p&gt;This is the scenario, system A is taking an xml from system B. System A will deserialize the xml into an object. Unfortunately, a dev in system B decides that System.Xml namespace is evil and construct the xml using StringBuilder. In one of the element, the dev call ToString() method of a boolean variable. The result? the xml contains ‘True’ or ‘False’. System A takes the input and it throws exceptions with error message “The string 'False' is not a valid Boolean value”, or “The string 'True' is not a valid Boolean value'”.&lt;/p&gt;  &lt;p&gt;W3C defines boolean data type as ‘true’, ‘false’, ‘0’, and ‘1’. So this behavior is expected. &lt;/p&gt;  &lt;p&gt;There is a way to workaround this, only under this scenario, you want to keep the type safety of your object, and you have the code. The steps are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Decorate the boolean property with XmlIgnore. &lt;/li&gt;    &lt;li&gt;Inherit your class and add a string property, decorate it with XmlElement. Set the element name to match the xml node name.&lt;/li&gt;    &lt;li&gt;Add code on the new property on the descendant class, to make the property in the descendant class work as an adapter for the actual boolean property.&lt;/li&gt; &lt;/ul&gt;  &lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;XmlRoot&lt;/span&gt;(ElementName = &lt;span style="color: #a31515"&gt;&amp;quot;data&amp;quot;&lt;/span&gt;, Namespace = &lt;span style="color: #a31515"&gt;&amp;quot;http://contoso.com/&amp;quot;&lt;/span&gt;)]
[&lt;span style="color: #2b91af"&gt;XmlInclude&lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;MyClassXmlAdapter&lt;/span&gt;))]
&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyClass
&lt;/span&gt;{
    [&lt;span style="color: #2b91af"&gt;XmlIgnore&lt;/span&gt;]
    &lt;span style="color: blue"&gt;public bool &lt;/span&gt;BooleanField { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}

[&lt;span style="color: #2b91af"&gt;XmlRoot&lt;/span&gt;(ElementName = &lt;span style="color: #a31515"&gt;&amp;quot;data&amp;quot;&lt;/span&gt;, Namespace = &lt;span style="color: #a31515"&gt;&amp;quot;http://contoso.com/&amp;quot;&lt;/span&gt;)]
&lt;span style="color: blue"&gt;public sealed class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyClassXmlAdapter &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;MyClass
&lt;/span&gt;{
    [&lt;span style="color: #2b91af"&gt;XmlElement&lt;/span&gt;(ElementName = &lt;span style="color: #a31515"&gt;&amp;quot;Test&amp;quot;&lt;/span&gt;)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;BooleanAsString
    {
        &lt;span style="color: blue"&gt;get
        &lt;/span&gt;{
            &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;XmlConvert&lt;/span&gt;.ToString(BooleanField);
        }

        &lt;span style="color: blue"&gt;set
        &lt;/span&gt;{
            &lt;span style="color: blue"&gt;bool &lt;/span&gt;ParsedValue;

            &lt;span style="color: blue"&gt;if &lt;/span&gt;(!&lt;span style="color: #2b91af"&gt;Boolean&lt;/span&gt;.TryParse(&lt;span style="color: blue"&gt;value&lt;/span&gt;, &lt;span style="color: blue"&gt;out &lt;/span&gt;ParsedValue))
                ParsedValue = &lt;span style="color: #2b91af"&gt;XmlConvert&lt;/span&gt;.ToBoolean(&lt;span style="color: blue"&gt;value&lt;/span&gt;);

            BooleanField = ParsedValue;
        }
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;In your serialization code, create the XmlSerializer using MyClassXmlAdapter as type, and the serializer will serialize and deserialize to MyClass with no problem. All of your code that works with MyClass won’t have to be changed.&lt;/p&gt;

&lt;p&gt;If you don’t have access to the code, or the class is sealed, it is much more difficult. The only way to do it is by using a helper class. In this sample below, MyClass is sealed and you have only the assembly. Then the solution is to create another class that have almost identical fields with MyClass, with the exception the boolean field.&lt;/p&gt;

&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;XmlRoot&lt;/span&gt;(ElementName = &lt;span style="color: #a31515"&gt;&amp;quot;data&amp;quot;&lt;/span&gt;, Namespace = &lt;span style="color: #a31515"&gt;&amp;quot;http://contoso.com/&amp;quot;&lt;/span&gt;)]
&lt;span style="color: blue"&gt;public sealed class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyClass
&lt;/span&gt;{
    [&lt;span style="color: #2b91af"&gt;XmlElement&lt;/span&gt;]
    &lt;span style="color: blue"&gt;public bool &lt;/span&gt;BooleanField { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
}

[&lt;span style="color: #2b91af"&gt;XmlRoot&lt;/span&gt;(ElementName = &lt;span style="color: #a31515"&gt;&amp;quot;data&amp;quot;&lt;/span&gt;, Namespace = &lt;span style="color: #a31515"&gt;&amp;quot;http://contoso.com/&amp;quot;&lt;/span&gt;)]
&lt;span style="color: blue"&gt;public sealed class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyClassXmlAdapter
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;private bool &lt;/span&gt;_BooleanField;

    [&lt;span style="color: #2b91af"&gt;XmlElement&lt;/span&gt;(ElementName = &lt;span style="color: #a31515"&gt;&amp;quot;Test&amp;quot;&lt;/span&gt;)]
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;BooleanAsString
    {
        &lt;span style="color: blue"&gt;get
        &lt;/span&gt;{
            &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;XmlConvert&lt;/span&gt;.ToString(_BooleanField);
        }

        &lt;span style="color: blue"&gt;set
        &lt;/span&gt;{
            &lt;span style="color: blue"&gt;bool &lt;/span&gt;ParsedValue;

            &lt;span style="color: blue"&gt;if &lt;/span&gt;(!&lt;span style="color: #2b91af"&gt;Boolean&lt;/span&gt;.TryParse(&lt;span style="color: blue"&gt;value&lt;/span&gt;, &lt;span style="color: blue"&gt;out &lt;/span&gt;ParsedValue))
                ParsedValue = &lt;span style="color: #2b91af"&gt;XmlConvert&lt;/span&gt;.ToBoolean(&lt;span style="color: blue"&gt;value&lt;/span&gt;);

            _BooleanField = ParsedValue;
        }
    }

    &lt;span style="color: blue"&gt;public void &lt;/span&gt;CopyTo(&lt;span style="color: #2b91af"&gt;MyClass &lt;/span&gt;target)
    {
        target.BooleanField = &lt;span style="color: blue"&gt;this&lt;/span&gt;.BooleanField;
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;After deserializing the xml with ‘True’ or ‘False’, call CopyTo().&lt;/p&gt;

&lt;p&gt;These solutions are not ideal, but at least, these are better options rather than cleaning up the Xml.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9531053" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author></entry><entry><title>C# 3.0 Automatic Property</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2009/02/12/c-3-0-automatic-property.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2009/02/12/c-3-0-automatic-property.aspx</id><published>2009-02-13T02:30:06Z</published><updated>2009-02-13T02:30:06Z</updated><content type="html">&lt;p&gt;C# introduces a number of syntactic sugar, including Automatic Property &lt;a href="http://msdn.microsoft.com/en-us/library/bb384054.aspx"&gt;link&lt;/a&gt;. A question was asked, why is this useful, what is the advantage of this approach compared to exposing the field as public field?&lt;/p&gt;  &lt;p&gt;The advantage of the automatic property is that while you have a much cleaner code, you still have encapsulation, you can restrict the setter, so only internal classes, or descendant classes can update the property.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;AutomaticPropertyClass
&lt;/span&gt;{
    &lt;span style="color: blue"&gt;public string &lt;/span&gt;PublicProperty { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public int    &lt;/span&gt;ReadOnlyProperty { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public double &lt;/span&gt;InternalProperty { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;internal set&lt;/span&gt;; }
    &lt;span style="color: blue"&gt;public char   &lt;/span&gt;ProtectedProperty { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;protected set&lt;/span&gt;; }
}&lt;/pre&gt;

&lt;p&gt;&lt;font face="Verdana"&gt;That code is much simpler than having to declare private field and then the property, and the developer still can define the accessibility. Yes, this is quite old, but since I was asked that question, might as well blog about it. :)&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9416231" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author><category term="Programming" scheme="http://blogs.msdn.com/helloworld/archive/tags/Programming/default.aspx" /><category term=".Net Framework" scheme="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx" /></entry><entry><title>How to Add Command Line Support with Your .MSI</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2009/02/06/how-to-add-command-line-support-with-your-msi.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2009/02/06/how-to-add-command-line-support-with-your-msi.aspx</id><published>2009-02-06T21:23:59Z</published><updated>2009-02-06T21:23:59Z</updated><content type="html">&lt;p&gt;One of the requirements in building setups was to allow the Operations team to execute the .msi file from the command line. GUI is not for expert! :)&lt;/p&gt;  &lt;p&gt;If you are familiar with Visual Studio setup, each of the UI element has a property, and you pass this property to your custom action, or use it as condition in your setup (whether to install the documentation, help, or whether to install to the gac or not, etc.).&lt;/p&gt;  &lt;p&gt;msiexec.exe allows you to pass some parameters to override those properties. Unfortunately the .msi file that Visual Studio generates always set the default values, default value being the values that the developers set during design time.&lt;/p&gt;  &lt;p&gt;After inspecting the .msi, there are some custom actions that will set the property value to the default. This custom actions are always executed, whether the users sets the property via command line parameter or not. That means, if the user pass some parameters to override the property, the setup will execute the custom action, reassign the property value to the original default value.&lt;/p&gt;  &lt;p&gt;So what needs to be done is to prevent the custom action from executing when the property is overridden by the user. This can be done quite easily with text box dialogs, checkbox dialogs are a little bit more difficult, I will cover it in other post.&lt;/p&gt;  &lt;p&gt;The property has blank value/null in the beginning, if the user overrides the property via command line parameter, this value will not be null. By giving custom actions a condition, to execute only if the property is blank, the value that the user gave via command line will not be overridden.&lt;/p&gt;  &lt;p&gt;You need to set the conditions in two different tables in your .msi file, InstallExecuteSequence and InstallUISequence. One to support GUI mode (interactive) and the other to support quiet/passive mode.&lt;/p&gt;  &lt;p&gt;This is the example on how to do it, repeat the steps for every property that you want to be able to override:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Identify the property that you want to override. From within Visual Studio, writes down the property that you want to be able to override. For example, you set a property &lt;strong&gt;SERVERNAME&lt;/strong&gt;, related to &lt;strong&gt;Edit1Property&lt;/strong&gt; on &lt;strong&gt;Textboxes (A)&lt;/strong&gt;, and the default value is 'DefaultServerName'.      &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/helloworld/WindowsLiveWriter/HowtoAddCommandLineSupportwithYour.MSI_80FA/image_6.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/helloworld/WindowsLiveWriter/HowtoAddCommandLineSupportwithYour.MSI_80FA/image_thumb_2.png" width="244" height="85" /&gt;&lt;/a&gt;&amp;#160; &lt;br /&gt;&amp;#160;&lt;/li&gt;    &lt;li&gt;Check the InstallUISequence and InstallExecuteSequence tables in the .msi using Orcas, you should see something like this:     &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/helloworld/WindowsLiveWriter/HowtoAddCommandLineSupportwithYour.MSI_80FA/image_4.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/helloworld/WindowsLiveWriter/HowtoAddCommandLineSupportwithYour.MSI_80FA/image_thumb_1.png" width="661" height="62" /&gt;&lt;/a&gt;       &lt;br /&gt;You know this is the action that you want from the Action name, it is CustomTextA, corresponds to the Textboxes (A), and it is the Edit1 control. If you check the CustomAction table, you will see something like this:      &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/helloworld/WindowsLiveWriter/HowtoAddCommandLineSupportwithYour.MSI_80FA/image_8.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/helloworld/WindowsLiveWriter/HowtoAddCommandLineSupportwithYour.MSI_80FA/image_thumb_3.png" width="664" height="43" /&gt;&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;To prevent the custom action from executing, add this condition on both &lt;u&gt;InstallUISequence and InstallExecuteSequence tables&lt;/u&gt;, &lt;strong&gt;SERVERNAME = &amp;quot;&amp;quot;&lt;/strong&gt;.      &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/helloworld/WindowsLiveWriter/HowtoAddCommandLineSupportwithYour.MSI_80FA/image_10.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/helloworld/WindowsLiveWriter/HowtoAddCommandLineSupportwithYour.MSI_80FA/image_thumb_4.png" width="663" height="56" /&gt;&lt;/a&gt;       &lt;br /&gt;This tells, if the SERVERNAME is blank (the user did not override), execute the custom action to assign the default value. Please pay attention that the condition is for an empty string (two double quotes).&lt;/li&gt;    &lt;li&gt;Save the .msi and close Orcas.&lt;/li&gt;    &lt;li&gt;Execute the .msi using this command:     &lt;br /&gt;msiexec /i [The msi name].msi SERVERNAME=&amp;quot;HELLOWORLDSERVER&amp;quot;&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;After executing those steps, the UI will show HELLOWORLDSERVER in the text box.&lt;/p&gt;  &lt;p&gt;To set this condition automatically as part of your build process, we need to use a script to modify the setup. This can be done by setting the post build event for your project.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create a .js script. Call it CommandLineSupport.js, repeat the update statement for every property that you want to be able to override.     &lt;br /&gt;&lt;span style="color: green"&gt;//This script adds command-line support for MSI build with Visual Studio 2008.        &lt;br /&gt;&lt;/span&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;msiOpenDatabaseModeTransact = 1;      &lt;br /&gt;      &lt;br /&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(WScript.Arguments.Length != 1)      &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; WScript.StdErr.WriteLine(WScript.ScriptName + &lt;span style="color: #a31515"&gt;&amp;quot; file&amp;quot;&lt;/span&gt;);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; WScript.Quit(1);      &lt;br /&gt;}      &lt;br /&gt;      &lt;br /&gt;WScript.Echo(WScript.Arguments(0));      &lt;br /&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;filespec = WScript.Arguments(0);      &lt;br /&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;installer = WScript.CreateObject(&lt;span style="color: #a31515"&gt;&amp;quot;WindowsInstaller.Installer&amp;quot;&lt;/span&gt;);      &lt;br /&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;database = installer.OpenDatabase(filespec, msiOpenDatabaseModeTransact);      &lt;br /&gt;      &lt;br /&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;sql      &lt;br /&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;view      &lt;br /&gt;      &lt;br /&gt;&lt;span style="color: blue"&gt;try       &lt;br /&gt;&lt;/span&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;//Update InstallUISequence to support command-line parameters in interactive mode.       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;sql = &lt;span style="color: #a31515"&gt;&amp;quot;UPDATE InstallUISequence SET Condition = 'SERVERNAME=\&amp;quot;\&amp;quot;' WHERE Action = 'CustomTextA_SetProperty_EDIT1'&amp;quot;&lt;/span&gt;;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; view = database.OpenView(sql);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; view.Execute();      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; view.Close();      &lt;br /&gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;//Update InstallExecuteSequence to support command line in passive or quiet mode.       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;sql = &lt;span style="color: #a31515"&gt;&amp;quot;UPDATE InstallExecuteSequence SET Condition = 'SERVERNAME=\&amp;quot;\&amp;quot;' WHERE Action = 'CustomTextA_SetProperty_EDIT1'&amp;quot;&lt;/span&gt;;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; view = database.OpenView(sql);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; view.Execute();      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; view.Close();      &lt;br /&gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; database.Commit();      &lt;br /&gt;}      &lt;br /&gt;&lt;span style="color: blue"&gt;catch&lt;/span&gt;(e)      &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; WScript.StdErr.WriteLine(e);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; WScript.Quit(1);      &lt;br /&gt;}      &lt;br /&gt;&lt;/li&gt;   &lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;    &lt;li&gt;Save the CommandLineSupport.js in the same folder with your setup project file, and set PostBuildEvent for the setup project.     &lt;br /&gt;&amp;#160;&lt;a href="http://blogs.msdn.com/blogfiles/helloworld/WindowsLiveWriter/HowtoAddCommandLineSupportwithYour.MSI_80FA/image_16.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/helloworld/WindowsLiveWriter/HowtoAddCommandLineSupportwithYour.MSI_80FA/image_thumb_7.png" width="639" height="41" /&gt;&lt;/a&gt;       &lt;br /&gt;Make sure .msi name matches your .msi file name.&lt;/li&gt;    &lt;li&gt;Build the project. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Standard disclaimer applies, no warranty, use it at your own risk, always test the changes. Wow, this post is longer than what I thought it would be.... &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9402677" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author><category term="Programming" scheme="http://blogs.msdn.com/helloworld/archive/tags/Programming/default.aspx" /><category term="Setup Project" scheme="http://blogs.msdn.com/helloworld/archive/tags/Setup+Project/default.aspx" /></entry><entry><title>Fixing the Issue that Does Not Exist</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2009/02/05/fixing-the-issue-that-does-not-exist.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2009/02/05/fixing-the-issue-that-does-not-exist.aspx</id><published>2009-02-05T14:06:44Z</published><updated>2009-02-05T14:06:44Z</updated><content type="html">&lt;p&gt;I spent some time planning on improvements that I wanted to do on one of our project. I reviewed the code that I notice a place where I think I could improve the performance by avoiding locking.&lt;/p&gt;  &lt;p&gt;So I wrote the code, and tested it, works great, the new non-locking algorithm works fine. Then when I replaced the old code with my new code and ran my test, to my surprise, it did not behave the way I want it to be. Still works, but the performance and the throughput was not any better. In some cases, it actually yield worse performance compared with the old code.&lt;/p&gt;  &lt;p&gt;Curious, I captured the performance counters by running my test app side by side, the one with old code, and the other one with new code.&lt;/p&gt;  &lt;p&gt;Look and behold, under extreme cases, the old code has zero Total # of Contentions. The new code, of course has zero Total # of Contentions, and at the same time, consumes more memory due to the lock free algorithm implementation that I chose.&lt;/p&gt;  &lt;p&gt;Basically, the lock free code tries to solve a problem that did not exist in the old code. Yes, it uses lock, but the old code runs really fast, and it is very tight, so there was no contention.&lt;/p&gt;  &lt;p&gt;Moral of the story, reviewing the code may reveal some issue, put it to test, and verify that the issue is really an issue, before planning any work on it. One good thing, I did not spend too much time on it. :)&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9399568" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author><category term=".Net Framework" scheme="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx" /><category term="Multi Thread" scheme="http://blogs.msdn.com/helloworld/archive/tags/Multi+Thread/default.aspx" /><category term="Performance" scheme="http://blogs.msdn.com/helloworld/archive/tags/Performance/default.aspx" /></entry><entry><title>IE 8 RC 1 is Now Available for Download</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2009/01/28/ie-8-rc-1-is-now-available-for-download.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2009/01/28/ie-8-rc-1-is-now-available-for-download.aspx</id><published>2009-01-28T03:06:38Z</published><updated>2009-01-28T03:06:38Z</updated><content type="html">&lt;p&gt;I downloaded and installed IE 8 RC 1 on my machine. It works really well, the performance improvement is significant.&lt;/p&gt;  &lt;p&gt;I would encourage you to give it a try, &lt;a title="http://www.microsoft.com/windows/internet-explorer/beta/worldwide-sites.aspx" href="http://www.microsoft.com/windows/internet-explorer/beta/worldwide-sites.aspx"&gt;http://www.microsoft.com/windows/internet-explorer/beta/worldwide-sites.aspx&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Disclaimer, this is an Release Candidate, not RTM. There could be some issues, but overall, this is really good.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9379093" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author><category term="Windows" scheme="http://blogs.msdn.com/helloworld/archive/tags/Windows/default.aspx" /></entry><entry><title>It Is 2009, Happy New Year!!!</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2009/01/01/now-is-2009-happy-new-year.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2009/01/01/now-is-2009-happy-new-year.aspx</id><published>2009-01-01T11:36:00Z</published><updated>2009-01-01T11:36:00Z</updated><content type="html">Happy New Year, everyone. Wish you all the best this year.&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9259423" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author></entry><entry><title>When Building Multi-Threaded Applications using ThreadPool, Make the Number of Worker Threads Configurable</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2008/12/16/when-building-multi-threaded-applications-using-threadpool-make-the-number-of-worker-threads-configurable.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2008/12/16/when-building-multi-threaded-applications-using-threadpool-make-the-number-of-worker-threads-configurable.aspx</id><published>2008-12-16T14:14:14Z</published><updated>2008-12-16T14:14:14Z</updated><content type="html">&lt;p&gt;Just a simple thought, when creating a multi-threaded applications, make the number of threads configurable. ThreadPool has this behavior that it will immediately set the minimum worker threads equal to the number of logical processors in your machine, and set the maximum to 250 per logical processor, well documented in msdn (.Net Framework 2.0 SP1).&lt;/p&gt;  &lt;p&gt;ThreadPool will create a new thread every half second, also documented in msdn. This behavior could lead to thread explosions, where there are too much threads fighting for limited resources (memory, lock, disk IO, processor, etc.). Your app may run slower if the thread creation is not managed wisely.&lt;/p&gt;  &lt;p&gt;It is also a good idea to be able to throttle the application to use fewer threads. It is a good idea to be able to limit the number of threads in the system through configuration. Set the max number of threads by calling SetMaxThreads, and if the config value is less than the number of logical processors, do not forget to call SetMinThreads. Be aware that this can also lead to other issue, like thread starvation.&lt;/p&gt;  &lt;p&gt;This is just an idea, if you have been writing multi-threaded applications using ThreadPool, probably you have thought about this already. Keep in mind, your application needs as few thread as possible, but not fewer.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9224423" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author><category term="Programming" scheme="http://blogs.msdn.com/helloworld/archive/tags/Programming/default.aspx" /><category term=".Net Framework" scheme="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx" /><category term="Multi Thread" scheme="http://blogs.msdn.com/helloworld/archive/tags/Multi+Thread/default.aspx" /></entry><entry><title>String.Split has High Cost on Performance</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2008/12/15/string-split-has-high-cost-on-performance.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2008/12/15/string-split-has-high-cost-on-performance.aspx</id><published>2008-12-16T01:59:24Z</published><updated>2008-12-16T01:59:24Z</updated><content type="html">&lt;p&gt;One application had a memory issue, it just consume too much resources. The application performed well, but it is very obvious when we saw the performance counters, something can be done to make it better. Reviewing the code did not reveal something significant, the code already allocated objects that are necessary. The code parses a text file and generate an object based on the content of that text file.&lt;/p&gt;  &lt;p&gt;After running &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=a362781c-3870-43be-8926-862b40aa0cd0&amp;amp;DisplayLang=en"&gt;CLR Profiler&lt;/a&gt;, I noticed that a lot of memory are consumed by int[], but there is no code that generates that kind of object. Digging a little bit deeper, I discovered that one method calls String.Split(). This method is one of the main method, it is being called about 80% of the time.&lt;/p&gt;  &lt;p&gt;String.Split() creates an array of strings, and in the process, it creates a helper array of int. The size of the helper array can be really huge. After the code had been modified to avoid calling String.Split(), the memory consumption was significantly reduced.&lt;/p&gt;  &lt;p&gt;If you need performance in a tight loop, avoid String.Split().&lt;/p&gt;  &lt;p&gt;If you are using Visual Studio 2008, I strongly recommend for you to step through the .Net framework source code using debugger. Check Shawn Burke's &lt;a href="http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx"&gt;post&lt;/a&gt; to do that.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9224371" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author><category term="Programming" scheme="http://blogs.msdn.com/helloworld/archive/tags/Programming/default.aspx" /><category term=".Net Framework" scheme="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx" /></entry><entry><title>How to Create Custom Event Log for Windows Service</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2008/12/11/creating-an-event-log.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2008/12/11/creating-an-event-log.aspx</id><published>2008-12-11T20:52:42Z</published><updated>2008-12-11T20:52:42Z</updated><content type="html">&lt;p&gt;When you created a Windows Service, you usually add a Service Installer to allow this service to be installed using InstallUtil or installing it programmatically like this &lt;a href="http://blogs.msdn.com/helloworld/archive/2008/10/21/how-to-install-windows-service-programmatically.aspx"&gt;post&lt;/a&gt; shows you.&lt;/p&gt;  &lt;p&gt;The service installer will create the Event Log for the service, and by default it is &amp;quot;Application&amp;quot;, and the source is the name of your service.&lt;/p&gt;  &lt;p&gt;The Event Source is most likely to be fine, but the event log may need to be set to something else. Unfortunately, the option is not visible from the installer.&lt;/p&gt;  &lt;p&gt;The Installer object for your windows service, actually hosted several other Installer objects, one of the child Installer is responsible to create the Event Log. You need to find this object and then changed the Log property and it will create the appropriate event log. It is easy to find this object as this object is an instance of EventLogInstaller.&lt;/p&gt;  &lt;p&gt;The code:&lt;/p&gt;  &lt;p&gt;&lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyServiceInstaller &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Installer     &lt;br /&gt;&lt;/span&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private const string&lt;/span&gt;LogName = &lt;span style="color: #a31515"&gt;&amp;quot;My Service Log&amp;quot;&lt;/span&gt;;    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public &lt;/span&gt;MyServiceInstaller()    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; InitializeComponent();    &lt;br /&gt;&lt;span style="color: green"&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EventLogInstaller &lt;/span&gt;EventLogInstall = &lt;span style="color: blue"&gt;null&lt;/span&gt;;    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Installer &lt;/span&gt;I &lt;span style="color: blue"&gt;in this&lt;/span&gt;.serviceInstaller.Installers)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EventLogInstall = I &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EventLogInstaller&lt;/span&gt;;    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt;(EventLogInstall != &lt;span style="color: blue"&gt;null&lt;/span&gt;)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EventLogInstall.Log = LogName;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EventLogInstall.UninstallAction = &lt;span style="color: #2b91af"&gt;UninstallAction&lt;/span&gt;.NoAction;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;break&lt;/span&gt;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;That code above shows how to create a custom Event Log for your service, the code also shows that the custom event log will never be uninstalled, depending on the requirements it can be easily changed.&lt;/p&gt;  &lt;p&gt;If the event source needs to be changed as well, simply change the Source property and you are set.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;One thing to pay attention&lt;/strong&gt;, if you create a custom event log, you install the service programmatically, and your service is running with a restricted account, &lt;u&gt;then you have to modify the code to install the service by adding a code to write one entry to the event log that you just created&lt;/u&gt;.&lt;/p&gt;  &lt;p&gt;After the service installer is executed, the log is 'registered', but not created yet. To create it, one event must be written. If the service runs using a restricted user account, that account may not have enough security permission to write the first log, as the log need to be created.&lt;/p&gt;  &lt;p&gt;When installing the service, the user must run the installer as Administrator, so the installer has all security privilege that it needs. So remember to write one event entry after programmatically installing the service. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9197604" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author><category term="Programming" scheme="http://blogs.msdn.com/helloworld/archive/tags/Programming/default.aspx" /><category term=".Net Framework" scheme="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx" /><category term="How to" scheme="http://blogs.msdn.com/helloworld/archive/tags/How+to/default.aspx" /><category term="Windows Service" scheme="http://blogs.msdn.com/helloworld/archive/tags/Windows+Service/default.aspx" /></entry><entry><title>How to Get IIS Web Sites Information Programmatically</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/helloworld/archive/2008/10/31/how-to-get-iis-web-sites-information-programmatically.aspx" /><id>http://blogs.msdn.com/helloworld/archive/2008/10/31/how-to-get-iis-web-sites-information-programmatically.aspx</id><published>2008-10-31T08:03:41Z</published><updated>2008-10-31T08:03:41Z</updated><content type="html">&lt;p&gt;I needed to get the location of IIS log files on my servers, after doing a quick investigation, I am quite amazed on how much information are exposed via managed code.&lt;/p&gt;  &lt;p&gt;This snippet will return the name of the sites and the location of the log files.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;DirectoryEntry &lt;/span&gt;Site &lt;span style="color: blue"&gt;in new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DirectoryEntry&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;IIS://&amp;quot; &lt;/span&gt;+ System.&lt;span style="color: #2b91af"&gt;Environment&lt;/span&gt;.MachineName + &lt;span style="color: #a31515"&gt;&amp;quot;/w3svc&amp;quot;&lt;/span&gt;).Children)
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;String&lt;/span&gt;.Compare(Site.SchemaClassName, &lt;span style="color: #a31515"&gt;&amp;quot;IIsWebServer&amp;quot;&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;StringComparison&lt;/span&gt;.OrdinalIgnoreCase) == 0)
        &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(Site.Properties[&lt;span style="color: #a31515"&gt;&amp;quot;ServerComment&amp;quot;&lt;/span&gt;].Value.ToString() + &lt;span style="color: #a31515"&gt;&amp;quot; == &amp;quot; &lt;/span&gt;+ Site.Properties[&lt;span style="color: #a31515"&gt;&amp;quot;LogFileDirectory&amp;quot;&lt;/span&gt;].Value.ToString());&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;To get more information about what fields and method you can access, please refer to this MSDN doc: &lt;a title="http://msdn.microsoft.com/en-us/library/ms524487.aspx" href="http://msdn.microsoft.com/en-us/library/ms524487.aspx"&gt;http://msdn.microsoft.com/en-us/library/ms524487.aspx&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Just remember, in Vista/Windows Server 2008, you will need to run that code with elevated privilege.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9025275" width="1" height="1"&gt;</content><author><name>HelloWorld</name><uri>http://blogs.msdn.com/members/HelloWorld.aspx</uri></author><category term="Programming" scheme="http://blogs.msdn.com/helloworld/archive/tags/Programming/default.aspx" /><category term=".Net Framework" scheme="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx" /><category term="Windows" scheme="http://blogs.msdn.com/helloworld/archive/tags/Windows/default.aspx" /></entry></feed>