Sometimes, 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:
namespace System
{
public static class Environment
{
public static string CurrentDirectory { get; set; }
public static bool HasShutdownStarted { get; }
public static string NewLine { get; }
public static OperatingSystem OSVersion { get; }
public static int ProcessorCount { get; }
public static int TickCount { get; }
public static Version Version { get; }
[SecurityCritical]
public static string GetFolderPath(Environment.SpecialFolder folder);
public enum SpecialFolder
{
Programs = 2,
Personal = 5,
Startup = 7,
StartMenu = 11,
Favorites = 22,
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 TextBlock
s 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:
Let’s see what we did in the code behind file. There, we will have DependencyProperty
as mentioned below:
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:
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:
<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.
CodeProject