<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Gunnar Peipman's ASP.NET blog : .NET</title><link>http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx</link><description>Tags: .NET</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Windows Azure – Using DataSet with cloud storage</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/12/01/windows-azure-using-dataset-with-cloud-storage.aspx</link><pubDate>Tue, 01 Dec 2009 13:25:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7267833</guid><dc:creator>DigiMortal</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7267833</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/12/01/windows-azure-using-dataset-with-cloud-storage.aspx#comments</comments><description>&lt;p&gt;On &lt;a href="http://www.microsoft.com/windowsazure/"&gt;Windows Azure CTP&lt;/a&gt; some file system operations are not allowed. You cannot create and modify files located on server hard disc. I have small application that stores data to DataSet and I needed place where I can hold this file. The logical choice is cloud storage service of course. In this posting I will show you how to read and write &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx"&gt;DataSet&lt;/a&gt; to your cloud storage as XML-file. &lt;/p&gt;  &lt;p&gt;Although my original code is more complex (there are other things you have to handle when using file based storage in multi-user environment) I give you here the main idea about how to write some methods to handle cloud storage files. Feel free to modify my code as you wish.&lt;/p&gt;  &lt;h3&gt;Before we start&lt;/h3&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 10px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Example of cloud storage structure" border="0" alt="Example of cloud storage structure" align="right" src="http://weblogs.asp.net/blogs/gunnarpeipman/azurestorageexample_26C6DC6F.png" width="332" height="280" /&gt;You need Windows Azure storage account. If you don’t have it currently then go to &lt;a href="http://www.microsoft.com/windowsazure/account/"&gt;this page&lt;/a&gt; and register yourself. When your account is ready you must create your cloud storage service in Windows Azure portal. Also you need &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=6967ff37-813e-47c7-b987-889124b43abd&amp;amp;displayLang=en"&gt;Windows Azure Tools for Visual Studio&lt;/a&gt;. Now create a simple application and add reference to StorageClient.dll file (it is shipped with Azure tools, you can search for it from Program Files folder if you cannot find it fast).&lt;/p&gt;  &lt;p&gt;The structure of cloud storage is simple. At the root level you have your account. Under your account you have services – Table, Queue and Blob storage. We are using Blob storage in this example. Blob storage has containers at root level. You can handle containers as independent root level folders. Each container may contain one or more BLOBs – handle BLOBs as files. Screenshot here should illustrate it somehow. _blob is the name of my cloud storage service (it is name that I use in my client software). dataset is the name of container and Data.xml is my data file.&lt;/p&gt;  &lt;h3&gt;Connecting to cloud storage&lt;/h3&gt;  &lt;p&gt;As we are working with same file in same container we can say that we need container as root level object. We can easily write method that connects to cloud storage and returns us container we need. &lt;/p&gt;  &lt;hr size="1" /&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: #2b91af"&gt;BlobContainer&lt;/span&gt; GetContainer()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; account = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;StorageAccountInfo&lt;/span&gt;(&lt;/p&gt;    &lt;p style="margin: 0px"&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;&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;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Uri        &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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;http://&amp;lt;ACCOUNT&amp;gt;.blob.core.windows.net/&amp;quot;&lt;/span&gt;),&lt;/p&gt;    &lt;p style="margin: 0px"&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;&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;null&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin: 0px"&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;&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: #a31515"&gt;&amp;quot;&amp;lt;ACCOUNT&amp;gt;&amp;quot;&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin: 0px"&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;&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: #a31515"&gt;&amp;quot;&amp;lt;KEY&amp;gt;&amp;quot;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; );&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; storage = &lt;span style="color: #2b91af"&gt;BlobStorage&lt;/span&gt;.Create(account);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; container = storage.GetBlobContainer(&lt;span style="color: #a31515"&gt;&amp;quot;dataset&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; container;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt; &lt;/div&gt;  &lt;p&gt;This method is private because I don’t want to expose cloud storage details to other classes.&lt;/p&gt;  &lt;h3&gt;Loading DataSet&lt;/h3&gt;  &lt;p&gt;Now let’s read our data file from cloud storage and let’s load it to DataSet. There is one little trick you should be aware of. Take a look at the following method and notice that I have to handle the location of stream pointer.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;hr size="1" /&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public void&lt;/span&gt; Load(&lt;span style="color: #2b91af"&gt;DataSet&lt;/span&gt; data)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; container = GetContainer();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; mem = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt;())&lt;/p&gt;    &lt;p style="margin: 0px"&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;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (!container.DoesBlobExist(&lt;span style="color: #a31515"&gt;&amp;quot;Data.xml&amp;quot;&lt;/span&gt;))&lt;/p&gt;    &lt;p style="margin: 0px"&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;return&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; fileBlob = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;BlobContents&lt;/span&gt;(mem);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; container.GetBlob(&lt;span style="color: #a31515"&gt;&amp;quot;Data.xml&amp;quot;&lt;/span&gt;, fileBlob, &lt;span style="color: blue"&gt;true&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; stream = fileBlob.AsStream;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: green"&gt;// Required - stream pointer must be at position 0&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; stream.Seek(0, &lt;span style="color: #2b91af"&gt;SeekOrigin&lt;/span&gt;.Begin);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; data.ReadXml(stream);&lt;/p&gt;    &lt;p style="margin: 0px"&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; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt; &lt;/div&gt;  &lt;h3&gt;Saving DataSet&lt;/h3&gt;  &lt;p&gt;After changes it is good idea to write data back to cloud storage at some moment. Here is the method that saves new file with fresh data to cloud storage container.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;hr size="1" /&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&amp;#160;&lt;span style="color: blue"&gt;void&lt;/span&gt; Save(&lt;span style="color: #2b91af"&gt;DataSet&lt;/span&gt; data)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; container = GetContainer();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (container.DoesBlobExist(&lt;span style="color: #a31515"&gt;&amp;quot;Data.xml&amp;quot;&lt;/span&gt;))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; container.DeleteBlob(&lt;span style="color: #a31515"&gt;&amp;quot;Data.xml&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; metadata = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NameValueCollection&lt;/span&gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; metadata[&lt;span style="color: #a31515"&gt;&amp;quot;FileName&amp;quot;&lt;/span&gt;] = &lt;span style="color: #a31515"&gt;&amp;quot;Data.xml&amp;quot;&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; properties = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;BlobProperties&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;Data.xml&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&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;&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; Metadata = metadata, &lt;/p&gt;    &lt;p style="margin: 0px"&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;&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; ContentType = &lt;span style="color: #a31515"&gt;&amp;quot;text/xml&amp;quot;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using&lt;/span&gt;(&lt;span style="color: blue"&gt;var&lt;/span&gt; mem = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt;())&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; data.WriteXml(mem);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; fileBlob = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;BlobContents&lt;/span&gt;(mem.ToArray());&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; container.CreateBlob(properties, fileBlob, &lt;span style="color: blue"&gt;true&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;hr size="1" /&gt; &lt;/div&gt;  &lt;h3&gt;Conclusion&lt;/h3&gt;  &lt;p&gt;Using Windows Azure cloud storage and BLOBs is pretty simple if we use right tools. StorageClient saved us some valuable time and we were able to write some methods that are very easy to use for other programmers. You can easily change those methods so accoun information is read from application configuration and programmers can also specify container and file they need from cloud storage.&lt;/p&gt;

&lt;hr size="1" /&gt;

&lt;table border="0" cellpadding="0" cellspacing="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f12%2f01%2fwindows-azure-using-dataset-with-cloud-storage.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f12%2f01%2fwindows-azure-using-dataset-with-cloud-storage.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/Windows-Azure-Using-DataSet-with-cloud-storage"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F12%2F01%2Fwindows-azure-using-dataset-with-cloud-storage.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/Windows-Azure-Using-DataSet-with-cloud-storage"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F12%2F01%2Fwindows-azure-using-dataset-with-cloud-storage.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/Windows-Azure-Using-DataSet-with-cloud-storage"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F12%2F01%2Fwindows-azure-using-dataset-with-cloud-storage.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/Windows-Azure-Using-DataSet-with-cloud-storage"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F12%2F01%2Fwindows-azure-using-dataset-with-cloud-storage.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="4"&gt;&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://servefault.com/Windows-Azure-Using-DataSet-with-cloud-storage"&gt;&lt;img alt="Shout it!" src="http://servefault.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F12%2F01%2Fwindows-azure-using-dataset-with-cloud-storage.aspx&amp;countBackColor=00CC00" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7267833" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Azure/default.aspx">Azure</category></item><item><title>Loading Enterprise Library validation rules from external file</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/11/15/loading-enterprise-library-validation-rules-from-external-file.aspx</link><pubDate>Sun, 15 Nov 2009 09:54:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7255324</guid><dc:creator>DigiMortal</dc:creator><slash:comments>12</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7255324</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/11/15/loading-enterprise-library-validation-rules-from-external-file.aspx#comments</comments><description>&lt;p&gt;Keeping validation rules in web.config may grow it pretty long and it is not convenient to make changes there anymore. Developers can manage validation rules easily by using Enterprise Library Configuration tool and therefore there is no good reason to handle validation rules manually. In this posting I will show you how to load validation rules from external configuration files.&lt;/p&gt;  &lt;h3&gt;Creating file for validation rules&lt;/h3&gt;  &lt;p&gt;Create new XML-file to your project and name it as Validation.config. I suggest .config extension because IIS blocks all requests to configuration files. Run Enterprise Library Condiguration tool and insert some validation rules.&lt;/p&gt;  &lt;p align="center"&gt;&lt;a href="http://weblogs.asp.net/blogs/gunnarpeipman/entlibvalidationconf_0B52F41E.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Enterprise Library: Configuration tool" border="0" alt="Enterprise Library: Configuration tool" src="http://weblogs.asp.net/blogs/gunnarpeipman/entlibvalidationconf_thumb_7DDCBB4A.png" width="530" height="340" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Save changes and close configuration tool. &lt;/p&gt;  &lt;h3&gt;Loading validation rules from external file&lt;/h3&gt;  &lt;p&gt;Now open the class where you handle validation logic. In my last posting &lt;a href="http://weblogs.asp.net/gunnarpeipman/archive/2009/11/13/asp-net-mvc-validating-objects-using-enterprise-library-validation-application-block.aspx"&gt;ASP.NET MVC: Validating objects using Enterprise Library validation application block&lt;/a&gt; I gave you some idea about how to use validation in your application. There was class called Validator that took object and validated it. All validation errors are returned as array. &lt;/p&gt;  &lt;hr size="1" /&gt;&lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium tahoma, arial, helvetica; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="font-family: &amp;#39;courier new&amp;#39;; font-size: 13px" class="Apple-style-span"&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: blue"&gt;class&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;Validator&lt;/span&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;{&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;ValidationError&lt;/span&gt;[] Validate&amp;lt;T&amp;gt;(T instance)&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;errors =&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43,145,175)"&gt;ValidationError&lt;/span&gt;&amp;gt;();&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;results =&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;Validation&lt;/span&gt;.Validate&amp;lt;T&amp;gt;(instance);&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: blue"&gt;foreach&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;(&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;result&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: blue"&gt;in&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;results)&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;error =&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: rgb(43,145,175)"&gt;ValidationError&lt;/span&gt;();&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; error.PropertyName = result.Key;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; error.Message = result.Message;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; errors.Add(error);&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="color: blue"&gt;return&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;errors.ToArray();&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;      &lt;p style="margin: 0px"&gt;} &lt;/p&gt;      &lt;hr size="1" /&gt;      &lt;p&gt;&lt;/p&gt;   &lt;/span&gt;&lt;/span&gt;  &lt;p&gt;In Validate&amp;lt;T&amp;gt;() method all validation rules are read from web.config. We have to modify this method to make it read validation rules from Validation.config. Here is the class with modifications.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;hr size="1" /&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Validator&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ValidationError&lt;/span&gt;[] Validate&amp;lt;T&amp;gt;(T instance)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; errors = &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: #2b91af"&gt;ValidationError&lt;/span&gt;&amp;gt;();      &lt;br /&gt;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; source = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FileConfigurationSource&lt;/span&gt;(&lt;span style="color: #a31515"&gt;&amp;quot;Validation.config&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; validator = &lt;span style="color: #2b91af"&gt;ValidationFactory&lt;/span&gt;.CreateValidatorFromConfiguration&amp;lt;T&amp;gt;(source);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; results = validator.Validate(instance);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&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: blue"&gt;var&lt;/span&gt; result &lt;span style="color: blue"&gt;in&lt;/span&gt; results)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&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;var&lt;/span&gt; error = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ValidationError&lt;/span&gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; error.PropertyName = result.Key;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; error.Message = result.Message;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; errors.Add(error);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; errors.ToArray();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt; &lt;/div&gt;  &lt;p&gt;Validation rules are now read from Validation.config file and your web.config is lighter than before. Enterprise Library made this move very easy – only couple of lines of code were modified. Keeping validation rules in separate file is also safer – you don’t have to touch web.config to make modifications to validation rules.&lt;/p&gt;

