Detecting Windows and .NET version

A

Arek

Hi,

How to detect Windows version and .NET version? I mean I want to know if
user is using Windows 2000, XP, Vista and which Service Pack and version (XP
Home, XP Pro, Vista Home Basic, Vista Premium, etc.). Is there any easy way
to retrive this information?
I'm wondering if all modern Windows version (2000, XP, 2003, Vista) has .NET
installed by default or do I need to implement such a feature in Win32
instead of .NET?

Thanks,
Arek
 
C

Cowboy \(Gregory A. Beamer\)

//To find OS
OperatingSystem operatingSystem = Environment.OSVersion;
Version version = operatingSystem.Version;

You can then pull the following:

version.Major
version.Minor
version.Revision
version.Build

You just need to map the above to a Windows OS. Major/Minor is generally
enough to give you the OS, while Revision and Build are aimed more at
Service Releases. I am not sure how to differentiate between 32 bit and 64
bit, however, so a good Google is in order.

For .NET, the easiest way is the registry. While it is not C# (it is C++),
here is a detection script:
http://astebner.sts.winisp.net/Tools/detectFX.cpp.txt

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box!
|
*************************************************
 
A

Arne Vajhøj

Arek said:
How to detect Windows version and .NET version? I mean I want to know if
user is using Windows 2000, XP, Vista and which Service Pack and version
(XP Home, XP Pro, Vista Home Basic, Vista Premium, etc.). Is there any
easy way to retrive this information?

Environment.OSVersion and Environment.Version
I'm wondering if all modern Windows version (2000, XP, 2003, Vista) has
.NET installed by default or do I need to implement such a feature in
Win32 instead of .NET?

2000 and XP did not ship with .NET
2003 shipped with .NET 1.1
Vista shipped with .NET 3.0

..NET is available via Windows Update.

Runtimes are available for redistribution.

Unless you have special requirements, then I would suggest
you try and analyze whether you can succeed with a .NET 2.0/3.0/3.5
based solution.

Arne
 
W

Willy Denoyette [MVP]

Arek said:
Hi,

How to detect Windows version and .NET version? I mean I want to know if
user is using Windows 2000, XP, Vista and which Service Pack and version
(XP Home, XP Pro, Vista Home Basic, Vista Premium, etc.). Is there any
easy way to retrive this information?
I'm wondering if all modern Windows version (2000, XP, 2003, Vista) has
.NET installed by default or do I need to implement such a feature in
Win32 instead of .NET?

Thanks,
Arek

..NET comes with WS2003 (v1.x), Vista (v1.x, v2) (V3 optional feature) and
WS2008 (v1.x, v2 SP1) (v3 SP1 optional feature).
v1.x = v1.0 & v1.1
None come with v3.5.
Downlevel OSses come without .NET.

You can't run managed code if .NET is not installed, so there is no reliable
way to get at this using C# (or other managed code).

The easiest to get at this info is to run a simple (VB) script like this:

On Error Resume Next
' Get OS Version
For Each os in GetObject("winmgmts:").InstancesOf ("Win32_OperatingSystem")
WScript.Echo "OS = " & os.Caption & " ,Version = " & os.Version & "
,Service Pack = " & os.ServicePackMajorVersion
Next

'Get .NET Framework versions
const HKLM = &H80000002
Set
objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

regPath = "SOFTWARE\Microsoft\.NetFramework"
objReg.EnumKey HKLM, regPath, subKeys
If Err = 0 Then
For Each subkey In subKeys
if(Instr(1, subkey, "v")) Then
WScript.Echo subkey
End If
Next
Else
WScript.Echo ".NET not available"
End if

Willy.
 
W

Willy Denoyette [MVP]

Willy Denoyette said:
.NET comes with WS2003 (v1.x), Vista (v1.x, v2) (V3 optional feature) and
WS2008 (v1.x, v2 SP1) (v3 SP1 optional feature).
v1.x = v1.0 & v1.1
None come with v3.5.
Downlevel OSses come without .NET.

You can't run managed code if .NET is not installed, so there is no
reliable way to get at this using C# (or other managed code).

The easiest to get at this info is to run a simple (VB) script like this:

On Error Resume Next
' Get OS Version
For Each os in GetObject("winmgmts:").InstancesOf
("Win32_OperatingSystem")
WScript.Echo "OS = " & os.Caption & " ,Version = " & os.Version & "
,Service Pack = " & os.ServicePackMajorVersion
Next

