<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>C#, VB.Net, XML and mass consumption of coffee. &#187; C#</title>
	<atom:link href="http://www.paul-zubkov.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.paul-zubkov.com</link>
	<description></description>
	<lastBuildDate>Wed, 03 Feb 2010 18:25:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Twitter API with C# example(part 2)</title>
		<link>http://www.paul-zubkov.com/2009/08/02/twitter-api-with-c-examplepart-2/</link>
		<comments>http://www.paul-zubkov.com/2009/08/02/twitter-api-with-c-examplepart-2/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 07:11:23 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[Twitter API C#]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.paul-zubkov.com/?p=249</guid>
		<description><![CDATA[
First of all, I have to admit that I decided to take a vacation.  Vacation from everything &#8211; work, freelance, projects, blog, etc.  It&#8217;s summer and despite the crappy weather here in Toronto, I am spending time with my family which actually rocks.  Now vacation is over, let&#8217;s get back to coding [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-254" title="sundial" src="http://www.paul-zubkov.com/wp-content/uploads/2009/08/sundial.jpg" alt="sundial" width="300" height="225" /></p>
<p>First of all, I have to admit that I decided to take a vacation.  Vacation from everything &#8211; work, freelance, projects, blog, etc.  It&#8217;s summer and despite the crappy weather here in Toronto, I am spending time with my family which actually rocks.  Now vacation is over, let&#8217;s get back to coding &#8211; today I wanted to continue with Twitter examples.  Twitter seems to be very hot, and I was curious as to how to get the most out of it from the coding perspective.    Here is a tiny function that gets the followers of a user, providing we know the username and password:</p>
<p><code lang="csharp[lines]">public string FetchFollowers(string userName, string password)<br />
{<br />
    using (var client = new WebClient())<br />
    {<br />
        client.Credentials = new NetworkCredential(userName, password);<br />
        try<br />
        {<br />
            using (var stream = client.OpenRead("http://twitter.com/statuses/followers.xml"))<br />
            {<br />
                using (var reader = new StreamReader(stream))<br />
                {<br />
                    return reader.ReadToEnd();<br />
                }<br />
            }<br />
        }<br />
        catch (WebException ex)<br />
        {<br />
            //insert code to deal with exception HERE<br />
        }<br />
    }<br />
    return string.Empty;<br />
}</code></p>
<p>There are two types of &#8220;connection&#8221; between the people on twitter &#8211; followers and friends &#8211; I am too lazy to look up the difference between the two, but if you want list of friends then line 8 in the function above should look like this:</p>
<p><code lang="csharp[lines]">using (var stream = client.OpenRead("http://twitter.com/statuses/friends.xml"));</code></p>
<p>I usually stuff the output of that in XML and then do what I need to do with it.</p>
<p>Let me take a break for now, next time we will cover posting to Twitter (status update).</p>
<p>BTW, first part of this could be found <a href="http://www.paul-zubkov.com/2009/07/01/twitter-api-with-c-examplepart-1/">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.paul-zubkov.com/2009/08/02/twitter-api-with-c-examplepart-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twitter API with C# example(part 1)</title>
		<link>http://www.paul-zubkov.com/2009/07/01/twitter-api-with-c-examplepart-1/</link>
		<comments>http://www.paul-zubkov.com/2009/07/01/twitter-api-with-c-examplepart-1/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 02:21:48 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[Twitter API C#]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.paul-zubkov.com/?p=220</guid>
		<description><![CDATA[
Currently I am involved in a project where number of things have to be done using Twitter API.  What really amazes me today is the quality of API&#8217;s available to developers.  Twitter is a perfect example of such API.  I will try to publish some code that I used to accomplish several [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.paul-zubkov.com/wp-content/uploads/2009/07/sunflower.jpg" alt="sunflower" title="sunflower" width="299" height="200" class="alignnone size-full wp-image-229" /></p>
<p>Currently I am involved in a project where number of things have to be done using Twitter API.  What really amazes me today is the quality of API&#8217;s available to developers.  Twitter is a perfect example of such API.  I will try to publish some code that I used to accomplish several tasks starting with getting the details of a Twitter user providing you are aware of the user password and username.</p>
<p>Following code will retrieve a variety of information about a Twitter account:<br />
<code lang="csharp"><br />
public string FetchUserDetailsAsXml(string userName, string password, string IDorScreenName)<br />
{</p>
<p>        if(string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))<br />
        {<br />
            throw new ArgumentException("userName or Password are not supplied, that is not good.");<br />
        }</p>
<p>    string url = string.Format(TwitterBaseUrlFormat, "users", "show" + "/" + IDorScreenName, "xml");</p>
<p>    using (var client = new WebClient())<br />
    {<br />
        client.Credentials = new NetworkCredential(userName, password);</p>
<p>        try<br />
        {<br />
            using (var stream = client.OpenRead(url))<br />
            {<br />
                using (var reader = new StreamReader(stream))<br />
                {<br />
                    return reader.ReadToEnd();<br />
                }<br />
            }<br />
        }<br />
        catch (WebException weex)<br />
        {<br />
            if (weex.Response is HttpWebResponse)<br />
            {<br />
                if ((weex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)<br />
                {<br />
                    return null;<br />
                }<br />
            }<br />
            throw;<br />
        }<br />
    }</p>
<p> }<br />
</code></p>
<p>That&#8217;s pretty much it.  Need System.Net and System.Web for this, obviously.  What you get out of that looks like this:</p>
<p><code lang="xml"></p>
<p>- <user><br />
  <id>14381487</id><br />
  <name>pzubkov</name><br />
  <screen_name>pzubkov</screen_name><br />
  <location>Ontario, Canada</location><br />
  <description>Coder, mostly .net</description></p>
<profile_image_url>http://s3.amazonaws.com/twitter_production/profile_images/56116140/Me_normal.jpg</profile_image_url>
  <url>http://www.paul-zubkov.com</url></p>
<protected>false</protected>
  <followers_count>39</followers_count></p>
<profile_background_color>709397</profile_background_color>
<profile_text_color>333333</profile_text_color>
<profile_link_color>FF3300</profile_link_color>
<profile_sidebar_fill_color>A0C5C7</profile_sidebar_fill_color>
<profile_sidebar_border_color>86A4A6</profile_sidebar_border_color>
  <friends_count>61</friends_count><br />
  <created_at>Mon Apr 14 05:08:22 +0000 2008</created_at><br />
  <favourites_count>0</favourites_count><br />
  <utc_offset>-18000</utc_offset><br />
  <time_zone>Eastern Time (US  Canada)</time_zone></p>
<profile_background_image_url>http://static.twitter.com/images/themes/theme6/bg.gif</profile_background_image_url>
<profile_background_tile>false</profile_background_tile>
  <statuses_count>49</statuses_count><br />
  <notifications>false</notifications><br />
  <verified>false</verified><br />
  <following>false</following><br />
- <status><br />
  <created_at>Wed Jul 01 19:15:11 +0000 2009</created_at><br />
  <id>2424620582</id><br />
  <text>twitter api rocks, this is sweet</text><br />
  <source></source>web</p>
<truncated>false</truncated>
  <in_reply_to_status_id><br />
  <in_reply_to_user_id><br />
  <favorited>false</favorited><br />
  <in_reply_to_screen_name><br />
  </in_reply_to_screen_name><br />
  </in_reply_to_user_id><br />
</in_reply_to_status_id></status></user></code></p>
<p>As we can see from above, I am not an avid Twitter user myself, but I find that the API produced by Twitter is extremely well done.  In Part 2 and 3 I will cover sending new update and getting followers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paul-zubkov.com/2009/07/01/twitter-api-with-c-examplepart-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New (at least for me) IO functions.</title>
		<link>http://www.paul-zubkov.com/2009/06/30/new-at-least-for-me-io-functions/</link>
		<comments>http://www.paul-zubkov.com/2009/06/30/new-at-least-for-me-io-functions/#comments</comments>
		<pubDate>Wed, 01 Jul 2009 04:39:24 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.paul-zubkov.com/?p=212</guid>
		<description><![CDATA[
Well, live and learn.  This was an interesting day.  I am working on a little project right now, can&#8217;t really discuss details, and quite frankly it is nothing that I am going to be too proud of once it is done.  But this is not the point.  I had to do [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-217" title="embarrassing" src="http://www.paul-zubkov.com/wp-content/uploads/2009/06/bear.png" alt="embarrassing" width="296" height="200" /></p>
<p>Well, live and learn.  This was an interesting day.  I am working on a little project right now, can&#8217;t really discuss details, and quite frankly it is nothing that I am going to be too proud of once it is done.  But this is not the point.  I had to do some minor IO work with this and this is when I found that instead of doing stuff like:<code lang="csharp"></p>
<p>_fsCFileStream = new FileStream(sFileName, FileMode.Open, System.IO.FileAccess.Read);<br />
_srStreamReader = new StreamReader(_fsCfMCCSV, Encoding.GetEncoding(1252));</p>
<p>try<br />
{<br />
    if ((sLineIn = _srCfMCCSV.ReadLine()) == null)<br />
        //do stuff here<br />
}catch(){}<br />
</code><br />
I can simply do<br />
<code lang="csharp"><br />
File.ReadAllLines();<br />
</code></p>
<p>I guess I was stuck on .Net 1.3 for so long, I completely skipped 2.x and now 3.5 is full of surprises.  I know I am behind times, need to catch up soon.  Gone to do some reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paul-zubkov.com/2009/06/30/new-at-least-for-me-io-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Displaying list of installed fonts with C#</title>
		<link>http://www.paul-zubkov.com/2008/09/28/displaying-list-of-installed-fonts-with-c/</link>
		<comments>http://www.paul-zubkov.com/2008/09/28/displaying-list-of-installed-fonts-with-c/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 04:06:44 +0000</pubDate>
		<dc:creator>Paul</dc:creator>
				<category><![CDATA[Code Samples]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[font list]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[Windows Forms]]></category>

		<guid isPermaLink="false">http://www.paul-zubkov.com/?p=55</guid>
		<description><![CDATA[I know that all 1337 hax0rz (sorry, I promise I will not be doing this any more, as a matter of fact it really bugs me when people are using this 1337 crap) are using stuff like Ruby and Python, I know that system programming is for old farts and the future is in web [...]]]></description>
			<content:encoded><![CDATA[<p>I know that all 1337 hax0rz (sorry, I promise I will not be doing this any more, as a matter of fact it really bugs me when people are using this 1337 crap) are using stuff like Ruby and Python, I know that system programming is for old farts and the future is in web development and all that.  I am not disputing this, but there are some cool things you can do with C# and Windows coding.  For some bizzare reason, I feel that it might be a good idea to publish some code which I find neat.  One of the reasons I like C# is that generally if you need a solution to a particular problem, it is most likely to be alot simpler then what I think.  Here is how you get the list of all installed fonts loaded into a ComboBox.<span id="more-55"></span></p>
<p>First thing first<code lang="csharp">using System.Drawing.Text;</code></p>
<p>Need this to get the collection of fonts.</p>
<p>Here is the form load method:<br />
<code lang="csharp"><br />
private void Form1_Load(object sender, EventArgs e)<br />
{<br />
    var fonts = new InstalledFontCollection();</p>
<p>    foreach (var family in fonts.Families)<br />
    {<br />
        comboBox1.Items.Add(family.Name);<br />
    }<br />
}<br />
</code></p>
<p>Pretty straight forward, get all the fonts into a collection called fonts, then loop through each element in this collection and add a new item to the ComboBox.  One can say, hey Paul, this could be done via DataBinding &#8211; yeah, sure, here is what you would do:<br />
<code lang="csharp"><br />
private void Form1_Load(object sender, EventArgs e)<br />
{<br />
    var fonts = new InstalledFontCollection().Families;<br />
    comboBox1.DataSource = fonts;<br />
}<br />
</code><br />
But let&#8217;s take a look at the output &#8211; here is what you get with my little foreach loop:</p>
<p><a href="http://www.paul-zubkov.com/wp-content/uploads/2008/09/form-w-loop1.png"><img class="alignnone size-medium wp-image-59" title="form-w-loop1" src="http://www.paul-zubkov.com/wp-content/uploads/2008/09/form-w-loop1.png" alt="" width="300" height="300" /></a></p>
<p>I know, not too fancy, but clean and I like clean.  Now here is what you get with DataBinding:</p>
<p><a href="http://www.paul-zubkov.com/wp-content/uploads/2008/09/form-w-databind.png"><img class="alignnone size-medium wp-image-60" title="form-w-databind" src="http://www.paul-zubkov.com/wp-content/uploads/2008/09/form-w-databind.png" alt="" width="300" height="300" /></a></p>
<p>I know, there are ways to clean this up, but why bother?</p>
<p>Next I just added a small text box to show how the fonts are going to look like:</p>
<p><a href="http://www.paul-zubkov.com/wp-content/uploads/2008/09/form-w-box.png"><img class="alignnone size-medium wp-image-61" title="form-w-box" src="http://www.paul-zubkov.com/wp-content/uploads/2008/09/form-w-box.png" alt="" width="300" height="300" /></a></p>
<p>To get this going, all I need is an event that will be triggered whenever the selection at the ComboBox is changed:<br />
<code lang="csharp"><br />
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)<br />
{<br />
    textBox1.Font = new Font(comboBox1.SelectedItem.ToString(), 12);<br />
}<br />
</code></p>
<p>That&#8217;s it, we are done here.  It compiles, it runs, and it switches the font on the TextBox.  Here is the complete code for the form:<br />
<code lang="csharp"><br />
using System;<br />
using System.Drawing;<br />
using System.Windows.Forms;<br />
using System.Drawing.Text;</p>
<p>namespace WindowsFormsApplication3<br />
{<br />
    public partial class Form1 : Form<br />
    {<br />
        public Form1()<br />
        {<br />
            InitializeComponent();<br />
        }</p>
<p>        private void Form1_Load(object sender, EventArgs e)<br />
        {<br />
            var fonts = new InstalledFontCollection();</p>
<p>            foreach (var family in fonts.Families)<br />
            {<br />
                comboBox1.Items.Add(family.Name);<br />
            }<br />
        }</p>
<p>        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)<br />
        {<br />
            textBox1.Font = new Font(comboBox1.SelectedItem.ToString(), 12);<br />
        }<br />
    }<br />
}<br />
</code></p>
<p>Beauty of C# is that it took me about 4 minutes to write this code.  It is clean, simple and does exactly what it is supposed to do.  It might not be the coolest technology out there, but it is good enough for what I do.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.paul-zubkov.com/2008/09/28/displaying-list-of-installed-fonts-with-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