&lt;hr size="1" /&gt;

&lt;table border="0" cellpadding="0" cellspacing="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f11%2f15%2floading-enterprise-library-validation-rules-from-external-file.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f11%2f15%2floading-enterprise-library-validation-rules-from-external-file.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/Loading-Enterprise-Library-validation-rules-from-external-file"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F11%2F15%2Floading-enterprise-library-validation-rules-from-external-file.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/Loading-Enterprise-Library-validation-rules-from-external-file"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F11%2F15%2Floading-enterprise-library-validation-rules-from-external-file.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/Loading-Enterprise-Library-validation-rules-from-external-file"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F11%2F15%2Floading-enterprise-library-validation-rules-from-external-file.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/Loading-Enterprise-Library-validation-rules-from-external-file"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F11%2F15%2Floading-enterprise-library-validation-rules-from-external-file.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="4"&gt;&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://servefault.com/Loading-Enterprise-Library-validation-rules-from-external-file"&gt;&lt;img alt="Shout it!" src="http://servefault.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F11%2F15%2Floading-enterprise-library-validation-rules-from-external-file.aspx&amp;countBackColor=00cc00" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7255324" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Enterprise+Library/default.aspx">Enterprise Library</category></item><item><title>Visual Studio 2010 Beta 2 downloads</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/11/03/visual-studio-2010-beta-2-downloads.aspx</link><pubDate>Tue, 03 Nov 2009 16:00:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7245245</guid><dc:creator>DigiMortal</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7245245</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/11/03/visual-studio-2010-beta-2-downloads.aspx#comments</comments><description>&lt;p&gt;Here are some download links to Visual Studio 2010 Beta 2 and related technologies.&lt;/p&gt;  &lt;h3&gt;Visual Studio&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=DED875C8-FE5E-4CC9-B973-2171B61FE982&amp;amp;displaylang=en"&gt;Microsoft .NET Framework 4 Beta 2&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=DC333AC8-596D-41E3-BA6C-84264E761B81&amp;amp;displaylang=en"&gt;Microsoft Visual Studio 2010 Ultimate Beta 2 - ISO&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=630de613-7b65-4179-9da4-04def4bf9ebc"&gt;Microsoft Visual Studio 2010 Premium Beta 2 – ISO&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=A80DFB5D-51C6-4778-8656-A9FF29D3A132&amp;amp;displaylang=en"&gt;Microsoft Visual Studio 2010 Professional Beta 2 - ISO&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=60A539EA-A345-446A-9DA0-EF86E4A4798F&amp;amp;displaylang=en"&gt;Microsoft Visual Studio Test Elements 2010 Beta 2 – ISO&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://bit.ly/2NuaZH"&gt;Microsoft Visual Studio 2010 Express Edition Beta 2 – ISO&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=C6A64E02-6FD6-41E3-A3FD-399D174A84B7&amp;amp;displaylang=en"&gt;Visual Studio 2010 Shell (Integrated) Beta 2 Redistributable Package&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Team Foundation Server&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=6c70fd8f-615e-4203-a028-acb2c2b8b88f"&gt;Microsoft Visual Studio Team Foundation Server 2010 Beta 2&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Visual Studio SDK-s&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=CB82D35C-1632-4370-ACFB-83C01C2ECE24&amp;amp;displaylang=en"&gt;Visual Studio 2010 SDK Beta 2&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=F5431A70-B421-4BE6-8FD3-6B27ABDA0817&amp;amp;displaylang=en"&gt;Microsoft Visual Studio 2010 DSL SDK Beta2&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Learn&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://channel9.msdn.com/shows/10-4/"&gt;10-4 Episodes of Visual Studio 2010 @ Channel 9&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=752CB725-969B-4732-A383-ED5740F02E93&amp;amp;displaylang=en"&gt;Visual Studio 2010 and .NET Framework 4 Training Kit - October Preview&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;

&lt;hr size="1" /&gt;

&lt;table border="0" cellpadding="0" cellspacing="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f11%2f03%2fvisual-studio-2010-beta-2-downloads.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f11%2f03%2fvisual-studio-2010-beta-2-downloads.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/Visual-Studio-2010-Beta-2-downloads"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F11%2F03%2Fvisual-studio-2010-beta-2-downloads.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/Visual-Studio-2010-Beta-2-downloads"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F11%2F03%2Fvisual-studio-2010-beta-2-downloads.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/Visual-Studio-2010-Beta-2-downloads"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F11%2F03%2Fvisual-studio-2010-beta-2-downloads.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/Visual-Studio-2010-Beta-2-downloads"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F11%2F03%2Fvisual-studio-2010-beta-2-downloads.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="4"&gt;&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://servefault.com/Visual-Studio-2010-Beta-2-downloads"&gt;&lt;img alt="Shout it!" src="http://servefault.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F11%2F03%2Fvisual-studio-2010-beta-2-downloads.aspx&amp;countBackColor=00CC00" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7245245" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>.Net Framework 4.0: Enumerating file system objects</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/10/27/net-framework-4-0-enumerating-file-system-objects.aspx</link><pubDate>Tue, 27 Oct 2009 09:14:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7240336</guid><dc:creator>DigiMortal</dc:creator><slash:comments>14</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7240336</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/10/27/net-framework-4-0-enumerating-file-system-objects.aspx#comments</comments><description>&lt;p&gt;In my last posting I introduced &lt;a href="http://weblogs.asp.net/gunnarpeipman/archive/2009/10/26/net-framework-4-0-system-io-file-supports-now-ienumerable-lt-string-gt.aspx"&gt;new ReadLines() method and new overloads for WriteAllLines() method of File class&lt;/a&gt;. But there are more new stuff in &lt;a href="http://msdn.microsoft.com/en-us/library/system.io(VS.100).aspx"&gt;System.IO namespace&lt;/a&gt;. In .Net Framework 4.0 &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.directory(VS.100).aspx"&gt;Directory&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.directoryinfo(VS.100).aspx"&gt;DirectoryInfo&lt;/a&gt; class are able to enumerate files, directories and file system entries. In this posting I will show you these new features.&lt;/p&gt;  &lt;p&gt;Let’s see now how to enumerate file system objects using new static methods of Directory class. There is one thing you should know. If you have to handle files and directories in same context then you should use &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.directory.enumeratefilesystementries(VS.100).aspx"&gt;EnumerateFileSystemEntries()&lt;/a&gt; method that enumerates both files and directories. The following example shows you how to enumerate different file system objects.&lt;/p&gt;  &lt;hr size="1" /&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; path = &lt;span style="color: #2b91af"&gt;Path&lt;/span&gt;.GetPathRoot(&lt;span style="color: #2b91af"&gt;Environment&lt;/span&gt;.CurrentDirectory);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; files = &lt;span style="color: #2b91af"&gt;Directory&lt;/span&gt;.EnumerateFiles(path);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; directories = &lt;span style="color: #2b91af"&gt;Directory&lt;/span&gt;.EnumerateDirectories(path);&amp;#160;&amp;#160;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; entries = &lt;span style="color: #2b91af"&gt;Directory&lt;/span&gt;.EnumerateFileSystemEntries(path);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Files:&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; file &lt;span style="color: blue"&gt;in&lt;/span&gt; files)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(file);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;\r\nDirectories:&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; directory &lt;span style="color: blue"&gt;in&lt;/span&gt; directories)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(directory);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;\r\nEntries:&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; entry &lt;span style="color: blue"&gt;in&lt;/span&gt; entries)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(entry);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt;&lt;/div&gt;  &lt;p&gt;You should get output like this (the path is root path of drive where application is located).&lt;/p&gt;  &lt;pre&gt;Files:
C:\autoexec.bat
C:\config.sys
C:\pagefile.sys
  &lt;br /&gt;Directories:
&lt;br /&gt;C:\$Recycle.Bin&lt;br /&gt;C:\Documents and Settings
&lt;br /&gt;C:\Install
&lt;br /&gt;C:\PerfLogs  &lt;br /&gt;C:\Program Files
&lt;br /&gt;C:\ProgramData  &lt;br /&gt;C:\Recovery  &lt;br /&gt;C:\System Volume Information  &lt;br /&gt;C:\Users  &lt;br /&gt;C:\Windows
  &lt;br /&gt;Entries:
&lt;br /&gt;C:\$Recycle.Bin
&lt;br /&gt;C:\autoexec.bat
&lt;br /&gt;C:\config.sys
&lt;br /&gt;C:\Documents and Settings
&lt;br /&gt;C:\Install
&lt;br /&gt;C:\pagefile.sys
&lt;br /&gt;C:\PerfLogs
&lt;br /&gt;C:\Program Files
&lt;br /&gt;C:\ProgramData
&lt;br /&gt;C:\Recovery
&lt;br /&gt;C:\System Volume Information
&lt;br /&gt;C:\Users
&lt;br /&gt;C:\Windows&lt;/pre&gt;

&lt;p&gt;These enumerating methods work better than ones that return arrays because requests to file system are made only when concrete object is asked from enumerator. I think these methods may be very good load balancers in applications that make heavy use of file system.&lt;/p&gt;

&lt;hr size="1" /&gt;

&lt;table border="0" cellpadding="0" cellspacing="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f10%2f27%2fnet-framework-4-0-enumerating-file-system-objects.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f10%2f27%2fnet-framework-4-0-enumerating-file-system-objects.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/Net-Framework-40-Enumerating-file-system-objects"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F27%2Fnet-framework-4-0-enumerating-file-system-objects.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/Net-Framework-40-Enumerating-file-system-objects"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F27%2Fnet-framework-4-0-enumerating-file-system-objects.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/Net-Framework-40-Enumerating-file-system-objects"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F27%2Fnet-framework-4-0-enumerating-file-system-objects.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/Net-Framework-40-Enumerating-file-system-objects"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F27%2Fnet-framework-4-0-enumerating-file-system-objects.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="4"&gt;&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://servefault.com/Net-Framework-40-Enumerating-file-system-objects"&gt;&lt;img alt="Shout it!" src="http://servefault.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F27%2Fnet-framework-4-0-enumerating-file-system-objects.aspx&amp;countBackColor=00CC00" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7240336" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category></item><item><title>.Net Framework 4.0: System.IO.File supports now IEnumerable&lt;string&gt;</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/10/26/net-framework-4-0-system-io-file-supports-now-ienumerable-lt-string-gt.aspx</link><pubDate>Mon, 26 Oct 2009 02:05:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7239286</guid><dc:creator>DigiMortal</dc:creator><slash:comments>15</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7239286</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/10/26/net-framework-4-0-system-io-file-supports-now-ienumerable-lt-string-gt.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/gunnarpeipman/pages/visual-studio-2010-and-net-framework-4-0.aspx"&gt;.Net Framework 4.0&lt;/a&gt; adds also some new and cool features to file system objects. &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.file(VS.100).aspx"&gt;File&lt;/a&gt; class has now &lt;a href="http://msdn.microsoft.com/en-us/library/dd383503(VS.100).aspx"&gt;ReadLines()&lt;/a&gt; methods that returns IEnumerable&amp;lt;string&amp;gt;. &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.file.writealllines(VS.100).aspx"&gt;WriteAllLines()&lt;/a&gt; methods has two overload methods that accept IEnumerable&amp;lt;string&amp;gt; instead of strings array that was also supported in previous versions of .Net Framework. This posting introduces ReadLines() and WriteAllLines() methods and gives you some ideas how to use these methods in your applications.&lt;/p&gt;  &lt;h3&gt;Querying file contents&lt;/h3&gt;  &lt;p&gt;My first example shows how to use ReadLines() to query file contents using LINQ. I have text file called lorem-ipsum.txt with some paragraphs with famous lorem ipsum text. You can generate your own text on &lt;a href="http://www.lipsum.com"&gt;www.lipsum.com&lt;/a&gt;. I use LINQ query to get all lines from this file that contain word ipsum. &lt;/p&gt;  &lt;hr size="1" /&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; lines = &lt;span style="color: #2b91af"&gt;File&lt;/span&gt;.ReadLines(&lt;span style="color: #a31515"&gt;&amp;quot;lorem-ipsum.txt&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; ipsumQuery = &lt;span style="color: blue"&gt;from&lt;/span&gt; l &lt;span style="color: blue"&gt;in&lt;/span&gt; lines&lt;/p&gt;    &lt;p style="margin: 0px"&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;where&lt;/span&gt; l.ToLower().Contains(&lt;span style="color: #a31515"&gt;&amp;quot;ipsum&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;select&lt;/span&gt; l;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; ipsumLine &lt;span style="color: blue"&gt;in&lt;/span&gt; ipsumQuery)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(ipsumLine);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt; &lt;/div&gt;  &lt;p&gt;Even if you don’t use LINQ you can still consider using ReadLines() method – it doesn’t load all the contents from file to memory like &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.file.readalllines(VS.100).aspx"&gt;ReadAllLines()&lt;/a&gt; does. When you are working with large files then ReadLines() is extremely useful method for you.&lt;/p&gt;  &lt;h3&gt;Writing query contents to file&lt;/h3&gt;  &lt;p&gt;My second example shows you how to write contents of IEnumerable&amp;lt;string&amp;gt; to file. ReadAllLines() method of File class has now two new overloads that accept IEnumerable&amp;lt;string&amp;gt;. To get example done with less effort we will use previous example and instead of printing lines to screen we will output them to separate file.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;hr size="1" /&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; lines = &lt;span style="color: #2b91af"&gt;File&lt;/span&gt;.ReadLines(&lt;span style="color: #a31515"&gt;&amp;quot;lorem-ipsum.txt&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; ipsumQuery = &lt;span style="color: blue"&gt;from&lt;/span&gt; l &lt;span style="color: blue"&gt;in&lt;/span&gt; lines&lt;/p&gt;    &lt;p style="margin: 0px"&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;where&lt;/span&gt; l.ToLower().Contains(&lt;span style="color: #a31515"&gt;&amp;quot;ipsum&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;select&lt;/span&gt; l;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;File&lt;/span&gt;.WriteAllLines(&lt;span style="color: #a31515"&gt;&amp;quot;only-ipsum.txt&amp;quot;&lt;/span&gt;, ipsumQuery);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt; &lt;/div&gt;  &lt;p&gt;Although this example is not something markable or revolutionary it illustrates you how to query one file and output results to another file. Of course, you can use instead of file query everything else that implements IEnumerable&amp;lt;string&amp;gt; interface.&lt;/p&gt;

