Yesterday I outlined how an end user can determine which version(s) of the .NET Compact Framework are installed on a device. Sometimes it can be useful to query which specific version of the framework an application is currently running on. This could be used for instance, in automated bug report forms to provide your support staff with an accurate picture of the environment your application is being utilised within.
Within an application you can simply query the Envrionment.Version property to determine the version of the CLR your application is running on, for example:
MessageBox.Show(Envrionment.Version.ToString(), "CLR Version Number");
The version property is an instance of the System.Version class. To check if your application is running on a specific version of the .NET Compact Framework you can construct an instance of the Version class to represent the desired version number (see yesterday’s entry for a table) and perform a simple comparision, as the following example demonstrates:
Version targetVersion = new Version("2.0.7045.0"); if (Envrionment.Version == targetVersion) MessageBox.Show("You are running .NET CF 2.0 SP1", "Status");