Archive for Novembre 29th, 2007

Check for Mobile Devices

CodeKeep C# Feed Novembre 29th, 2007

Description: Check for Mobile Devices

Link: http://www.codekeep.net/snippets/645b1297-ac91-4060-9ef9-c09369a8befc.aspx


<add key="MobileDevices" value="(nokia|sonyericsson|blackberry|samsung|sec-|windows ce|motorola|mot-|up.b)" />

Then I wrote a property called IsMobile which tries the Request.Browser.IsMobileDevice property first, and then tries the regular expression afterwards.

private static readonly Regex MOBILE_REGEX = new Regex(ConfigurationManager.AppSettings.Get("MobileDevices"), RegexOptions.IgnoreCase | RegexOptions.Compiled);

 

public static bool IsMobile

{

  get

  {

    HttpContext context = HttpContext.Current;

    if (context != null)

    {

      HttpRequest request = context.Request;

      if (request.Browser.IsMobileDevice)

        return true;

 

      if (!string.IsNullOrEmpty(request.UserAgent) && MOBILE_REGEX.IsMatch(request.UserAgent))

        return true;

    }

 

    return false;

  }

}

Path - Application.StartupPath (Windows Form)

CodeKeep C# Feed Novembre 29th, 2007

Description: Where the application starts

Link: http://www.codekeep.net/snippets/c1eb9568-de92-44f8-8496-fdbecf5cd7cd.aspx

string printLabel = Application.StartupPath + @"\Resources\";