&lt;p&gt;There is also &lt;a href="http://msdn.microsoft.com/en-us/library/system.io.file.appendalllines(VS.100).aspx"&gt;AppendAllLines()&lt;/a&gt; method that appends lines to file instead of overwriting the file like WriteAllLines() does.&lt;/p&gt;

 &lt;p&gt;In my opinion these features are very good ones and you can use these methods in all of your applications where you need to read and query or query something and write results to files.&lt;/p&gt;

&lt;hr size="1" /&gt;

&lt;table border="0" cellpadding="0" cellspacing="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f10%2f26%2fnet-framework-4-0-system-io-file-supports-now-ienumerable-lt-string-gt.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f10%2f26%2fnet-framework-4-0-system-io-file-supports-now-ienumerable-lt-string-gt.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/Net-Framework-40-SystemIOFile-supports-now-IEnumerable"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F26%2Fnet-framework-4-0-system-io-file-supports-now-ienumerable-lt-string-gt.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/Net-Framework-40-SystemIOFile-supports-now-IEnumerable"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F26%2Fnet-framework-4-0-system-io-file-supports-now-ienumerable-lt-string-gt.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/Net-Framework-40-SystemIOFile-supports-now-IEnumerable"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F26%2Fnet-framework-4-0-system-io-file-supports-now-ienumerable-lt-string-gt.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/Net-Framework-40-SystemIOFile-supports-now-IEnumerable"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F26%2Fnet-framework-4-0-system-io-file-supports-now-ienumerable-lt-string-gt.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="4"&gt;&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://servefault.com/Net-Framework-40-SystemIOFile-supports-now-IEnumerable"&gt;&lt;img alt="Shout it!" src="http://servefault.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F26%2Fnet-framework-4-0-system-io-file-supports-now-ienumerable-lt-string-gt.aspx&amp;countBackColor=00CC00" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7239286" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>.Net Framework 4.0: string.IsNullOrWhiteSpace() method</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/10/25/net-framework-4-0-string-isnullorwhitespace-method.aspx</link><pubDate>Sun, 25 Oct 2009 08:14:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7238641</guid><dc:creator>DigiMortal</dc:creator><slash:comments>23</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7238641</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/10/25/net-framework-4-0-string-isnullorwhitespace-method.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/vstudio/dd582936.aspx"&gt;.Net Framework 4.0 Beta 2&lt;/a&gt; has new IsNullOrWhiteSpace() method for strings generalizes IsNullOrEmpty() method to incluse also other white space besides empty string. In this posting I will show you simple example that illustrates how to use IsNullOrWhiteSpace() method.&lt;/p&gt;  &lt;p&gt;Term “white space” includes all characters that are not visible on screen. By example, space, line break, tab and empty string are white space characters. The following example shows how to use &lt;a href="http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace(VS.100).aspx"&gt;IsNullOrWhiteSpace()&lt;/a&gt; method.&lt;/p&gt;  &lt;hr size="1" /&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;string&lt;/span&gt; helloString = &lt;span style="color: #a31515"&gt;&amp;quot;Hello, world!&amp;quot;&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;string&lt;/span&gt; nullString = &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;string&lt;/span&gt; emptyString = &lt;span style="color: blue"&gt;string&lt;/span&gt;.Empty;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;string&lt;/span&gt; whiteSpaceString = &lt;span style="color: #a31515"&gt;&amp;quot;\t\r\n &amp;quot;&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Is null or whitespace?&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;-----------------------&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;helloString: &amp;quot;&lt;/span&gt; + &lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrWhiteSpace(helloString));&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;nullString: &amp;quot;&lt;/span&gt; + &lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrWhiteSpace(nullString));&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;emptyString: &amp;quot;&lt;/span&gt; + &lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrWhiteSpace(emptyString));&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;whiteSpaceString: &amp;quot;&lt;/span&gt; + &lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrWhiteSpace(whiteSpaceString));&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;hr size="1" /&gt;  &lt;p&gt;When you run this example you get the following output.&lt;/p&gt;  &lt;pre&gt;&lt;hr size="1" /&gt;    Is null or whitespace?
    -----------------------
    helloString: False
    nullString: True
    emptyString: True
    whiteSpaceString: True&lt;hr size="1" /&gt;&lt;/pre&gt;

&lt;p&gt;IsNullOrWhiteSpace() is useful in scenarios where string to be checked may contain some white space characters instead of null or empty string. You can use this method to check values that user inserts to form fields, by example. &lt;/p&gt;

&lt;p&gt;Also you can use this method with fixed-length character fields in database. These fields left-pad their values with spaces to field length and if database provider doesn’t trim values of these fields automatically you can use IsNullOrWhiteSpace() methods to check if field has value or not.&lt;/p&gt;

&lt;h3&gt;IsNullOrWhiteSpace() for older .Net Framework versions&lt;/h3&gt;

&lt;p&gt;If you like IsNullOrWhiteSpace() method but you cannot move to .Net Framework 4.0 you can use helper class with IsNullOrWhiteSpace() method. Yes, you have to use some helper class because you cannot add static extension methods to existing classes without recompiling them.&lt;/p&gt;

&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;
  &lt;hr size="1" /&gt;

  &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;StringHelper&lt;/span&gt;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;{&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; IsNullOrWhiteSpace(&lt;span style="color: blue"&gt;string&lt;/span&gt; s)&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (s == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/p&gt;

  &lt;p style="margin: 0px"&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;return&lt;/span&gt; &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; (s.Trim() == &lt;span style="color: blue"&gt;string&lt;/span&gt;.Empty);&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;}&lt;/p&gt;

  &lt;hr size="1" /&gt;&lt;/div&gt;

&lt;p&gt;Well, that’s it. If you have some interesting uses for IsNullOrWhiteSpace() method then feel free to drop a comment here. &lt;/p&gt;

&lt;hr size="1" /&gt;

&lt;table border="0" cellspacing="0" cellpadding="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f10%2f25%2fnet-framework-4-0-string-isnullorwhitespace-method.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f10%2f25%2fnet-framework-4-0-string-isnullorwhitespace-method.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/Net-Framework-40-stringIsNullOrWhiteSpace-method"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F25%2Fnet-framework-4-0-string-isnullorwhitespace-method.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/Net-Framework-40-stringIsNullOrWhiteSpace-method"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F25%2Fnet-framework-4-0-string-isnullorwhitespace-method.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/Net-Framework-40-stringIsNullOrWhiteSpace-method"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F25%2Fnet-framework-4-0-string-isnullorwhitespace-method.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/Net-Framework-40-stringIsNullOrWhiteSpace-method-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F25%2Fnet-framework-4-0-string-isnullorwhitespace-method.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="4"&gt;&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://servefault.com/Net-Framework-40-stringIsNullOrWhiteSpace-method"&gt;&lt;img alt="Shout it!" src="http://servefault.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F25%2Fnet-framework-4-0-string-isnullorwhitespace-method.aspx&amp;countBackColor=00CC00" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7238641" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category></item><item><title>.Net Framework 4.0: Complex numbers</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/10/23/net-framework-4-0-complex-numbers.aspx</link><pubDate>Fri, 23 Oct 2009 08:20:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7236844</guid><dc:creator>DigiMortal</dc:creator><slash:comments>20</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7236844</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/10/23/net-framework-4-0-complex-numbers.aspx#comments</comments><description>&lt;p&gt;.Net Framework 4.0 Beta 2 introduces new class in &lt;a href="http://msdn.microsoft.com/en-us/library/system.numerics(VS.100).aspx"&gt;System.Numerics&lt;/a&gt; namespace: &lt;a href="http://msdn.microsoft.com/en-us/library/system.numerics.complex(VS.100).aspx"&gt;Complex&lt;/a&gt;. Complex represents complex numbers and enables different arithmetic operations with complex numbers. In this posting I will show you how to use complex numbers in &lt;a href="http://msdn.microsoft.com/et-ee/vstudio/dd582936(en-us).aspx"&gt;.Net Framework 4.0&lt;/a&gt; applications.&lt;/p&gt;  &lt;p&gt;Complex class has two constructors – one of them has no arguments and the other takes real and complex parts of complex number. Complex numbers have also properties for &lt;a href="http://en.wikipedia.org/wiki/Complex_number"&gt;phase and magnitude&lt;/a&gt;. Let’s see the following code.&lt;/p&gt;  &lt;hr size="1" /&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; z1 = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(); &lt;span style="color: green"&gt;// this creates complex zero (0, 0)&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; z2 = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(2, 4);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; z3 = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(3, 5);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Complex zero: &amp;quot;&lt;/span&gt; + z1);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(z2 + &lt;span style="color: #a31515"&gt;&amp;quot; + &amp;quot;&lt;/span&gt; + z3 + &lt;span style="color: #a31515"&gt;&amp;quot; = &amp;quot;&lt;/span&gt; + (z2 + z3));&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;|z2| = &amp;quot;&lt;/span&gt; + z2.Magnitude);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Phase of z2 = &amp;quot;&lt;/span&gt; + z2.Phase);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt;&lt;/div&gt;  &lt;p&gt;The output of this code is as follows.&lt;/p&gt;  &lt;pre&gt;&lt;hr size="1" /&gt;    Complex zero: (0, 0)
    (2, 4) + (3, 5) = (5, 9)
    |z2| = 4,47213595499958
    Phase of z2 = 1,10714871779409&lt;hr size="1" /&gt;&lt;/pre&gt;

&lt;p&gt;As you can see from output there are some cool things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ee259521(VS.100).aspx"&gt;ToString()&lt;/a&gt; method of complex number formats complex number like in calculus courses in University (it is called Cartesian form). &lt;/li&gt;

  &lt;li&gt;You can use complex numbers like any other numbers and use usual operators on them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you feel that this is not good for some reason then you can also use static methods of Complex like Add(), Divide() etc. Following example illustrates how to use methods for calculations.&lt;/p&gt;

&lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;
  &lt;hr size="1" /&gt;

  &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;{&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; z2 = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(2, 4);&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; z3 = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;(3, 5);&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; z4 = &lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;.Add(z2, z3);&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; z5 = &lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;.Subtract(z2, z3);&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; z6 = &lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;.Multiply(z2, z3);&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; z7 = &lt;span style="color: #2b91af"&gt;Complex&lt;/span&gt;.Divide(z2, z3);&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;z2 + z3 = &amp;quot;&lt;/span&gt; + z4);&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;z2 - z3 = &amp;quot;&lt;/span&gt; + z5);&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;z2 * z3 = &amp;quot;&lt;/span&gt; + z6);&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;z2 / z3 = &amp;quot;&lt;/span&gt; + z7);&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine();&lt;/p&gt;

  &lt;p style="margin: 0px"&gt;}&lt;/p&gt;

  &lt;hr size="1" /&gt;&lt;/div&gt;

