Click here to Skip to main content
13,458,003 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 12 Aug 2011

How Can You Detect Browser Information in Silverlight?

, 12 Aug 2011
Rate this:
Please Sign up or sign in to vote.
Read this post to know how to detect the browser information using the HtmlPage.BrowserInformation property.

Sometimes, we need to detect the browser information of the user's computer where our Silverlight application is running. We can do this using JavaScript, but sometimes it is useful to detect it from our Silverlight application. So, how to do it? Let us discuss this with a small simple example.

Read this post to know how to detect the browser information using the HtmlPage.BrowserInformation property.

Know about HtmlPage.BrowserInformation

Detecting browser information is not much difficult. You can use the inbuilt HtmlPage class available in the namespace called System.Windows.Browser. The class has a static property called BrowserInformation of type BrowserInformation. This returns the following properties:

public sealed class BrowserInformation
    public string ProductName { get; }
    public string ProductVersion { get; }
    public string Name { get; }
    public Version BrowserVersion { get; }
    public bool CookiesEnabled { get; }
    public string Platform { get; }
    public string UserAgent { get; }

BrowserInformation class is a sealed class which returns browser name, product name, product version, browser version, platform, user agent and whether the browser has cookies enabled.

Implementing Code

Let us start with the code implementation. Let's create two properties in code behind of the page. We will create string properties called "Platform" and "BrowserInformation". Here is the code for your reference:

public string Platform
    get { return (string)GetValue(PlatformProperty); }
    set { SetValue(PlatformProperty, value); }

public static readonly DependencyProperty PlatformProperty =
    DependencyProperty.Register("Platform", typeof(string), 
    typeof(MainPage), new PropertyMetadata(string.Empty));
public string BrowserInformation
    get { return (string)GetValue(BrowserInformationProperty); }
    set { SetValue(BrowserInformationProperty, value); }

public static readonly DependencyProperty BrowserInformationProperty =
    typeof(string), typeof(MainPage), new PropertyMetadata(string.Empty));

Now populate these properties from the code behind by extracting the proper property values from the HtmlPage.BrowserInformation property.

Here is the code that we are going to use for the demonstration:

var browserInfo = HtmlPage.BrowserInformation;
BrowserInformation = "You are using " + browserInfo.Name + " 
		(Product Name: " + browserInfo.ProductName +
                     " - " + browserInfo.ProductVersion + ") Version: " +
Platform = "You are on " + browserInfo.Platform + " 
platform and using User Agent: " + browserInfo.UserAgent;

Now design our XAML page with two TextBlocks binded with proper properties as shown below:

<StackPanel x:Name="LayoutRoot" 

Background="White" Width="400" 

            HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock Text="{Binding BrowserInformation, ElementName=userControl}" 

               TextWrapping="Wrap" Margin="5"/>
    <TextBlock Text="{Binding Platform, ElementName=userControl}"

               TextWrapping="Wrap" Margin="5"/>

That's all about our code implementation.

See it in Action

Build your project and run it in various browsers and you will see different information based on the running browser. I tested in various browsers, various versions and here is the report for each:

Tested in Internet Explorer 9

Tested in Firefox 5

Tested in Chrome 13

Tested in Chrome 14


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kunal Chowdhury «IN»
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Book authored:

Connect with me on:

My CodeProject Awards/Prizes:

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180323.1 | Last Updated 12 Aug 2011
Article Copyright 2011 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid