Thursday, 31 July 2008

R2-D2 console application

This is the best application I've ever made - but absolutely pointless!

using System;

class Program
{
static void Main( string[] args )
{
Random random = new Random( DateTime.UtcNow.Millisecond );

do
{
Console.Beep( random.Next( 37, 32767 ), 50 );
}
while ( !System.Console.KeyAvailable );
}
}

Tuesday, 22 July 2008

How do I have a public get and a private set for a property in C#.NET?

using System;

public class Person
{
private string name;

public string Name
{
get { return name; }
private set { name = value; }
}

public Person( string name )
{
Name = name;
}
}

You're probably thinking that the constructor could simply access the private member variable but I prefer to use properties for any interaction. It also means that you don't have to use the 'this' keyword if members and parameters have the same name:

this.name = name;

Thursday, 17 July 2008

Windows Vista Areo theme stopped working!

Make sure the theme service is still running:

  • Start > Run > 'services.msc'
  • Scroll down the list and find 'Theme'
  • Restart it by clicking the link on the left

If this doesn't bring Aero back then you'll need to re-select the theme:

  • Right click on the desktop and choose 'Personalize'
  • Choose 'Theme'
  • From the drop down list, click 'Browse' and go to 'C:\Windows\Resources\Themes'
  • Choose 'aero.theme'

You should be back in business!

Friday, 11 July 2008

Robots META tag

The Robots META tag can be used to control how robots function when they crawl specific pages. The most common reason for adding this tag would be to prevent robots indexing and/or following any links on the page.

Example:

<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">

The following values can be added the CONTENT attribute:

  • INDEX
  • NOINDEX
  • FOLLOW
  • NOFOLLOW

Multiple comma-separated values are allowed and the default is "INDEX, FOLLOW" (if no Robots META tag is supplied).

Remaining combinations:

<META NAME="ROBOTS" CONTENT="INDEX, FOLLOW">

<META NAME="ROBOTS" CONTENT="INDEX, NOFOLLOW">

<META NAME="ROBOTS" CONTENT="NOINDEX, FOLLOW">

Refresh META tag

A Refresh META tells the browser to refresh or redirect after a certain interval.

These examples show how to refresh the current content after 5 seconds, redirect to another URL after 5 seconds and redirect to another URL immediately respectively:

<META HTTP-EQUIV="REFRESH" CONTENT="5">

<META HTTP-EQUIV="REFRESH" CONTENT="5;url=http://andrewgunn.blogspot.com/">

<META HTTP-EQUIV="REFRESH" CONTENT="0;url=http://andrewgunn.blogspot.com/">

Wednesday, 2 July 2008

How do I validate a string Guid in C#.NET?

using System;
using System.Text.RegularExpressions;

public static class GuidUtility
{
public static bool IsGuid( string guid )
{
if ( !string.IsNullOrEmpty( guid ) )
{
Regex regex = new Regex( @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" );

return regex.IsMatch( guid );
}

return false;
}
}

Tuesday, 1 July 2008

How do I convert a Unix timestamp into a DateTime object in C#.NET?

Unix time, or POSIX time, is the number of seconds elapsed since midnight of January 1, 1970 (UTC).

Here is a utility class that can be used to convert a Unix timestamp into a DateTime object and vice versa:

using System;

public static class DateTimeUtility
{
public static DateTime UnixOrigin
{
get { return new DateTime( 1970, 1, 1, 0, 0, 0, 0 ); }
}

public static DateTime ConvertFromUnixTimestamp( long timestamp )
{
return UnixOrigin.AddSeconds( (long)timestamp );
}

public static long ConvertToUnixTimestamp( DateTime dateTime )
{
TimeSpan differnce = dateTime - UnixOrigin;

return (long)differnce.TotalSeconds;
}
}