<?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>Andrew Beaton :: FAQ &#187; Programming</title>
	<atom:link href="http://andrewbeaton.net/faq/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://andrewbeaton.net/faq</link>
	<description>Tips from the world of a software developer</description>
	<lastBuildDate>Wed, 10 Mar 2010 21:03:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Read MP3 ID3 Tags in Perl</title>
		<link>http://andrewbeaton.net/faq/2009/12/03/read-mp3-id3-tags-in-perl/</link>
		<comments>http://andrewbeaton.net/faq/2009/12/03/read-mp3-id3-tags-in-perl/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 20:59:12 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[id3]]></category>
		<category><![CDATA[id3 tags]]></category>
		<category><![CDATA[mp3]]></category>
		<category><![CDATA[tags]]></category>

		<guid isPermaLink="false">http://andrewbeaton.net/faq/?p=76</guid>
		<description><![CDATA[The package I use for simple tag reading is MP3::Tag, available on CPAN with more information here.
The following example shows how easy it is to retrieve tags from an MP3 file.

use MP3::Tag;
&#160;
my $file = MP3::Tag-&#62;new&#40;$filename&#41;;
&#160;
my &#40;$title, $track, $artist, $album, $comment, $year, $genre&#41; = $file-&#62;autoinfo&#40;&#41;;
&#160;
print &#34;Title: [$title]\n&#34;;
print &#34;Artist: [$artist]\n&#34;;
print &#34;Album: [$album]\n&#34;;
print &#34;Track: [$track]\n&#34;;
print &#34;Year: [$year]\n&#34;;
print &#34;Genre: [...]]]></description>
			<content:encoded><![CDATA[<p>The package I use for simple tag reading is MP3::Tag, available on CPAN with more information <a href="http://search.cpan.org/~ilyaz/MP3-Tag-1.12/lib/MP3/Tag.pm">here</a>.</p>
<p>The following example shows how easy it is to retrieve tags from an MP3 file.</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> MP3<span style="color: #339933;">::</span><span style="color: #006600;">Tag</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$file</span> <span style="color: #339933;">=</span> MP3<span style="color: #339933;">::</span><span style="color: #006600;">Tag</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$title</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$track</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$artist</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$album</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$comment</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$year</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$genre</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$file</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">autoinfo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Title: [$title]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Artist: [$artist]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Album: [$album]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Track: [$track]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Year: [$year]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Genre: [$genre]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Comment: [$comment]<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://andrewbeaton.net/faq/2009/12/03/read-mp3-id3-tags-in-perl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add a space in XSL</title>
		<link>http://andrewbeaton.net/faq/2009/11/13/add-a-space-in-xsl/</link>
		<comments>http://andrewbeaton.net/faq/2009/11/13/add-a-space-in-xsl/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 23:35:52 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[XSL]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://andrewbeaton.com/faq/?p=52</guid>
		<description><![CDATA[The following shows you how to add a space or &#160; in XSL:

&#60;xsl:text disable-output-escaping=&#34;yes&#34;&#62;&#38;nbsp;&#60;/xsl:text&#62;

]]></description>
			<content:encoded><![CDATA[<p>The following shows you how to add a space or &nbsp; in XSL:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:text</span> <span style="color: #000066;">disable-output-escaping</span>=<span style="color: #ff0000;">&quot;yes&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://andrewbeaton.net/faq/2009/11/13/add-a-space-in-xsl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Specifically target each browser in CSS</title>
		<link>http://andrewbeaton.net/faq/2009/11/13/specifically-target-each-browser-in-css/</link>
		<comments>http://andrewbeaton.net/faq/2009/11/13/specifically-target-each-browser-in-css/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 23:31:33 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[IE7]]></category>
		<category><![CDATA[IE8]]></category>
		<category><![CDATA[Internet Explorer]]></category>

		<guid isPermaLink="false">http://andrewbeaton.com/faq/?p=49</guid>
		<description><![CDATA[The following CSS &#8216;hack&#8217; can be used to target all browsers IE 7, IE 6 and IE < 6 ...
All browsers:

margin-top: 44px;

IE7 and below:

*margin-top: 26px;

IE6 and below:

_margin-top: 44px;

IE6 only:

_mar\gin-top: 44px;

All firefox:

#selector&#91;id=selector&#93; &#123; color: red; &#125;

All firefox:

@-moz-document url-prefix() { .selector { color: red; } }

All Gecko:

*&#62;.selector &#123; color: red; &#125;

Firefox 1.5 and newer:

.selector, x:-moz-any-link, x:only-child &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>The following CSS &#8216;hack&#8217; can be used to target all browsers IE 7, IE 6 and IE < 6 ...</p>
<p>All browsers:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">44px</span><span style="color: #00AA00;">;</span></pre></div></div>

<p>IE7 and below:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #00AA00;">*</span><span style="color: #000000; font-weight: bold;">margin-top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">26px</span><span style="color: #00AA00;">;</span></pre></div></div>

<p>IE6 and below:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">_margin-<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">44px</span><span style="color: #00AA00;">;</span></pre></div></div>

<p>IE6 only:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">_mar\gin-<span style="color: #000000; font-weight: bold;">top</span><span style="color: #00AA00;">:</span> <span style="color: #933;">44px</span><span style="color: #00AA00;">;</span></pre></div></div>

<p>All firefox:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#selector</span><span style="color: #00AA00;">&#91;</span>id<span style="color: #00AA00;">=</span>selector<span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>All firefox:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #a1a100;">@-moz-document url-prefix() { .selector { color: red; } }</span></pre></div></div>

<p>All Gecko:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #00AA00;">*&gt;</span><span style="color: #6666ff;">.selector</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Firefox 1.5 and newer:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.selector</span><span style="color: #00AA00;">,</span> x<span style="color: #3333ff;">:-moz-any-link</span><span style="color: #00AA00;">,</span> x<span style="color: #3333ff;">:only-child </span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Firefox 2 and older:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">body<span style="color: #3333ff;">:empty </span><span style="color: #6666ff;">.selector</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Firefox 2 and older:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #cc00cc;">#selector</span><span style="color: #00AA00;">&#91;</span>id<span style="color: #00AA00;">=</span>SELECTOR<span style="color: #00AA00;">&#93;</span> <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Firefox 2 and older:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">html<span style="color: #00AA00;">&gt;</span><span style="color: #808080; font-style: italic;">/**/</span>body <span style="color: #6666ff;">.selector</span><span style="color: #00AA00;">,</span> x<span style="color: #3333ff;">:-moz-any-link </span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>Firefox 3:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">html<span style="color: #00AA00;">&gt;</span><span style="color: #808080; font-style: italic;">/**/</span>body <span style="color: #6666ff;">.selector</span><span style="color: #00AA00;">,</span> x<span style="color: #3333ff;">:-moz-any-link</span><span style="color: #00AA00;">,</span> x<span style="color: #3333ff;">:<span style="color: #993333;">default</span> </span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://andrewbeaton.net/faq/2009/11/13/specifically-target-each-browser-in-css/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use a button as an A Href link</title>
		<link>http://andrewbeaton.net/faq/2009/11/13/use-a-button-as-an-a-href-link/</link>
		<comments>http://andrewbeaton.net/faq/2009/11/13/use-a-button-as-an-a-href-link/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 23:06:42 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[a href]]></category>
		<category><![CDATA[Button]]></category>

		<guid isPermaLink="false">http://andrewbeaton.com/faq/?p=33</guid>
		<description><![CDATA[To create an A Href link that can be used as a button, we can use one of the two following methods:

function SubmitFrom&#40;&#41;&#123;
    document.myform.target.value=&#34;somescript.php3&#34;
    document.myfrom.submit&#40;&#41;
&#125;
function ResetFrom&#40;&#41;&#123;
    document.myfrom.reset&#40;&#41;
&#125;
&#160;
&#60;a href=&#34;javascript:SubmitForm()&#34;&#62;&#60;img src=....&#62;&#60;/a&#62;

or

&#60;FORM name=remove ACTION='removeItem.php3' METHOD='POST' ENCTYPE='application/x-
www-form-urlencoded'&#62; 
    &#60;a href=&#34;javascript:document.remove.submit()&#34;&#62;remove from cart&#60;/a&#62;
&#60;/form&#62;

]]></description>
			<content:encoded><![CDATA[<p>To create an A Href link that can be used as a button, we can use one of the two following methods:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> SubmitFrom<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    document.<span style="color: #660066;">myform</span>.<span style="color: #660066;">target</span>.<span style="color: #660066;">value</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;somescript.php3&quot;</span>
    document.<span style="color: #660066;">myfrom</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #003366; font-weight: bold;">function</span> ResetFrom<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    document.<span style="color: #660066;">myfrom</span>.<span style="color: #660066;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;javascript:SubmitForm()&quot;</span><span style="color: #339933;">&gt;&lt;</span>img src<span style="color: #339933;">=</span>....<span style="color: #339933;">&gt;&lt;/</span>a<span style="color: #339933;">&gt;</span></pre></div></div>

<p>or</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">FORM</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span>remove <span style="color: #000066;">ACTION</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'removeItem.php3'</span> <span style="color: #000066;">METHOD</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'POST'</span> <span style="color: #000066;">ENCTYPE</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'application/x-</span>
<span style="color: #009900;">www-form-urlencoded'</span>&gt;</span> 
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;javascript:document.remove.submit()&quot;</span>&gt;</span>remove from cart<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://andrewbeaton.net/faq/2009/11/13/use-a-button-as-an-a-href-link/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Run only one instance of a program in C#</title>
		<link>http://andrewbeaton.net/faq/2009/11/13/run-only-one-instance-of-a-program-in-csharp/</link>
		<comments>http://andrewbeaton.net/faq/2009/11/13/run-only-one-instance-of-a-program-in-csharp/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 22:45:22 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Mutex]]></category>

		<guid isPermaLink="false">http://andrewbeaton.com/faq/?p=15</guid>
		<description><![CDATA[This example shows you how to ensure that only one instance of an application is running.

// Ensure only one instance of this application is running.
bool alone = false;
Mutex m = new Mutex&#40;true, &#34;&#60;name&#62;&#34;, out alone&#41;;
&#160;
if &#40;!alone&#41;
&#123;
    Console.WriteLine&#40;&#34;Another instance is already running.&#34;&#41;;
    Console.ReadKey&#40;&#41;;
    return;
&#125;
&#160;
Console.WriteLine&#40;&#34;Running...\n&#34;&#41;;
Console.ReadKey&#40;&#41;;
&#160;
// Needed to ensure [...]]]></description>
			<content:encoded><![CDATA[<p>This example shows you how to ensure that only one instance of an application is running.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Ensure only one instance of this application is running.</span>
<span style="color: #FF0000;">bool</span> alone <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span>
Mutex m <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Mutex<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span>, <span style="color: #666666;">&quot;&lt;name&gt;&quot;</span>, <span style="color: #0600FF;">out</span> alone<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>alone<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Another instance is already running.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Console.<span style="color: #0000FF;">ReadKey</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    return<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Running...<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Console.<span style="color: #0000FF;">ReadKey</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Needed to ensure only one instance is running.</span>
GC.<span style="color: #0000FF;">KeepAlive</span><span style="color: #000000;">&#40;</span>m<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://andrewbeaton.net/faq/2009/11/13/run-only-one-instance-of-a-program-in-csharp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Restart a Windows service using C#</title>
		<link>http://andrewbeaton.net/faq/2009/11/13/restart-a-windows-service-using-csharp/</link>
		<comments>http://andrewbeaton.net/faq/2009/11/13/restart-a-windows-service-using-csharp/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 22:38:05 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[ServiceController]]></category>
		<category><![CDATA[ServiceControllerStatus]]></category>
		<category><![CDATA[services]]></category>
		<category><![CDATA[TimeSpan]]></category>

		<guid isPermaLink="false">http://andrewbeaton.com/faq/?p=11</guid>
		<description><![CDATA[This example shows how to start, stop and restart a windows service programmatically in C#. 
Start service
The following method tries to start a service specified by a service name. Then it waits until the service is running or a timeout occurs.

public static void StartService&#40;string serviceName, int timeoutMilliseconds&#41;
&#123;
    ServiceController service = new ServiceController&#40;serviceName&#41;;
 [...]]]></description>
			<content:encoded><![CDATA[<p>This example shows how to start, stop and restart a windows service programmatically in C#. </p>
<p><strong>Start service</strong></p>
<p>The following method tries to start a service specified by a service name. Then it waits until the service is running or a timeout occurs.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> StartService<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> serviceName, <span style="color: #FF0000;">int</span> timeoutMilliseconds<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    ServiceController service <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ServiceController<span style="color: #000000;">&#40;</span>serviceName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">try</span>
    <span style="color: #000000;">&#123;</span>
        TimeSpan timeout <span style="color: #008000;">=</span> TimeSpan.<span style="color: #0000FF;">FromMilliseconds</span><span style="color: #000000;">&#40;</span>timeoutMilliseconds<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        service.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        service.<span style="color: #0000FF;">WaitForStatus</span><span style="color: #000000;">&#40;</span>ServiceControllerStatus.<span style="color: #0000FF;">Running</span>, timeout<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">catch</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// ...</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong>Stop service</strong></p>
<p>The following method tries to stop the specified service and it waits until the service is stopped or a timeout occurs.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> StopService<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> serviceName, <span style="color: #FF0000;">int</span> timeoutMilliseconds<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    ServiceController service <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ServiceController<span style="color: #000000;">&#40;</span>serviceName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">try</span>
    <span style="color: #000000;">&#123;</span>
        TimeSpan timeout <span style="color: #008000;">=</span> TimeSpan.<span style="color: #0000FF;">FromMilliseconds</span><span style="color: #000000;">&#40;</span>timeoutMilliseconds<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        service.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        service.<span style="color: #0000FF;">WaitForStatus</span><span style="color: #000000;">&#40;</span>ServiceControllerStatus.<span style="color: #0000FF;">Stopped</span>, timeout<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">catch</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// ...</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p><strong>Restart service</strong></p>
<p>This method combinates both previous methods. It tries to stop the service (and waits until it&#8217;s stopped) then it begins to start the service (and waits until the service is running). The specified timeout is used for both operations together.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> RestartService<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> serviceName, <span style="color: #FF0000;">int</span> timeoutMilliseconds<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    ServiceController service <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ServiceController<span style="color: #000000;">&#40;</span>serviceName<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">try</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">int</span> millisec1 <span style="color: #008000;">=</span> Environment.<span style="color: #0000FF;">TickCount</span><span style="color: #008000;">;</span>
        TimeSpan timeout <span style="color: #008000;">=</span> TimeSpan.<span style="color: #0000FF;">FromMilliseconds</span><span style="color: #000000;">&#40;</span>timeoutMilliseconds<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        service.<span style="color: #0000FF;">Stop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        service.<span style="color: #0000FF;">WaitForStatus</span><span style="color: #000000;">&#40;</span>ServiceControllerStatus.<span style="color: #0000FF;">Stopped</span>, timeout<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// count the rest of the timeout</span>
        <span style="color: #FF0000;">int</span> millisec2 <span style="color: #008000;">=</span> Environment.<span style="color: #0000FF;">TickCount</span><span style="color: #008000;">;</span>
        timeout <span style="color: #008000;">=</span> TimeSpan.<span style="color: #0000FF;">FromMilliseconds</span><span style="color: #000000;">&#40;</span>timeoutMilliseconds <span style="color: #008000;">-</span> <span style="color: #000000;">&#40;</span>millisec2<span style="color: #008000;">-</span>millisec1<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        service.<span style="color: #0000FF;">Start</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        service.<span style="color: #0000FF;">WaitForStatus</span><span style="color: #000000;">&#40;</span>ServiceControllerStatus.<span style="color: #0000FF;">Running</span>, timeout<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #000000;">&#125;</span>
   <span style="color: #0600FF;">catch</span>
   <span style="color: #000000;">&#123;</span>
       <span style="color: #008080; font-style: italic;">// ...</span>
   <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://andrewbeaton.net/faq/2009/11/13/restart-a-windows-service-using-csharp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Read source of a web page in C#</title>
		<link>http://andrewbeaton.net/faq/2009/11/13/read-source-of-a-web-page-in-csharp/</link>
		<comments>http://andrewbeaton.net/faq/2009/11/13/read-source-of-a-web-page-in-csharp/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 22:31:03 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[source]]></category>
		<category><![CDATA[WebRequest]]></category>
		<category><![CDATA[WebResponse]]></category>

		<guid isPermaLink="false">http://andrewbeaton.com/faq/?p=3</guid>
		<description><![CDATA[This example shows how to read the source of a web page in C#.

// used to build entire input
StringBuilder sb = new StringBuilder&#40;&#41;;
&#160;
// used on each read operation
byte&#91;&#93; buf = new byte&#91;8192&#93;;
&#160;
// prepare the web page we will be asking for
WebRequest request  = WebRequest.Create&#40;&#34;http://andrewbeaton.com&#34;&#41;;
&#160;
// execute the request
WebResponse response = request.GetResponse&#40;&#41;;
&#160;
// we will read data via [...]]]></description>
			<content:encoded><![CDATA[<p>This example shows how to read the source of a web page in C#.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// used to build entire input</span>
StringBuilder sb <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// used on each read operation</span>
<span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> buf <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">8192</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// prepare the web page we will be asking for</span>
WebRequest request  <span style="color: #008000;">=</span> WebRequest.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;http://andrewbeaton.com&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// execute the request</span>
WebResponse response <span style="color: #008000;">=</span> request.<span style="color: #0000FF;">GetResponse</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// we will read data via the response stream</span>
Stream resStream <span style="color: #008000;">=</span> response.<span style="color: #0000FF;">GetResponseStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #FF0000;">string</span> tempString <span style="color: #008000;">=</span> null<span style="color: #008000;">;</span>
<span style="color: #FF0000;">int</span> count <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">do</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// fill the buffer with data</span>
    count <span style="color: #008000;">=</span> resStream.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>buf, <span style="color: #FF0000;">0</span>, buf.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// make sure we read some data</span>
    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>count <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> 
    <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// translate from bytes to ASCII text</span>
        tempString <span style="color: #008000;">=</span> Encoding.<span style="color: #0000FF;">ASCII</span>.<span style="color: #0000FF;">GetString</span><span style="color: #000000;">&#40;</span>buf, <span style="color: #FF0000;">0</span>, count<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// continue building the string</span>
       sb.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>tempString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>count <span style="color: #008000;">&amp;</span>gt<span style="color: #008000;">;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://andrewbeaton.net/faq/2009/11/13/read-source-of-a-web-page-in-csharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
