With SharePoint 2013, Microsoft has introduced a small monkey wrench in where solution files get deployed when mapped to the LAYOUTS (/_layouts) folder. Files in SharePoint 2007 and 2010 would simply be deployed under the /_layouts folder. With SharePoint 2013, however, these same files are now deployed under /_layouts/15. Not really a big deal if you are writing a solution only for SharePoint 2013. However, if you are writing a solution for multiple versions of SharePoint, you may need to identify the version of SharePoint running so that you can reference files in their correct location. This is just one scenario in which you may want to identify which version of SharePoint you are running.
To identify the current version of SharePoint, you simply need to use the SPFarm BuildVersion. For example:
var version = SPFarm.Local.BuildVersion;
// Set the label to the Major version (12, 14, 15, etc).
lblVersion.Text = version.Major.ToString()
The Major version can be used to identify the version of SharePoint (12 = 2007, 14 = 2010, 15 = 2013). In addition, you can use the minor and build version numbers to identify what updates have been applied to the farm.
That’s it. Hopefully you find it useful.