&lt;p&gt;This example produces the following output.&lt;/p&gt;

&lt;pre&gt;&lt;hr size="1" /&gt;    z2 + z3 = (5, 9)  &lt;br /&gt;    z2 - z3 = (-1, -1)
    z2 * z3 = (-14, 22)
    z2 / z3 = (0,764705882352941, 0,0588235294117647)
&lt;hr size="1" /&gt;&lt;/pre&gt;

&lt;p&gt;There are also other mathematical operations defined for complex numbers like trigonometric ones and logarithms, so you can do basically everything you like with complex numbers. I found one thing missing – Parse() and TryParse() methods. Let’s hope these methods are available in stable version of .Net Framework 4.0.&lt;/p&gt;

&lt;hr size="1" /&gt;
&lt;table border="0" cellpadding="0" cellspacing="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f10%2f23%2fnet-framework-4-0-complex-numbers.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f10%2f23%2fnet-framework-4-0-complex-numbers.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/Net-Framework-40-Complex-numbers-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F23%2Fnet-framework-4-0-complex-numbers.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/Net-Framework-40-Complex-numbers-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F23%2Fnet-framework-4-0-complex-numbers.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/Net-Framework-40-Complex-numbers-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F23%2Fnet-framework-4-0-complex-numbers.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/Net-Framework-40-Complex-numbers"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F23%2Fnet-framework-4-0-complex-numbers.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="4"&gt;&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://servefault.com/Net-Framework-40-Complex-numbers-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Shout it!" src="http://servefault.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F10%2F23%2Fnet-framework-4-0-complex-numbers.aspx&amp;countBackColor=00CC00" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7236844" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Creating squared avatar images</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/09/16/creating-squared-avatar-images.aspx</link><pubDate>Wed, 16 Sep 2009 11:28:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7207934</guid><dc:creator>DigiMortal</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7207934</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/09/16/creating-squared-avatar-images.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/gunnarpeipman/gunn128_7D4096FD.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 10px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="Gunnar" border="0" alt="Gunnar" align="left" src="http://weblogs.asp.net/blogs/gunnarpeipman/gunn128_thumb_732BF5D2.jpg" width="36" height="36" /&gt;&lt;/a&gt; In one of my current projects I needed something that takes image uploaded by user and converts it to avatar image. There is requirement for avatar images: they must be 32x32 pixel squares. Here is my solution.&lt;/p&gt;  &lt;p&gt;Creating squared thumbnail image from what ever user uploaded is pretty simple process. It is illustrated on image you can see on right side of this posting. Here are some explanations.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 10px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Creating avatar image: the process" border="0" alt="Creating avatar image: the process" align="right" src="http://weblogs.asp.net/blogs/gunnarpeipman/avatarprocess_36B38128.png" width="200" height="435" /&gt;&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;strong&gt;Detect image size&lt;/strong&gt;       &lt;br /&gt;It doesn’t really matter what dimensions image has. I just wanted to make it visually easier to understand how my algorithm works.        &lt;br /&gt;&amp;#160; &lt;/li&gt;      &lt;li&gt;&lt;strong&gt;Detect shorter size&lt;/strong&gt;       &lt;br /&gt;To take a square we have to take one of image sides as square side. We take shorter side to avoid some resizing operations that may affect the quality of avatar image we are creating. We will use Math.Mix to detect correct side length of square.       &lt;br /&gt;&amp;#160; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Focus square to center&lt;/strong&gt;       &lt;br /&gt;Usually pictures are taken with focus on some object and this object is located it the center area of picture. Today it is not hard to use photo as avatar, so let’s consider this as a default behavior of end user. To get center area we subtract square side length from original imade size and divide the result by two.       &lt;br /&gt;&amp;#160; &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;Create new image from square&lt;/strong&gt;       &lt;br /&gt;After steps above we have detected the area of original image we want to use as avatar. Now we will copy this area to new avatar image and save avatar to disc (or database or some other place). &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Now you should understand what we are going to do and it’s time to show you my code. I have small and easy method that does all the work mentioned above. I am using classes from System.Drawing and these classes are really powerful. Here is my method.&lt;/p&gt;  &lt;hr size="1" /&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; CreateAvatar(&lt;span style="color: blue"&gt;int&lt;/span&gt; sideLength, &lt;span style="color: #2b91af"&gt;Stream&lt;/span&gt; fromStream, &lt;span style="color: #2b91af"&gt;Stream&lt;/span&gt; toStream)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using&lt;/span&gt;(&lt;span style="color: blue"&gt;var&lt;/span&gt; image = System.Drawing.&lt;span style="color: #2b91af"&gt;Image&lt;/span&gt;.FromStream(fromStream))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; thumbBitmap = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Bitmap&lt;/span&gt;(sideLength, sideLength))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; a = &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Min(image.Width, image.Height);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; x = (image.Width - a)/2;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; y = (image.Height - a)/2;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; thumbGraph = &lt;span style="color: #2b91af"&gt;Graphics&lt;/span&gt;.FromImage(thumbBitmap))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; thumbGraph.CompositingQuality = &lt;span style="color: #2b91af"&gt;CompositingQuality&lt;/span&gt;.HighQuality;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; thumbGraph.SmoothingMode = &lt;span style="color: #2b91af"&gt;SmoothingMode&lt;/span&gt;.HighQuality;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; thumbGraph.InterpolationMode = &lt;span style="color: #2b91af"&gt;InterpolationMode&lt;/span&gt;.HighQualityBicubic;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&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;var&lt;/span&gt; imgRectangle = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Rectangle&lt;/span&gt;(0, 0, sideLength, sideLength);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; thumbGraph.DrawImage(image, imgRectangle, x, y, a, a, &lt;span style="color: #2b91af"&gt;GraphicsUnit&lt;/span&gt;.Pixel);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; thumbBitmap.Save(toStream, &lt;span style="color: #2b91af"&gt;ImageFormat&lt;/span&gt;.Png);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt; &lt;/div&gt;  &lt;p&gt;If you know some better method how to write this code then please let me know! :)&lt;/p&gt;

&lt;hr size="1" /&gt;

