Thursday, May 26, 2011

URL shortener in .net using API

It’s simple to generate shorten url based on long url using API.

For this we need to have account with which is free. After sign up completed, you will be provided with an API key

This API key can be used to access the API. Below is the code for API class in c#

public static class BitlyApi
private const string apiKey = "[add api key here]"
private const string login = "[add login name here]"

public static BitlyResults ShortenUrl(string
var url =
HttpUtility.UrlEncode(longUrl), login, apiKey);
var resultXml = XDocument.Load(url);
var x = (from result
in resultXml.Descendants("nodeKeyVal"
UserHash = result.Element(
ShortUrl = result.Element(

public class
public string
UserHash { get; set; }

public string ShortUrl { get; set; }

Now, we can generate short url as below:

string strShortUrl = BitlyApi.ShortenUrl(“<long url>).ShortUrl;

Happy Coding…

Monday, May 23, 2011

Customizing Date format in WPF

The datepicker control in WPF allow short and Long formats. By default it will take system date format.

So we can change the date format as our application requires like dd/MM/yyyy or MM/dd/YYYY,…

Need to write some couple of lines under Application_Startup Event in App.XAML.

Below is the code for custom date format.

Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();

Thread.CurrentThread.CurrentCulture.DateTimeFormat.DateSeparator = "/";

Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";


This is event should be initiated from XAML code like below.


Happy Coding…