How can I detect Windows 7 (versions) in .net?
What code can I use?
How can I detect Windows 7 (versions) in .net?
What code can I use?
How to determine the Windows version by using Visual C# at http://support.microsoft.com/kb/304283
via Environment.OSVersion
which "Gets an System.OperatingSystem object that contains the current platform identifier and version number."
System.Environment.OSVersion
has the information you need. It consists of three components which map to the following Windows versions:
+----------------------------------------------------------------------------------------------+
| |Windows|Windows|Windows|Windows NT|Windows|Windows|Windows|Windows|Windows|Windows|
| | 95 | 98 | Me | 4.0 | 2000 | XP | 2003 | Vista | 2008 | 7 |
+----------------------------------------------------------------------------------------------+
|PlatformID | 1 | 1 | 1 | 2 | 2 | 2 | 2 | 2 | 2 | 2 |
+----------------------------------------------------------------------------------------------+
|Major | | | | | | | | | | |
| version | 4 | 4 | 4 | 4 | 5 | 5 | 5 | 6 | 6 | 6 |
+----------------------------------------------------------------------------------------------+
|Minor | | | | | | | | | | |
| version | 0 | 10 | 90 | 0 | 0 | 1 | 2 | 0 | 1 | 2 |
+----------------------------------------------------------------------------------------------+
EDIT: updated with more versions from this link
I used this when I had to determine various Microsoft Operating System versions:
string getOSInfo()
{
//Get Operating system information.
OperatingSystem os = Environment.OSVersion;
//Get version information about the os.
Version vs = os.Version;
//Variable to hold our return value
string operatingSystem = "";
if (os.Platform == PlatformID.Win32Windows)
{
//This is a pre-NT version of Windows
switch (vs.Minor)
{
case 0:
operatingSystem = "95";
break;
case 10:
if (vs.Revision.ToString() == "2222A")
operatingSystem = "98SE";
else
operatingSystem = "98";
break;
case 90:
operatingSystem = "Me";
break;
default:
break;
}
}
else if (os.Platform == PlatformID.Win32NT)
{
switch (vs.Major)
{
case 3:
operatingSystem = "NT 3.51";
break;
case 4:
operatingSystem = "NT 4.0";
break;
case 5:
if (vs.Minor == 0)
operatingSystem = "2000";
else
operatingSystem = "XP";
break;
case 6:
if (vs.Minor == 0)
operatingSystem = "Vista";
else
operatingSystem = "7";
break;
default:
break;
}
}
//Make sure we actually got something in our OS check
//We don't want to just return " Service Pack 2" or " 32-bit"
//That information is useless without the OS version.
if (operatingSystem != "")
{
//Got something. Let's prepend "Windows" and get more info.
operatingSystem = "Windows " + operatingSystem;
//See if there's a service pack installed.
if (os.ServicePack != "")
{
//Append it to the OS name. i.e. "Windows XP Service Pack 3"
operatingSystem += " " + os.ServicePack;
}
//Append the OS architecture. i.e. "Windows XP Service Pack 3 32-bit"
//operatingSystem += " " + getOSArchitecture().ToString() + "-bit";
}
//Return the information we've gathered.
return operatingSystem;
}
Source: here
Like always - you use System.Environment.OSVersion
(see TechNet - How to determine the Windows version by using Visual C#):
var osInfo = System.Environment.OSVersion;
// see osInfo.Version
http://msdn.microsoft.com/en-us/library/ms724358.aspx Please check this information
Whenever someone asks "How?" I always tend to think of "Why?"
If you're detecting Windows 7 to use Windows 7 specific features, consider using the Windows® API Code Pack for Microsoft® .NET Framework library instead.
one way:
public string GetOSVersion()
{
int _MajorVersion = Environment.OSVersion.Version.Major;
switch (_MajorVersion) {
case 5:
return "Windows XP";
case 6:
switch (Environment.OSVersion.Version.Minor) {
case 0:
return "Windows Vista";
case 1:
return "Windows 7";
default:
return "Windows Vista & above";
}
break;
default:
return "Unknown";
}
}
Then simply do wrap a select case around the function..
Like R. Bemrose suggested, if you are doing Windows 7 specific features, you should look at the Windows® API Code Pack for Microsoft® .NET Framework.
It contains a CoreHelpers
class that let you determine the OS you are currently on (XP and above only, its a requirement for .NET nowaday)
It also provide multiple helper methods. For example, suppose that you want to use the jump list of Windows 7, there is a class TaskbarManager
that provide a property called IsPlatformSupported
and it will return true if you are on Windows 7 and above.