Click here to Skip to main content
15,881,898 members
Articles / Programming Languages / C#

Detecting System Information of WP7 Device

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
2 Dec 2011CPOL2 min read 11.3K   2  
Detecting System Information of WP7 Device

imageSometimes, we want to show the System Information to the user, whether it is an application or a simple Game. We need to show those system specific information to the Windows Phone 7 user by using a System page. So, in such a case, how will you fetch that information and show it to the user?

Well, this post will help you to understand it and let you know about this data in Windows Phone 7 device. Read to learn more about this topic.

Know about System.Environment

Under the System namespace, we have a class called “Environment” which has different properties to return information about the Windows Phone 7 device. The exposed APIs are as mentioned below. Read the comments section to know more about those here:

C#
namespace System
{
    // Summary:
    //     Provides information about, and means to manipulate, the current environment
    //     and platform. This class cannot be inherited.
    public static class Environment
    {
        // Summary:
        //     When called by trusted applications, gets the fully qualified path of the
        //     current working directory.
        public static string CurrentDirectory { get; set; }
 
        // Summary:
        //     Gets a value indicating whether the common language runtime is shutting down
        //     or the current application domain is unloading.
        public static bool HasShutdownStarted { get; }
 
        // Summary:
        //     Gets the newline string defined for this environment.
        public static string NewLine { get; }
 
        // Summary:
        //     Gets an System.OperatingSystem object that contains the current platform
        //     identifier and version number.
        public static OperatingSystem OSVersion { get; }
 
        // Summary:
        //     Gets the number of processors on the current machine.
        public static int ProcessorCount { get; }
 
        // Summary:
        //     Gets the number of milliseconds elapsed since the system started.
        public static int TickCount { get; }
 
        // Summary:
        //     Gets a System.Version object that describes the major, minor, build, and
        //     revision numbers of the common language runtime.
        public static Version Version { get; }
 
        // Summary:
        //     Gets the path to the system special folder identified by the specified 
        //     enumeration.

        [SecurityCritical]
        public static string GetFolderPath(Environment.SpecialFolder folder);
 
        // Summary:
        //     When it is called by trusted applications, specifies enumerated constants
        //     used to retrieve directory paths to system special folders.
        public enum SpecialFolder
        {
            // Summary: The directory that contains the user's program groups.
            Programs = 2,
 
            // Summary: The directory that serves as a common repository for documents.
            Personal = 5,
 
            // Summary: The directory that corresponds to the user's Startup program group.
            Startup = 7,
 
            // Summary: The directory that contains the Start menu items.
            StartMenu = 11,
 
            // Summary: The directory that serves as a common repository for the user's 
            // favorite items.

            Favorites = 22,
 
            // Summary: The directory that serves as a common repository for 
            // application-specific data for the current roaming user.

           ApplicationData = 26,
        }
    }
}

Hope you got a basic idea about the class and its various properties. Let’s create a sample demo to discuss a few of them. We will expose our own properties to show the device information in the Phone UI.

Demonstration

Let us first design the UI as shown below (Figure 1) where we will have a Content Grid having two columns and multiple rows. The first column will have labels of the property that we will show at the second column. After that, we will expose some custom dependency properties from the code behind page or ViewModel (if you are following the MVVM pattern) which we will bind to the TextBlocks present in the XAML container. Once you run the application, you will see the proper information in the UI as shown in Figure 2 below:

System.Environment Information in Windows Phone 7 - DesignSystem.Environment Information in Windows Phone 7

Let’s see what we did in the code behind file. There, we will have DependencyProperty as mentioned below:

C#
public int TickCount
{
    get { return (int)GetValue(TickCountProperty); }
    set { SetValue(TickCountProperty, value); }
}
 
public string CLRVersion
{
    get { return (string)GetValue(CLRVersionProperty); }
    set { SetValue(CLRVersionProperty, value); }
}
 
public string OSVersion
{
    get { return (string)GetValue(OSVersionProperty); }
    set { SetValue(OSVersionProperty, value); }
}
 
public string CurrentDirectory
{
    get { return (string)GetValue(CurrentDirectoryProperty); }
    set { SetValue(CurrentDirectoryProperty, value); }
}

In the constructor, we will populate all these properties from the System.Environment class. Here is the code snippet of that:

C#
OSVersion = System.Environment.OSVersion.ToString();
CLRVersion = System.Environment.Version.ToString();
TickCount = System.Environment.TickCount;

Once our back end code is ready, we need to create the UI and bind proper data to the UI elements. Here is our XAML code in case you need a reference:

XML
<TextBlock Text="OS Version" Grid.Row="1" Grid.Column="0" Margin="5"/>
<TextBlock Text="{Binding OSVersion, ElementName=phonePage}" 
           Grid.Row="1" Grid.Column="1" Margin="5" TextWrapping="Wrap"/>
 
<TextBlock Text="CLR Version" Grid.Row="2" Grid.Column="0" Margin="5"/>
<TextBlock Text="{Binding CLRVersion, ElementName=phonePage}" 
           Grid.Row="2" Grid.Column="1" Margin="5"/>
 
<TextBlock Text="Tick Count" Grid.Row="3" Grid.Column="0" Margin="5"/>
<TextBlock Text="{Binding TickCount, ElementName=phonePage}" 
           Grid.Row="3" Grid.Column="1" Margin="5"/>

That’s all from the coding part. Now build and run the application. You will see proper data populated in the phone screen.

Hope this post was helpful to understand the basic concept of the System Environment information. Now you will be able to easily fetch the system information of the device.

This article was originally posted at http://www.kunal-chowdhury.com/feeds/posts/default

License

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


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead 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 app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
-- There are no messages in this forum --