&lt;table border="0" cellpadding="0" cellspacing="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f09%2f16%2fcreating-squared-avatar-images.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f09%2f16%2fcreating-squared-avatar-images.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/Creating-squared-avatar-images-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F09%2F16%2Fcreating-squared-avatar-images.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/Creating-squared-avatar-images-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F09%2F16%2Fcreating-squared-avatar-images.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/Creating-squared-avatar-images-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F09%2F16%2Fcreating-squared-avatar-images.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/Creating-squared-avatar-images-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F09%2F16%2Fcreating-squared-avatar-images.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7207934" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Graphics/default.aspx">Graphics</category></item><item><title>GetValue&lt;T&gt;() extension method for RegistryKey class</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/08/20/getvalue-extension-method-for-registrykey-class.aspx</link><pubDate>Thu, 20 Aug 2009 10:37:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7174843</guid><dc:creator>DigiMortal</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7174843</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/08/20/getvalue-extension-method-for-registrykey-class.aspx#comments</comments><description>&lt;p&gt;I needed some elegant method to read registry values in my &lt;a href="http://weblogs.asp.net/gunnarpeipman/archive/2009/08/19/bookmarks2livewriter-live-writer-plug-in-for-serious-bloggers.aspx"&gt;Live Writer Delicious bookmarks plug-in&lt;/a&gt;. &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.aspx"&gt;RegistryKey&lt;/a&gt; has &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.getvalue.aspx"&gt;GetValue() method&lt;/a&gt; but it returns values as object. I wrote generic version of GetValue() as RegistryKey &lt;a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx"&gt;extension method&lt;/a&gt;. It returns you specified value from key and converts it to type you ask. If conversions are not successful then default value of given type will be used. &lt;/p&gt;  &lt;p&gt;I have two overloads for GetValue&amp;lt;T&amp;gt;(). First of them wants only value name to be specified. Second one expects also default value that is returned in the case of problems. I give some credits to Tony Wright whose blog entry &lt;a href="http://tonesdotnetblog.wordpress.com/2009/02/25/how-to-add-a-tonumber-c-language-extension-method-to-the-string-class-calling-tryparse-using-reflection/"&gt;How to add a ToNumber C# language extension method to the String class calling TryParse using reflection&lt;/a&gt; gave me some directions.&lt;/p&gt;  &lt;p&gt;Here are my extension methods in C# and VB.NET. Don’t forget to import &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.win32.aspx"&gt;Microsoft.Win32 namespace&lt;/a&gt;.&lt;/p&gt; &lt;strong&gt;C#&lt;/strong&gt;   &lt;hr size="1" /&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; T GetValue&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt; &lt;span style="color: #2b91af"&gt;RegistryKey&lt;/span&gt; key, &lt;span style="color: blue"&gt;string&lt;/span&gt; name)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; key.GetValue&amp;lt;T&amp;gt;(name, &lt;span style="color: blue"&gt;default&lt;/span&gt;(T));&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; T GetValue&amp;lt;T&amp;gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt; &lt;span style="color: #2b91af"&gt;RegistryKey&lt;/span&gt; key, &lt;span style="color: blue"&gt;string&lt;/span&gt; name,       &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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; T defaultValue)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; obj = key.GetValue(name, defaultValue);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (obj == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; defaultValue;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; (T)obj;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;catch&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;InvalidCastException&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(T) == &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; obj = obj.ToString();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; (T)obj;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; types = &lt;span style="color: blue"&gt;new&lt;/span&gt;[]{&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;),&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(T).MakeByRefType()};&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; method = &lt;span style="color: blue"&gt;typeof&lt;/span&gt; (T).GetMethod(&lt;span style="color: #a31515"&gt;&amp;quot;TryParse&amp;quot;&lt;/span&gt;, types);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (method == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; defaultValue;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; valueString = obj.ToString();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; T value = defaultValue;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; params = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;object&lt;/span&gt;[] {valueString, value};&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; method.Invoke(obj, params);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; (T)methodParams[1];&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt;&lt;/div&gt; &lt;strong&gt;&amp;#160; &lt;br /&gt;VB.NET&lt;/strong&gt;   &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;hr size="1" /&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;System.Runtime.CompilerServices.Extension()&amp;gt; _&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;Public&lt;/span&gt; &lt;span style="color: blue"&gt;Function&lt;/span&gt; GetValue(&lt;span style="color: blue"&gt;Of&lt;/span&gt; T)(&lt;span style="color: blue"&gt;ByVal&lt;/span&gt; key &lt;span style="color: blue"&gt;As&lt;/span&gt; RegistryKey, &lt;span style="color: blue"&gt;ByVal&lt;/span&gt; name &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;String&lt;/span&gt;) &lt;span style="color: blue"&gt;As&lt;/span&gt; T&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Return&lt;/span&gt; key.GetValue(&lt;span style="color: blue"&gt;Of&lt;/span&gt; T)(name, &lt;span style="color: blue"&gt;Nothing&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Function&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;lt;System.Runtime.CompilerServices.Extension()&amp;gt; _&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;Public&lt;/span&gt; &lt;span style="color: blue"&gt;Function&lt;/span&gt; GetValue(&lt;span style="color: blue"&gt;Of&lt;/span&gt; T)(&lt;span style="color: blue"&gt;ByVal&lt;/span&gt; key &lt;span style="color: blue"&gt;As&lt;/span&gt; RegistryKey, &lt;span style="color: blue"&gt;ByVal&lt;/span&gt; name &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;String&lt;/span&gt;, &lt;span style="color: blue"&gt;ByVal&lt;/span&gt; defaultValue &lt;span style="color: blue"&gt;As&lt;/span&gt; T) &lt;span style="color: blue"&gt;As&lt;/span&gt; T&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Dim&lt;/span&gt; obj = key.GetValue(name, defaultValue)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;If&lt;/span&gt; obj &lt;span style="color: blue"&gt;Is&lt;/span&gt; &lt;span style="color: blue"&gt;Nothing&lt;/span&gt; &lt;span style="color: blue"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Return&lt;/span&gt; defaultValue&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Try&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Return&lt;/span&gt; &lt;span style="color: blue"&gt;DirectCast&lt;/span&gt;(obj, T)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Catch&lt;/span&gt; generatedExceptionName &lt;span style="color: blue"&gt;As&lt;/span&gt; InvalidCastException&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Try&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;If&lt;/span&gt; &lt;span style="color: blue"&gt;GetType&lt;/span&gt;(T) &lt;span style="color: blue"&gt;Is&lt;/span&gt; &lt;span style="color: blue"&gt;GetType&lt;/span&gt;(&lt;span style="color: blue"&gt;String&lt;/span&gt;) &lt;span style="color: blue"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; obj = obj.ToString()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Return&lt;/span&gt; &lt;span style="color: blue"&gt;DirectCast&lt;/span&gt;(obj, T)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Dim&lt;/span&gt; paramTypes = &lt;span style="color: blue"&gt;New&lt;/span&gt; Type() {&lt;span style="color: blue"&gt;GetType&lt;/span&gt;(&lt;span style="color: blue"&gt;String&lt;/span&gt;), &lt;span style="color: blue"&gt;GetType&lt;/span&gt;(T).MakeByRefType()}&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Dim&lt;/span&gt; method = &lt;span style="color: blue"&gt;GetType&lt;/span&gt;(T).GetMethod(&lt;span style="color: #a31515"&gt;&amp;quot;TryParse&amp;quot;&lt;/span&gt;, paramTypes)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;If&lt;/span&gt; method &lt;span style="color: blue"&gt;Is&lt;/span&gt; &lt;span style="color: blue"&gt;Nothing&lt;/span&gt; &lt;span style="color: blue"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Return&lt;/span&gt; defaultValue&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Dim&lt;/span&gt; valueString = obj.ToString()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Dim&lt;/span&gt; value &lt;span style="color: blue"&gt;As&lt;/span&gt; T = defaultValue&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Dim&lt;/span&gt; methodParams = &lt;span style="color: blue"&gt;New&lt;/span&gt; &lt;span style="color: blue"&gt;Object&lt;/span&gt;() {valueString, value}&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; method.Invoke(obj, methodParams)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;Return&lt;/span&gt; &lt;span style="color: blue"&gt;DirectCast&lt;/span&gt;(methodParams(1), T)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Function&lt;/span&gt;&lt;/p&gt;    &lt;hr size="1" /&gt;&lt;/div&gt;  &lt;h3&gt;Example&lt;/h3&gt;  &lt;p&gt;And here is my little example in C# and VB.NET.&lt;/p&gt; &lt;strong&gt;C#&lt;/strong&gt;   &lt;hr size="1" /&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; softKey = &lt;span style="color: #2b91af"&gt;Registry&lt;/span&gt;.CurrentUser.OpenSubKey(&lt;span style="color: #a31515"&gt;&amp;quot;Software&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; myKey = softKey.OpenSubKey(&lt;span style="color: #a31515"&gt;&amp;quot;MyAppSettings&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;showTitle.Checked = myKey.GetValue&amp;lt;&lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;(&lt;span style="color: #a31515"&gt;&amp;quot;ShowTitle&amp;quot;&lt;/span&gt;,&lt;span style="color: blue"&gt;true&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;showDescr.Checked = myKey.GetValue&amp;lt;&lt;span style="color: blue"&gt;bool&lt;/span&gt;&amp;gt;(&lt;span style="color: #a31515"&gt;&amp;quot;ShowDescriptions&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;hr size="1" /&gt;&lt;/div&gt; &lt;strong&gt;&amp;#160; &lt;br /&gt;VB.NET&lt;/strong&gt;   &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;hr size="1" /&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;Dim&lt;/span&gt; softKey &lt;span style="color: blue"&gt;As&lt;/span&gt; RegistryKey&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;Dim&lt;/span&gt; myKey &lt;span style="color: blue"&gt;As&lt;/span&gt; RegistryKey&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;softKey = Registry.CurrentUser.OpenSubKey(&lt;span style="color: #a31515"&gt;&amp;quot;Software&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;myKey = softKey.OpenSubKey(&lt;span style="color: #a31515"&gt;&amp;quot;MyAppSettings&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;showTitle.Checked = myKey.GetValue(&lt;span style="color: blue"&gt;Of&lt;/span&gt; &lt;span style="color: blue"&gt;Boolean&lt;/span&gt;)(&lt;span style="color: #a31515"&gt;&amp;quot;ShowTitle&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;True&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;showDescr.Checked = myKey.GetValue(&lt;span style="color: blue"&gt;Of&lt;/span&gt; &lt;span style="color: blue"&gt;Boolean&lt;/span&gt;)(&lt;span style="color: #a31515"&gt;&amp;quot;ShowDescriptions&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;hr size="1" /&gt;&lt;/div&gt;  &lt;p&gt;That’s it. If you know better solution for reading registry keys or you find bugs here then please feel free to drop me a line. All your feedback is very welcome.&lt;/p&gt;

&lt;hr size="1" /&gt;

&lt;table border="0" cellpadding="0" cellspacing="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f08%2f20%2fgetvalue-extension-method-for-registrykey-class.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f08%2f20%2fgetvalue-extension-method-for-registrykey-class.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/GetValue-extension-method-for-RegistryKey-class-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F20%2Fgetvalue-extension-method-for-registrykey-class.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/GetValue-extension-method-for-RegistryKey-class-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F20%2Fgetvalue-extension-method-for-registrykey-class.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/GetValue-extension-method-for-RegistryKey-class-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F20%2Fgetvalue-extension-method-for-registrykey-class.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/GetValue-extension-method-for-RegistryKey-class-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F20%2Fgetvalue-extension-method-for-registrykey-class.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7174843" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Windows/default.aspx">Windows</category></item><item><title>Links 2009-08-19</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/08/19/links-2009-08-19.aspx</link><pubDate>Wed, 19 Aug 2009 12:34:36 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7173422</guid><dc:creator>DigiMortal</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7173422</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/08/19/links-2009-08-19.aspx#comments</comments><description>&lt;h3&gt;JQuery and web development&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://feedproxy.google.com/~r/devcurry/~3/VaqnR2-uK9k/limit-number-of-characters-in-textarea.html"&gt;Limit Number of Characters in a TextArea using jQuery&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.devcurry.com/2009/07/find-out-which-key-was-pressed-using.html"&gt;Find out which Key was pressed using jQuery&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.electrictoolbox.com/jquery-superfish-menus-plugin/"&gt;jQuery Superfish Menus Plug-in&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://matthewjamestaylor.com/blog/website-images-video-effect"&gt;Make website images look like a video with this CSS effect&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.quirksmode.org/css/opacity.html"&gt;CSS2 – opacity&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Mobile&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://feedproxy.google.com/~r/mobile-ent/cyPp/~3/Upc9oAsb_9k/55-per-cent-of-subscribers-think-content-is-too-expensive"&gt;55 per cent of subscribers think content is too expensive&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://feedproxy.google.com/~r/IntoMobile/~3/PhXPESfGimM/rim-files-patent-for-merged-capacitive-and-resistive-touchscreen.html"&gt;RIM Files Patent for Merged Capacitive and Resistive Touchscreen&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://wmpoweruser.com/?p=6842"&gt;HTC patents magnetic stylus for capacitive screens&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Sharepoint&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://feedproxy.google.com/~r/WaldekMastykarz/~3/1ByRSKX6jXs/"&gt;Programmatically creating Wiki Pages&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/rodneyviana/archive/2009/08/13/presenting-moss-support-explorer-not-even-beta-yet.aspx"&gt;Presenting MOSS Support Explorer (Not even Beta Yet)&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/ketaanhs/archive/2009/08/12/this-week-in-pictures-web-part-in-sharepoint-2007-and-time-interval.aspx"&gt;&amp;quot;This week in Pictures&amp;quot; web part in Sharepoint 2007 and Time Interval&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://feedproxy.google.com/~r/Made4thenet/~3/wpCGhrmy_zo/sharepoint-site-collections-and-100gb-content-database-guidance.aspx"&gt;#SharePoint Site Collections and 100Gb Content Database guidance&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Social Networks&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/tzink/archive/2009/08/17/facebook-spam.aspx"&gt;Facebook spam&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://feedproxy.google.com/~r/IntoMobile/~3/NzVZ27BL4Ek/twitter-co-founder-evan-williams-wife-tweets-to-the-world-while-in-labor.html"&gt;Twitter Co-Founder Evan Williamsā€™ Wife Tweets to the World While in Labor&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.newsfactor.com/story.xhtml?story_id=68342"&gt;Twitter-Written Opera Will Debut at Royal Opera House&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.cio.com/article/499670/Stop_the_Presses_40_of_Tweets_Are_Pointless_Babble_?source=rss_all"&gt;Stop the Presses! 40% of Tweets Are 'Pointless Babble'&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.cio.com/article/499631/Twitter_Used_to_Manage_Botnet_Says_Security_Expert?source=rss_all"&gt;Twitter Used to Manage Botnet, Says Security Expert&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Software&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://dbschemaviewer.codeplex.com/"&gt;DB Schema Viewer&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://weblogs.asp.net/hishamelbreky/archive/2009/08/18/game-program-for-kids-less-than-2-years.aspx"&gt;Game program for kids less than 2 years :)&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/drnick/archive/2009/08/13/using-tcp-analyzer-to-debug-tcp-performance.aspx"&gt;Using TCP Analyzer to Debug TCP Performance&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://feedproxy.google.com/~r/SmallWorkarounds/~3/gcm-GLt4JNg/o3d-google-releases-3d-support-open-api.html"&gt;O3D:- Google Releases 3D support open api for web browsers&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://communityclips.officelabs.com/download.aspx"&gt;Community Clips&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/nickmac/archive/2009/08/12/remote-server-administration-tools-rsat-for-windows-7-released.aspx"&gt;Remote Server Administration Tools RSAT for Windows 7 Released&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Programming&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://blogs.msdn.com/laurionb/archive/2009/08/13/multimethods-in-c-4-0-with-dynamic.aspx"&gt;Multimethods in C# 4.0 with 'dynamic'&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/pfxteam/archive/2009/08/12/9867246.aspx"&gt;Implementing Parallel While with Parallel.ForEach&lt;/a&gt;&lt;/li&gt;    &lt;li&gt;&lt;a href="http://feedproxy.google.com/~r/AyendeRahien/~3/YIETZN8jMy0/instantiating-interfaces.aspx"&gt;Instantiating interfaces&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://msmvps.com/blogs/deborahk/archive/2009/08/14/using-linq-with-microsoft-word-and-excel.aspx"&gt;Using Linq with Microsoft Word and Excel&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/cellfish/archive/2009/08/12/20-tips-to-write-a-good-stored-procedure-is-really-just-12.aspx"&gt;20 tips to write a good stored procedure (is really just 12)&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a href="http://blog.coderlab.us/2005/09/22/using-the-innertext-property-with-firefox/"&gt;Using the innerText Property in Firefox&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Other stuff&lt;/h3&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a href="http://www.myokyawhtun.com/tips-tricks/how-to-reset-regional-language-to-english-in-windows-live-writer.html"&gt;How to reset regional language to English in Windows Live Writer&lt;/a&gt; (Live Writer)&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.webdevbros.net/2009/07/06/nhibernate-and-the-null-object-pattern/"&gt;Nhibernate and the Null object pattern&lt;/a&gt; (programming)&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.sharepointjoel.com/Lists/Posts/ViewPost.aspx?ID=249"&gt;Preparing Today for SharePoint 2010 Q&amp;amp;A&lt;/a&gt; (SharePoint 2010)&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/dphill/articles/66300.aspx"&gt;What is a Smart Client anyway?&lt;/a&gt; (varia)&lt;/li&gt;    &lt;li&gt;&lt;a href="http://blogs.msdn.com/wer/archive/2009/08/12/the-three-cs-of-response-satisfaction.aspx"&gt;WER Services : The Three Cs of Response Satisfaction&lt;/a&gt; (Windows)&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=d2afacbb-5af6-45c2-b275-932116e27b0b"&gt;Download details: Virtual Hard Disk Getting Started Guide&lt;/a&gt; (virtual-pc)&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.devcurry.com/2009/08/creating-visual-guidelines-inside.html"&gt;Creating Visual Guidelines inside Visual Studio&lt;/a&gt; (Visual Studio)&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.cmswire.com/cms/web-development/googles-decision-to-launch-the-chrome-operating-system-reflects-the-growing-importance-of-web-applications-005301.php?utm_source=MainRSSFeed&amp;amp;utm_medium=Web&amp;amp;utm_campaign=RSS-News#evt-never"&gt;Google’s Decision to Launch the Chrome Operating System Reflects the Growing Importance of Web Applications&lt;/a&gt; (Google)&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.cmswire.com/cms/web-development/google-services-for-websites-adds-new-tools-005288.php?utm_source=MainRSSFeed&amp;amp;utm_medium=Web&amp;amp;utm_campaign=RSS-News"&gt;Google Services for Websites Adds New Tools&lt;/a&gt; (Google)&lt;/li&gt; &lt;/ul&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7173422" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Databases/default.aspx">Databases</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Web+development/default.aspx">Web development</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Windows/default.aspx">Windows</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Tools/default.aspx">Tools</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Live/default.aspx">Live</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Links/default.aspx">Links</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Software/default.aspx">Software</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Social+webs/default.aspx">Social webs</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Parallel+computing/default.aspx">Parallel computing</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/AJAX/default.aspx">AJAX</category></item><item><title>.Net Framework 4.0: C# and optional arguments and named parameters</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/08/19/net-framework-4-0-c-and-optional-arguments-and-named-parameters.aspx</link><pubDate>Tue, 18 Aug 2009 23:11:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7172769</guid><dc:creator>DigiMortal</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7172769</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/08/19/net-framework-4-0-c-and-optional-arguments-and-named-parameters.aspx#comments</comments><description>&lt;p&gt;C# 4.0 supports optional method arguments. Related to this is support for named parameters in function calls. For us it makes easier to use methods which have long argument list. It also introduces some new dangers which may lead us to messy and hard to understand code. In this code I will show how to use optional arguments and give some hints how to avoid messy code.&lt;/p&gt;  &lt;p&gt;I wrote simple application to illustrate default values and their usage. &lt;/p&gt;  &lt;hr size="1" /&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Program&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(GetArguments(&lt;span style="color: #a31515"&gt;&amp;quot;one&amp;quot;&lt;/span&gt;));&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(GetArguments(&lt;span style="color: #a31515"&gt;&amp;quot;one&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;four&amp;quot;&lt;/span&gt;));&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(GetArguments(&lt;span style="color: #a31515"&gt;&amp;quot;one&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;four&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;five&amp;quot;&lt;/span&gt;));&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(GetArguments(&lt;span style="color: #a31515"&gt;&amp;quot;one&amp;quot;&lt;/span&gt;, third:&lt;span style="color: #a31515"&gt;&amp;quot;five&amp;quot;&lt;/span&gt;));&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Press any key to exit...&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; GetArguments(&lt;span style="color: blue"&gt;string&lt;/span&gt; first, &lt;span style="color: blue"&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160; string&lt;/span&gt; second = &lt;span style="color: #a31515"&gt;&amp;quot;two&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt; third=&lt;span style="color: #a31515"&gt;&amp;quot;three&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; first + &lt;span style="color: #a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + second + &lt;span style="color: #a31515"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + third;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt;&lt;/div&gt;  &lt;p&gt;&lt;a href="http://weblogs.asp.net/blogs/gunnarpeipman/csharpoptionalargumentsoutput_6B6486BF.png"&gt;&lt;img style="border-right-width: 0px; margin: 5px 0px 0px 10px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="csharp-optional-arguments-output" border="0" alt="csharp-optional-arguments-output" align="right" src="http://weblogs.asp.net/blogs/gunnarpeipman/csharpoptionalargumentsoutput_thumb_10159E37.png" width="296" height="175" /&gt;&lt;/a&gt;Example (not exact) output of this program is shown on image on right.&lt;/p&gt;  &lt;p&gt;First three calls to GetArguments() are not something crucial and they follow current C# syntax. &lt;/p&gt;  &lt;p&gt;Fourth call introduces named parameter. Notice how we are giving value to first and third argument. Syntax is very simple: &lt;strong&gt;parameter:value&lt;/strong&gt;.&lt;/p&gt;  &lt;h3&gt;Use carefully&lt;/h3&gt;  &lt;p&gt;Optional arguments are widely used in VBA for long time. Although they make life a little bit easier for programmers (you don’t have to repeat default values in your method calls) there will be always danger that optional arguments are overused.&lt;/p&gt;  &lt;p&gt;Too long argument lists refers to the need of parameter object (refactoring method called &lt;a href="http://sourcemaking.com/refactoring/introduce-parameter-object"&gt;Introduce Parameter Object&lt;/a&gt;). Parameter object may also carry default values because you can assign initial values to object attributes and properties. &lt;/p&gt;  &lt;p&gt;Of course, it is up to you and depends on context if you want to use method with many arguments or parameter object. If you know pitfalls then it is easier for you to make decisions. &lt;/p&gt;

&lt;hr size="1" /&gt;

&lt;table border="0" cellpadding="0" cellspacing="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f08%2f19%2fnet-framework-4-0-c-and-optional-arguments-and-named-parameters.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f08%2f19%2fnet-framework-4-0-c-and-optional-arguments-and-named-parameters.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/Net-Framework-40-C-and-optional-arguments-and-named-parameters-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F19%2Fnet-framework-4-0-c-and-optional-arguments-and-named-parameters.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/Net-Framework-40-C-and-optional-arguments-and-named-parameters-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F19%2Fnet-framework-4-0-c-and-optional-arguments-and-named-parameters.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/Net-Framework-40-C-and-optional-arguments-and-named-parameters-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F19%2Fnet-framework-4-0-c-and-optional-arguments-and-named-parameters.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/Net-Framework-40-C-and-optional-arguments-and-named-parameters-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F19%2Fnet-framework-4-0-c-and-optional-arguments-and-named-parameters.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7172769" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Developing Delicious applications</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/08/15/developing-delicious-applications.aspx</link><pubDate>Sat, 15 Aug 2009 11:51:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7170168</guid><dc:creator>DigiMortal</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7170168</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/08/15/developing-delicious-applications.aspx#comments</comments><description>&lt;p&gt;I am developing a little piece of software that interacts with &lt;a href="http://delicious.com/"&gt;Delicious&lt;/a&gt; through HTTP API. Using &lt;a href="http://delicious.com/help/api"&gt;Delicious API&lt;/a&gt; puts you one responsibility – use their service carefully. Don’t flood it and don’t stress it some other way. In this posting I will show you how to get bookmarks from Delicious and how to use their service without stressing it pointlessly.&lt;/p&gt;  &lt;p&gt;Why Delicious? Because I keep &lt;a href="http://delicious.com/gpeipman"&gt;my bookmarks&lt;/a&gt; there. If you are interested in my selection you can always check out what new bookmarks I have. I try to add new bookmarks as often as I find something useful.&lt;/p&gt;  &lt;h3&gt;Connecting to Delicious&lt;/h3&gt;  &lt;p&gt;I am using very simple custom class to query my bookmarks at Delicious. My class connects to Delicious using my username and password. As my class is still very yound it has some not so nice things – like hardcoded UrlPrefix. But it is okay for now.&lt;/p&gt;  &lt;hr size="1" /&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DeliciousClient&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; _userName;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; _password;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; UrlPrefix = &lt;span style="color: #a31515"&gt;&amp;quot;https://api.del.icio.us/v1/&amp;quot;&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; DeliciousClient(&lt;span style="color: blue"&gt;string&lt;/span&gt; userName, &lt;span style="color: blue"&gt;string&lt;/span&gt; password)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _userName = userName;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; _password = password;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Bookmark&lt;/span&gt;&amp;gt; GetBookmarks(&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt; fromDate,       &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;&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt; toDate)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; url = &lt;span style="color: #a31515"&gt;&amp;quot;posts/all?&amp;quot;&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; url += &lt;span style="color: #a31515"&gt;&amp;quot;fromdt=&amp;quot;&lt;/span&gt;;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; url += fromDate.Date.ToString(&lt;span style="color: #a31515"&gt;&amp;quot;yyyy-MM-ddThh:mm:ssZ&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; url += &lt;span style="color: #a31515"&gt;&amp;quot;&amp;amp;todt=&amp;quot;&lt;/span&gt;;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; url += toDate.Date.ToString(&lt;span style="color: #a31515"&gt;&amp;quot;yyyy-MM-ddThh:mm:ssZ&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;string&lt;/span&gt; result = RequestData(url);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;     &lt;br /&gt;&lt;/p&gt;    &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; // Parse XML and return list of bookmarks&lt;/span&gt;&lt;/p&gt;   &lt;/div&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; RequestData(&lt;span style="color: blue"&gt;string&lt;/span&gt; uri)&lt;/p&gt;    &lt;p style="margin: 0px"&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; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; client = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WebClient&lt;/span&gt;())&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&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;var&lt;/span&gt; credential = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NetworkCredential&lt;/span&gt;(_userName,       &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;&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;&amp;#160;&amp;#160;&amp;#160; _password);       &lt;br /&gt;      &lt;br /&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; client.Credentials = credential;&lt;/p&gt;    &lt;p style="margin: 0px"&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;var&lt;/span&gt; url = UrlPrefix + uri;&lt;/p&gt;    &lt;p style="margin: 0px"&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;var&lt;/span&gt; result = client.DownloadData(url);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&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; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Encoding&lt;/span&gt;.Default.GetString(result);&lt;/p&gt;    &lt;p style="margin: 0px"&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;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt; &lt;/div&gt;  &lt;p&gt;Using this code I am able to connect to Delicious and run my queries to get data. But if I connect to often then Delicious handles my requests as flooding and stops access for hours. With little efforts we are able to avoid it.&lt;/p&gt;  &lt;h3&gt;Caching queries&lt;/h3&gt;  &lt;p&gt;To avoid real connections to Delicious we can cache query results for us. Just perform there steps:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Put break-point somewhere where you are able to get pure XML response (RequestData method is good candidate).&lt;/li&gt;    &lt;li&gt;Connect to Delicious through your code.&lt;/li&gt;    &lt;li&gt;When break-point is hit copy the XML result to clipboard.&lt;/li&gt;    &lt;li&gt;Stop your program.&lt;/li&gt;    &lt;li&gt;Create new XML-file called DeliciousResponse.xml to bin\Debug folder.&lt;/li&gt;    &lt;li&gt;Paste XML to this file and save it.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Now a little trick. We have Debug and Release configurations available out-of-box in Visual Studio. Let’s use them now. Debug configuration defines DEBUG constant for preprocessor. We can therefore write a code that is executed if DEBUG is there. &lt;/p&gt;  &lt;p&gt;If DEBUG is defined we will read data from XML-file we created previously. The code is here for GetBookmarks method.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;hr size="1" /&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Bookmark&lt;/span&gt;&amp;gt; GetBookmarks(&lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt; fromDate,       &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;&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;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt; toDate)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;var&lt;/span&gt; url = &lt;span style="color: #a31515"&gt;&amp;quot;posts/all?&amp;quot;&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; url += &lt;span style="color: #a31515"&gt;&amp;quot;fromdt=&amp;quot;&lt;/span&gt;;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; url += fromDate.Date.ToString(&lt;span style="color: #a31515"&gt;&amp;quot;yyyy-MM-ddThh:mm:ssZ&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; url += &lt;span style="color: #a31515"&gt;&amp;quot;&amp;amp;todt=&amp;quot;&lt;/span&gt;;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; url += toDate.Date.ToString(&lt;span style="color: #a31515"&gt;&amp;quot;yyyy-MM-ddThh:mm:ssZ&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;string&lt;/span&gt; result;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;#if&lt;/span&gt; DEBUG&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: blue"&gt;var&lt;/span&gt; reader = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;StreamReader&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;File&lt;/span&gt;.OpenRead      &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;&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: #a31515"&gt;&amp;quot;DeliciousResponse.xml&amp;quot;&lt;/span&gt;)))&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; result = reader.ReadToEnd();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;#else&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: gray"&gt;&amp;#160;&amp;#160;&amp;#160; result = RequestData(url);&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;#endif&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;     &lt;br /&gt;&lt;/p&gt;    &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;&amp;#160;&amp;#160;&amp;#160; // Parse XML and return list of bookmarks&lt;/span&gt;&lt;/p&gt;   &lt;/div&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt; &lt;/div&gt;  &lt;p&gt;Now you can run your code using Debug configuration and avoid requests to Delicious. If you need real data and communication with Delicious you can switch your project to Release configuration. I think it is very simple and powerful solution for now (of course, I will change my mind when requirements to my software change).&lt;/p&gt;  &lt;p&gt;That’s it. If you write something cool for Delicious then let also community to know about it.&lt;/p&gt;