'Get .NET Framework versions
const HKLM = &H80000002
Set
objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

regPath = "SOFTWARE\Microsoft\.NetFramework"
objReg.EnumKey HKLM, regPath, subKeys
If Err = 0 Then
For Each subkey In subKeys
if(Instr(1, subkey, "v")) Then
WScript.Echo subkey
End If
Next
Else
WScript.Echo ".NET not available"
End if

Willy.

Or better, use the .NET setup path in the HKLM hive.

'file: frameworkver.vbs
'
On Error Resume Next

'Get installed .NET Framework versions
const HKLM = &H80000002
const REG_SZ = 1
const REG_EXPAND_SZ = 2
const REG_BINARY = 3
const REG_DWORD = 4
const REG_MULTI_SZ = 7

Set registry =
GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

netRegPath = "SOFTWARE\Microsoft\NET Framework Setup\NDP"
'frameworkPath
dim sValue
registry.EnumKey HKLM, netRegPath, subKeys
If Err = 0 Then
For Each subkey In subKeys
WScript.Echo subkey
if(Instr(1, subkey, "v")) Then
frameworkPath = netRegPath & "\" & subkey
registry.EnumValues HKLM, frameworkPath, valueNames, valueTypes
For inx = 0 To UBound(valueNames)
Select Case valueTypes(inx)
Case REG_SZ
registry.GetStringValue HKLM, frameworkPath, valueNames(inx), sValue
WScript.Echo VbTab & valueNames(inx) &": " & sValue
Case REG_DWORD
registry.GetDWORDValue HKLM, frameworkPath, valueNames(inx), dValue
WScript.Echo VbTab & valueNames(inx) &": " & dValue
End Select
Next
End If
Next
Else
WScript.Echo ".NET not installed!?"
Err.Clear
End if

Willy.
 
J

Jon Skeet [C# MVP]

Arne Vajhøj said:
Environment.Verison must be easier.

That's fine if you only want to find out the *runtime* version. It
doesn't find out the version of .NET which is installed though.

For example, I've got .NET 3.5 installed, but Environment.Version
returns 2.0.50727.1434.
 
A

Arne Vajhøj

Jon said:
That's fine if you only want to find out the *runtime* version. It
doesn't find out the version of .NET which is installed though.

For example, I've got .NET 3.5 installed, but Environment.Version
returns 2.0.50727.1434.

That is a good point.

Which leads to an only partially related question: does 3.0 and
3.5 need to be installed or would it work with 2.0 if just the
relevant assemblies were distributed with the app ?

Arne
 
J

Jon Skeet [C# MVP]

That is a good point.

Which leads to an only partially related question: does 3.0 and
3.5 need to be installed or would it work with 2.0 if just the
relevant assemblies were distributed with the app ?

I wouldn't like to say exactly what the .NET installation does beyond
making the assemblies available - I think it's much safer to do the
proper installation.
 
W

Willy Denoyette [MVP]

That is a good point.

Which leads to an only partially related question: does 3.0 and
3.5 need to be installed or would it work with 2.0 if just the
relevant assemblies were distributed with the app ?

I wouldn't like to say exactly what the .NET installation does beyond
making the assemblies available - I think it's much safer to do the
proper installation.

That's right, you need to install the frameworks, here is some of the stuff
that gets done during the install:

- install assemblies in the GAC ,
- pre-jit and regasm,
- compile MOF files (see *.mof files),
- run sql scripts
- create/update registry entries (see *.reg files).

The registry entries that hold the installed framework attributes is
SOFTWARE\Microsoft\NET Framework Setup\NDP, which keeps track of the
installed version and properties like SP number, version number, install
state etc....
This entry is also used by WUS so it can keep track of the installed
updates.

Here is how you can get at the installed FW versions:

...
string fwSetup = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\";
string HKLM = Registry.LocalMachine.ToString() + "\\";
using(RegistryKey fwInstallKey =
Registry.LocalMachine.OpenSubKey(fwSetup))
{
foreach(string vers in fwInstallKey.GetSubKeyNames())
{
string fwVersion = (string)Registry.GetValue(HKLM +
fwSetup + vers, "Version", vers + " N/A");
Console.WriteLine(fwVersion);
}
}
....

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top