&lt;hr size="1" /&gt;

&lt;table border="0" cellpadding="0" cellspacing="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f08%2f15%2fdeveloping-delicious-applications.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f08%2f15%2fdeveloping-delicious-applications.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/Developing-Delicious-applications"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F15%2Fdeveloping-delicious-applications.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/Developing-Delicious-applications-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F15%2Fdeveloping-delicious-applications.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/Developing-Delicious-applications"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F15%2Fdeveloping-delicious-applications.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/Developing-Delicious-applications"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F15%2Fdeveloping-delicious-applications.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://webdevvote.com:80/vote/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f08%2f15%2fdeveloping-delicious-applications.aspx"&gt;&lt;img src="http://webdevvote.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f08%2f15%2fdeveloping-delicious-applications.aspx" border="0" alt="vote it on WebDevVote.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7170168" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Web+Services/default.aspx">Web Services</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Social+webs/default.aspx">Social webs</category></item><item><title>Displaying custom HTML in WebBrowser control</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/08/15/displaying-custom-html-in-webbrowser-control.aspx</link><pubDate>Sat, 15 Aug 2009 07:46:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7170064</guid><dc:creator>DigiMortal</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7170064</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/08/15/displaying-custom-html-in-webbrowser-control.aspx#comments</comments><description>&lt;p&gt;I am using &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx"&gt;WebBrowser control&lt;/a&gt; to show preview of automatically generated HTML. Users can select options and preview pane reflects those changes automatically. WebBrowser control has some problems that have been here for years already. Here is my example about how to show custom HTML in WebBrowser control.&lt;/p&gt;  &lt;h3&gt;Problem&lt;/h3&gt;  &lt;p&gt;When we look at methods and properties of WebBrowser control our common sense tells us that something like this should work (VB.NET users: delete “;” after this line):&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;hr size="1" /&gt;    &lt;p style="margin: 0px"&gt;webBrowser1.DocumentText = &lt;span style="color: #a31515"&gt;&amp;quot;some text here&amp;quot;&lt;/span&gt;;&lt;/p&gt;    &lt;hr size="1" /&gt;&lt;/div&gt;  &lt;p&gt;And it works - only once. All subsequent assignments change nothing. When we try to refresh WebBrowser control we get only white are. Setting &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.allownavigation.aspx"&gt;AllowNavigation property&lt;/a&gt; to true – some guys suggest it – has no effect also. &lt;/p&gt;  &lt;h4&gt;Solution&lt;/h4&gt;  &lt;p&gt;To get all assignments works after first one we need to navigate to some page. about:blank is good candidate because it “exists” also in local machine for sure. And that’s not enough – we need to write something to document. After that we can show our custom HTML. &lt;/p&gt; &lt;strong&gt;C#&lt;/strong&gt;   &lt;hr size="1" /&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; DisplayHtml(&lt;span style="color: blue"&gt;string&lt;/span&gt; html)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; webBrowser1.Navigate(&lt;span style="color: #a31515"&gt;&amp;quot;about:blank&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (webBrowser1.Document != &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; webBrowser1.Document.Write(&lt;span style="color: blue"&gt;string&lt;/span&gt;.Empty);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; webBrowser1.DocumentText = html;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt;&lt;/div&gt;  &lt;p&gt;&lt;/p&gt; &lt;strong&gt;VB.NET&lt;/strong&gt;   &lt;hr size="1" /&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;Private&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt; DisplayHtml(&lt;span style="color: blue"&gt;ByVal&lt;/span&gt; html &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;String&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; webBrowser1.Navigate(&lt;span style="color: #a31515"&gt;&amp;quot;about:blank&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;If&lt;/span&gt; webBrowser1.Document &lt;span style="color: blue"&gt;IsNot&lt;/span&gt; &lt;span style="color: blue"&gt;Nothing&lt;/span&gt; &lt;span style="color: blue"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; webBrowser1.Document.Write(&lt;span style="color: blue"&gt;String&lt;/span&gt;.Empty)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; webBrowser1.DocumentText = html&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;&lt;/p&gt;    &lt;hr size="1" /&gt;&lt;/div&gt;  &lt;p&gt;&lt;strong&gt;NB!&lt;/strong&gt; You should set AllowNavigation property to true before you deal with contents shown to users. &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Keeping users on generated page&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Now we can show HTML correctly and everything seems to be okay. But are we finished? Answer is yes if we want users to be able to follow the links we show to them and no if we want to keep users on generated page. &lt;/p&gt;  &lt;p&gt;Currently we allowed navigation because otherwise we cannot move to about:blank. We have to allow this navigation and disable all other navigation. Fortunately there is &lt;a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigating.aspx"&gt;Navigating event&lt;/a&gt; of WebBrowser. In Navigating event we can do our filtering.&lt;/p&gt; &lt;strong&gt;C#&lt;/strong&gt;   &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;hr size="1" /&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; webBrowser1_Navigating(&lt;span style="color: blue"&gt;object&lt;/span&gt; sender, &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;WebBrowserNavigatingEventArgs&lt;/span&gt; e)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (e.Url.ToString() != &lt;span style="color: #a31515"&gt;&amp;quot;about:blank&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.Cancel = &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt;    &lt;hr size="1" /&gt;&lt;/div&gt;  &lt;p&gt;&lt;/p&gt; &lt;strong&gt;VB.NET&lt;/strong&gt;   &lt;hr size="1" /&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;Private&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt; webBrowser1_Navigating(&lt;span style="color: blue"&gt;ByVal&lt;/span&gt; sender &lt;span style="color: blue"&gt;As&lt;/span&gt; &lt;span style="color: blue"&gt;Object&lt;/span&gt;, _&lt;/p&gt;    &lt;p style="margin: 0px"&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;ByVal&lt;/span&gt; e &lt;span style="color: blue"&gt;As&lt;/span&gt; WebBrowserNavigatingEventArgs)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;If&lt;/span&gt; e.Url.ToString() &amp;lt;&amp;gt; &lt;span style="color: #a31515"&gt;&amp;quot;about:blank&amp;quot;&lt;/span&gt; &lt;span style="color: blue"&gt;Then&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.Cancel = &lt;span style="color: blue"&gt;True&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;If&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;End&lt;/span&gt; &lt;span style="color: blue"&gt;Sub&lt;/span&gt;&lt;/p&gt;    &lt;hr size="1" /&gt;&lt;/div&gt;  &lt;p&gt;So, that’s it. We can now display our custom HTML in WebBrowser control as many times as we want. If you have some better trick then please let me know!&lt;/p&gt;

&lt;hr size="1" /&gt;

&lt;table border="0" cellpadding="0" cellspacing="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f08%2f15%2fdisplaying-custom-html-in-webbrowser-control.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f08%2f15%2fdisplaying-custom-html-in-webbrowser-control.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/Displaying-custom-HTML-in-WebBrowser-control-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F15%2Fdisplaying-custom-html-in-webbrowser-control.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/Displaying-custom-HTML-in-WebBrowser-control-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F15%2Fdisplaying-custom-html-in-webbrowser-control.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/Displaying-custom-HTML-in-WebBrowser-control-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F15%2Fdisplaying-custom-html-in-webbrowser-control.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/Displaying-custom-HTML-in-WebBrowser-control-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F15%2Fdisplaying-custom-html-in-webbrowser-control.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7170064" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Windows+Forms/default.aspx">Windows Forms</category></item><item><title>LINQ To Entities, SQL and performance</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/08/02/linq-to-entities-sql-and-performance.aspx</link><pubDate>Sun, 02 Aug 2009 00:22:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7159499</guid><dc:creator>DigiMortal</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7159499</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/08/02/linq-to-entities-sql-and-performance.aspx#comments</comments><description>&lt;p&gt;I have introduced my photo gallery building process in couple of postings. As &lt;a href="http://weblogs.asp.net/gunnarpeipman/pages/mygallery2010.aspx"&gt;my gallery&lt;/a&gt; uses &lt;a href="http://weblogs.asp.net/gunnarpeipman/pages/visual-studio-2010-and-net-framework-4-0.aspx"&gt;Entity Framework 4.0&lt;/a&gt; it is good source for some internal processing that takes place inside Entity Framework. In this posting I will show you some LINQ To Entities queries and SQL that is generated for them. I added also some thoughts about performance.&lt;/p&gt;  &lt;p&gt;My gallery model is shown here with database structure.&lt;/p&gt;  &lt;table border="0" cellspacing="2" cellpadding="0"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="mygallery-semicomposite" border="0" alt="mygallery-semicomposite" src="http://weblogs.asp.net/blogs/gunnarpeipman/mygallerysemicomposite_26DCE80A.png" width="241" height="302" /&gt;&lt;/td&gt;        &lt;td&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="mygallery2010-db" border="0" alt="mygallery2010-db" src="http://weblogs.asp.net/blogs/gunnarpeipman/mygallery2010db_014344DC.png" width="277" height="302" /&gt;&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top"&gt;         &lt;p align="center"&gt;&lt;em&gt;Class diagram of gallery.&lt;/em&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top"&gt;         &lt;p align="center"&gt;&lt;em&gt;Database structure of gallery.&lt;/em&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;h3&gt;&lt;strong&gt;Get all public gallery items that have no parent album&lt;/strong&gt;&lt;/h3&gt;  &lt;p&gt;First example shows LINQ query that returns all gallery items that have no parent album. I can use this query for gallery main page by example.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;hr size="1" /&gt;     &lt;p style="margin: 0px"&gt;query = &lt;span style="color: blue"&gt;from&lt;/span&gt; g &lt;span style="color: blue"&gt;in&lt;/span&gt; context.GalleryItems&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;where&lt;/span&gt; g.ParentAlbum == &lt;span style="color: blue"&gt;null&lt;/span&gt; &amp;amp;&amp;amp;&lt;/p&gt;    &lt;p style="margin: 0px"&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; g.Visible&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;select&lt;/span&gt; g;&lt;/p&gt;    &lt;hr size="1" /&gt; &lt;/div&gt;  &lt;p&gt;This query produces the following SQL query. Take a closer look at it and see how inheritance hierarchy is handled here.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;hr size="1" /&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; 1 &lt;span style="color: blue"&gt;AS &lt;/span&gt;[C1], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;CASE &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;WHEN &lt;/span&gt;([UnionAll1].[C1] = 1) &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;THEN &lt;/span&gt;&lt;span style="color: #a31515"&gt;'0X0X' &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;ELSE &lt;/span&gt;&lt;span style="color: #a31515"&gt;'0X1X' &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;END AS &lt;/span&gt;[C2], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; [UnionAll1].[gallery_item_id] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[C3], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; [Extent3].[title] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[title], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; [Extent3].[description] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[description], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; [Extent3].[visible] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[visible], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;CASE &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;WHEN &lt;/span&gt;([UnionAll1].[C1] = 1) &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;THEN CAST&lt;/span&gt;(&lt;span style="color: blue"&gt;NULL AS varchar&lt;/span&gt;(1)) &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;ELSE &lt;/span&gt;[UnionAll1].[file_name] &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;END AS &lt;/span&gt;[C4], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;CASE &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;WHEN &lt;/span&gt;([UnionAll1].[C1] = 1) &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;THEN CAST&lt;/span&gt;(&lt;span style="color: blue"&gt;NULL AS bit&lt;/span&gt;) &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;ELSE &lt;/span&gt;[UnionAll1].[gallery_thumb] &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;END AS &lt;/span&gt;[C5], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; [Extent3].[parent_album_id] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[parent_album_id]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;(&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Extent1].[gallery_item_id] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[gallery_item_id], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Extent1].[file_name] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[file_name], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Extent1].[gallery_thumb] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[gallery_thumb], &lt;/p&gt;    &lt;p style="margin: 0px"&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;cast&lt;/span&gt;(0 &lt;span style="color: blue"&gt;as bit&lt;/span&gt;) &lt;span style="color: blue"&gt;AS &lt;/span&gt;[C1]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [dbo].[photo] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Extent1]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;UNION ALL&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [Extent2].[gallery_item_id] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[gallery_item_id], &lt;/p&gt;    &lt;p style="margin: 0px"&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;CAST&lt;/span&gt;(&lt;span style="color: blue"&gt;NULL AS varchar&lt;/span&gt;(1)) &lt;span style="color: blue"&gt;AS &lt;/span&gt;[C1], &lt;/p&gt;    &lt;p style="margin: 0px"&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;CAST&lt;/span&gt;(&lt;span style="color: blue"&gt;NULL AS bit&lt;/span&gt;) &lt;span style="color: blue"&gt;AS &lt;/span&gt;[C2], &lt;/p&gt;    &lt;p style="margin: 0px"&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;cast&lt;/span&gt;(1 &lt;span style="color: blue"&gt;as bit&lt;/span&gt;) &lt;span style="color: blue"&gt;AS &lt;/span&gt;[C3]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [dbo].[album] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Extent2]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; ) &lt;span style="color: blue"&gt;AS &lt;/span&gt;[UnionAll1]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;INNER JOIN &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; [dbo].[gallery_item] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Extent3] &lt;span style="color: blue"&gt;ON &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; [UnionAll1].[gallery_item_id] = [Extent3].[gallery_item_id]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;WHERE &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; ([Extent3].[visible] = 1) &lt;span style="color: blue"&gt;AND &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; ([Extent3].[parent_album_id] &lt;span style="color: blue"&gt;IS NULL&lt;/span&gt;)&lt;/p&gt; &lt;/div&gt;  &lt;hr size="1" /&gt;  &lt;h3&gt;Get all top level galleries&lt;/h3&gt;  &lt;p&gt;Well… I have one simple rule in my gallery – all gallery items that are not albums must have parent album. Query shown above returns always only Album type objects. Let’s say now in LINQ To Entities query that we need only albums to be searched.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;hr size="1" /&gt;     &lt;p style="margin: 0px"&gt;query = &lt;span style="color: blue"&gt;from&lt;/span&gt; g &lt;span style="color: blue"&gt;in&lt;/span&gt; context.GalleryItems.OfType&amp;lt;&lt;span style="color: #2b91af"&gt;Album&lt;/span&gt;&amp;gt;()&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;where&lt;/span&gt; g.ParentAlbum == &lt;span style="color: blue"&gt;null&lt;/span&gt; &amp;amp;&amp;amp;&lt;/p&gt;    &lt;p style="margin: 0px"&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; g.Visible&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;select&lt;/span&gt; g;&lt;/p&gt;    &lt;hr size="1" /&gt; &lt;/div&gt;  &lt;p&gt;This is the query that is produced by Entity Framework.&lt;/p&gt;  &lt;div style="font-family: courier new; background: white; color: black; font-size: 10pt"&gt;   &lt;hr size="1" /&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; 1 &lt;span style="color: blue"&gt;AS &lt;/span&gt;[C1], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #a31515"&gt;'0X0X' &lt;/span&gt;&lt;span style="color: blue"&gt;AS &lt;/span&gt;[C2], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; [Extent1].[gallery_item_id] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[gallery_item_id], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; [Extent2].[title] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[title], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; [Extent2].[description] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[description], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; [Extent2].[visible] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[visible], &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; [Extent2].[parent_album_id] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[parent_album_id]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; [dbo].[album] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Extent1]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;INNER JOIN &lt;/span&gt;[dbo].[gallery_item] &lt;span style="color: blue"&gt;AS &lt;/span&gt;[Extent2] &lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;ON &lt;/span&gt;[Extent1].[gallery_item_id] = [Extent2].[gallery_item_id]&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;WHERE &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; ([Extent2].[visible] = 1) &lt;span style="color: blue"&gt;AND &lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; ([Extent2].[parent_album_id] &lt;span style="color: blue"&gt;IS NULL&lt;/span&gt;)&lt;/p&gt;    &lt;hr size="1" /&gt; &lt;/div&gt;  &lt;p&gt;We can see that this query is not so complex than previous one. It is shorter and doesn’t have any complex parts like unions and from clauses that contain sub-query.&lt;/p&gt;  &lt;h3&gt;Getting better performance&lt;/h3&gt;  &lt;p&gt;Two examples above showed us very well how same thing can be done in more that one way. First example produced long and complex SQL-query while second example produced short and simple query. Both of them return exactly the same results.&lt;/p&gt;  &lt;p&gt;When writing LINQ To Entities queries it is very good idea to make sure that queries use minimal set of data. If we have 1.000 albums and 100.000 photos then second query takes less time to execute because it contains no selects or joins to photos table.&lt;/p&gt;

&lt;hr size="1" /&gt;

&lt;table border="0" cellpadding="0" cellspacing="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f08%2f02%2flinq-to-entities-sql-and-performance.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f08%2f02%2flinq-to-entities-sql-and-performance.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/LINQ-To-Entities-SQL-and-performance-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F02%2Flinq-to-entities-sql-and-performance.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/LINQ-To-Entities-SQL-and-performance-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F02%2Flinq-to-entities-sql-and-performance.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/LINQ-To-Entities-SQL-and-performance-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F02%2Flinq-to-entities-sql-and-performance.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/LINQ-To-Entities-SQL-and-performance-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F08%2F02%2Flinq-to-entities-sql-and-performance.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7159499" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Databases/default.aspx">Databases</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Performance/default.aspx">Performance</category></item><item><title>Visual Studio 2010: Database projects</title><link>http://weblogs.asp.net/gunnarpeipman/archive/2009/07/29/visual-studio-2010-database-projects.aspx</link><pubDate>Wed, 29 Jul 2009 10:15:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:7156550</guid><dc:creator>DigiMortal</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/gunnarpeipman/rsscomments.aspx?PostID=7156550</wfw:commentRss><comments>http://weblogs.asp.net/gunnarpeipman/archive/2009/07/29/visual-studio-2010-database-projects.aspx#comments</comments><description>&lt;p&gt;Visual Studio 2008 introduced us database projects. These projects are also present in Visual Studio 2010. I noticed some differences already when building my photo gallery application. Let’s see how these projects work under Visual Studio 2010. &lt;/p&gt;  &lt;h3&gt;Why database projects?&lt;/h3&gt;  &lt;p&gt;Database projects are good for more than one reason. Main reason is simple – all database objects are defined in same solution where is code that is using this database. Because database project is project like the others then it is possible to add it to code repository – you get versioning this way. &lt;/p&gt;  &lt;p&gt;You can build and deploy database projects so you get SQL-scripts and up to date database on your database server. And… I’ve heard that database projects are also able to incremental deployments. &lt;/p&gt;  &lt;h3&gt;Structure of database projects&lt;/h3&gt;  &lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 5px 0px 0px 15px; display: inline; border-top: 0px; border-right: 0px" title="Mygallery2010: Database project" border="0" alt="Mygallery2010: Database project" align="right" src="http://weblogs.asp.net/blogs/gunnarpeipman/mygallery2010databaseproject_525713D7.png" width="285" height="619" /&gt; In my last posting about my photo gallery system, &lt;a href="http://weblogs.asp.net/gunnarpeipman/archive/2009/07/25/packaging-my-gallery-code.aspx"&gt;Packaging my gallery code&lt;/a&gt;, I wrote about how to separate files from web project to other projects. One of there projects was database project called MyGallery2010.Database. &lt;/p&gt;  &lt;p&gt;You can see my database project structure on right. Currently I have definitions for tables, primary keys and foreign keys. I think it is enough for now. When project gets some more complex requirements on database then I will add new features. &lt;/p&gt;  &lt;p&gt;One difference I noticed is that empty database project is created without deep folder structure in Visual Studio 2010. Visual Studio 2008 database projects created also folders for tables, keys and so on. Visual Studio 2010 let’s you decide how do you want to organize the definitions of your database projects.&lt;/p&gt;  &lt;p&gt;The internal structure of database projects is very rich. Take a look at properties section – there are settings for SQL command variables, deployments, permissions and other settings. You can also add database references to Visual Studio database projects.&lt;/p&gt;  &lt;p&gt;And one thing more – take a look at Post-Deployment and Pre-Deployment folders. You can add there special SQL-scripts that are run before and after database deployment.&lt;/p&gt;  &lt;h3&gt;Adding database objects to project&lt;/h3&gt;  &lt;p&gt;After creating new database project it is easy to start adding definitions for database objects. You can right-click on database project and select database object type from menu as show on following screenshot fragment.&lt;/p&gt;  &lt;p align="center"&gt;&lt;a href="http://weblogs.asp.net/blogs/gunnarpeipman/visualstudio2010dbmenu_01E50C97.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Visual Studio 2010: Database menu" border="0" alt="Visual Studio 2010: Database menu" src="http://weblogs.asp.net/blogs/gunnarpeipman/visualstudio2010dbmenu_thumb_5102552B.png" width="530" height="568" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You can also use &amp;quot;Add New Item” menu selection to add new database object definition from database objects menu as shown on following screenshot.&lt;/p&gt;  &lt;p align="center"&gt;&lt;a href="http://weblogs.asp.net/blogs/gunnarpeipman/visualstudio2010dbadditem_0BB9D835.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Visual Studio 2010: Add new database object" border="0" alt="Visual Studio 2010: Add new database object" src="http://weblogs.asp.net/blogs/gunnarpeipman/visualstudio2010dbadditem_thumb_279AE760.png" width="530" height="347" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;After adding new object definition Visual Studio 2010 creates file for it and adds example definition to it. You can use it to build up your own object definition. Building your database using Visual Studio database projects guarantees pretty well that you pay attention to every object you define. I think it may decrease also bugs in database definitions.&lt;/p&gt;  &lt;h3&gt;Building and deploying database&lt;/h3&gt;  &lt;p&gt;Building and deploying database projects is not different from other projects. What is different is output. &lt;/p&gt;  &lt;p align="center"&gt;&lt;a href="http://weblogs.asp.net/blogs/gunnarpeipman/mygallery2010databasebuildoutput_158EA3D3.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="MyGallery2010: Build results of database project" border="0" alt="MyGallery2010: Build results of database project" src="http://weblogs.asp.net/blogs/gunnarpeipman/mygallery2010databasebuildoutput_thumb_0F675738.png" width="530" height="190" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;You can see that before any deployments database scripts are validated and also object dependencies are analyzed. It means that before deployment the order of changes is already clear and deployment scripts have no errors.&lt;/p&gt;

&lt;hr size="1" /&gt;

&lt;table border="0" cellpadding="0" cellspacing="2" width="100%"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f07%2f29%2fvisual-studio-2010-database-projects.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fweblogs.asp.net%2fgunnarpeipman%2farchive%2f2009%2f07%2f29%2fvisual-studio-2010-database-projects.aspx" border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://pimpthisblog.com/Visual-Studio-2010-Database-projects-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="pimp it" src="http://pimpthisblog.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F07%2F29%2Fvisual-studio-2010-database-projects.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://progg.ru/Visual-Studio-2010-Database-projects-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Progg it" src="http://progg.ru/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F07%2F29%2Fvisual-studio-2010-database-projects.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="center"&gt;
&lt;a rev="vote-for" href="http://www.9efish.com/Visual-Studio-2010-Database-projects-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="顶" src="http://www.9efish.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F07%2F29%2Fvisual-studio-2010-database-projects.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;td align="right"&gt;
&lt;a rev="vote-for" href="http://dotnetshoutout.com/Visual-Studio-2010-Database-projects-Gunnar-Peipmans-ASPNET-blog"&gt;&lt;img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fweblogs.asp.net%2Fgunnarpeipman%2Farchive%2F2009%2F07%2F29%2Fvisual-studio-2010-database-projects.aspx" style="border:0px"/&gt;&lt;/a&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=7156550" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/.NET/default.aspx">.NET</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Visual+Studio/default.aspx">Visual Studio</category><category domain="http://weblogs.asp.net/gunnarpeipman/archive/tags/Databases/default.aspx">Databases</category></item></channel></rss>