Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 38 additions & 26 deletions snippets/csharp/framework/migration-guide/versions-installed1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,73 @@ public static void Main()

private static void GetVersionFromRegistry()
{
// Opens the registry key for the .NET Framework entry.
using (RegistryKey ndpKey =
RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
// Opens the registry key for the .NET Framework entry.
using (RegistryKey ndpKey =
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).
OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
{
foreach (var versionKeyName in ndpKey.GetSubKeyNames())
{
// As an alternative, if you know the computers you will query are running .NET Framework 4.5
// or later, you can use:
// using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
// RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
foreach (string versionKeyName in ndpKey.GetSubKeyNames())
{
// Skip .NET Framework 4.5 version information.
if (versionKeyName == "v4")
{
continue;
}

if (versionKeyName.StartsWith("v"))
{

RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
string name = (string)versionKey.GetValue("Version", "");
string sp = versionKey.GetValue("SP", "").ToString();
string install = versionKey.GetValue("Install", "").ToString();
if (install == "") //no install info, must be later.
Console.WriteLine(versionKeyName + " " + name);
// Get the .NET Framework version value.
var name = (string)versionKey.GetValue("Version", "");
// Get the service pack (SP) number.
var sp = versionKey.GetValue("SP", "").ToString();

// Get the installation flag, or an empty string if there is none.
var install = versionKey.GetValue("Install", "").ToString();
if (string.IsNullOrEmpty(install)) // No install info; it must be in a child subkey.
Console.WriteLine($"{versionKeyName} {name}");
else
{
if (sp != "" && install == "1")
if (!(string.IsNullOrEmpty(sp)) && install == "1")
{
Console.WriteLine(versionKeyName + " " + name + " SP" + sp);
Console.WriteLine($"{versionKeyName} {name} SP{sp}");
}

}
if (name != "")
if (! string.IsNullOrEmpty(name))
{
continue;
}
foreach (string subKeyName in versionKey.GetSubKeyNames())
foreach (var subKeyName in versionKey.GetSubKeyNames())
{
RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
name = (string)subKey.GetValue("Version", "");
if (name != "")
if (! string.IsNullOrEmpty(name))
sp = subKey.GetValue("SP", "").ToString();

install = subKey.GetValue("Install", "").ToString();
if (install == "") //no install info, must be later.
Console.WriteLine(versionKeyName + " " + name);
if (string.IsNullOrEmpty(install)) //No install info; it must be later.
Console.WriteLine($"{versionKeyName} {name}");
else
{
if (sp != "" && install == "1")
if (!(string.IsNullOrEmpty(sp )) && install == "1")
{
Console.WriteLine(" " + subKeyName + " " + name + " SP" + sp);
Console.WriteLine($"{subKeyName} {name} SP{sp}");
}
else if (install == "1")
{
Console.WriteLine(" " + subKeyName + " " + name);
Console.WriteLine($" {subKeyName} {name}");
}
}
}
}
}
}
}
}
}
// The example displays output similar to the following:
// v2.0.50727 2.0.50727.4927 SP2
// v3.0 3.0.30729.4926 SP2
// v3.5 3.5.30729.4926 SP1
// v4.0
// Client 4.0.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ public static void Main()
Console.WriteLine($"Version: {Environment.Version}");
}
}
// The example displays output similiar to the following:'
// Version: 4.0.30319.18010
56 changes: 28 additions & 28 deletions snippets/csharp/framework/migration-guide/versions-installed3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,41 @@ private static void Get45PlusFromRegistry()
{
const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";

using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
{
if (ndpKey != null && ndpKey.GetValue("Release") != null) {
Console.WriteLine(".NET Framework Version: " + CheckFor45PlusVersion((int) ndpKey.GetValue("Release")));
}
else {
Console.WriteLine(".NET Framework Version 4.5 or later is not detected.");
}
}
}

// Checking the version using >= will enable forward compatibility.
private static string CheckFor45PlusVersion(int releaseKey)
{
if (releaseKey >= 461808)
return "4.7.2 or later";
if (releaseKey >= 461308)
return "4.7.1";
if (releaseKey >= 460798)
return "4.7";
if (releaseKey >= 394802)
return "4.6.2";
if (releaseKey >= 394254)
return "4.6.1";
if (releaseKey >= 393295)
return "4.6";
if (releaseKey >= 379893)
return "4.5.2";
if (releaseKey >= 378675)
return "4.5.1";
if (releaseKey >= 378389)
return "4.5";
// This code should never execute. A non-null release key should mean
// that 4.5 or later is installed.
return "No 4.5 or later version detected";
}

// Checking the version using >= enables forward compatibility.
string CheckFor45PlusVersion(int releaseKey)
{
if (releaseKey >= 461808)
return "4.7.2 or later";
if (releaseKey >= 461308)
return "4.7.1";
if (releaseKey >= 460798)
return "4.7";
if (releaseKey >= 394802)
return "4.6.2";
if (releaseKey >= 394254)
return "4.6.1";
if (releaseKey >= 393295)
return "4.6";
if (releaseKey >= 379893)
return "4.5.2";
if (releaseKey >= 378675)
return "4.5.1";
if (releaseKey >= 378389)
return "4.5";
// This code should never execute. A non-null release key should mean
// that 4.5 or later is installed.
return "No 4.5 or later version detected";
}
}
}
// This example displays output like the following:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,45 @@ Public Module VersionTest
Private Sub GetVersionFromRegistry()

' Opens the registry key for the .NET Framework entry.
Using ndpKey As RegistryKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ""). _
Using ndpKey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).
OpenSubKey("SOFTWARE\Microsoft\NET Framework Setup\NDP\")
' As an alternative, if you know the computers you will query are running .NET Framework 4.5
' or later, you can use:

' As an alternative, if you know the computers you will query are running .NET Framework 4.5
' or later, you can use:
' Using ndpKey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, _
' RegistryView.Registry32).OpenSubKey("SOFTWARE\Microsoft\NET Framework Setup\NDP\")
For Each versionKeyName In ndpKey.GetSubKeyNames()
' Skip .NET Framework 4.5 and later.
If versionKeyName = "v4" Then Continue For

For Each versionKeyName As String In ndpKey.GetSubKeyNames()
If versionKeyName.StartsWith("v") Then
Dim versionKey As RegistryKey = ndpKey.OpenSubKey(versionKeyName)
Dim name As String = DirectCast(versionKey.GetValue("Version", ""), String)
Dim sp As String = versionKey.GetValue("SP", "").ToString()
Dim install As String = versionKey.GetValue("Install", "").ToString()
If install = "" Then
'no install info, ust be later
Console.WriteLine(versionKeyName & " " & name)
' Get the .NET Framework version value.
Dim name = DirectCast(versionKey.GetValue("Version", ""), String)
' Get the service pack (SP) number.
Dim sp = versionKey.GetValue("SP", "").ToString()

Dim install = versionKey.GetValue("Install", "").ToString()
If String.IsNullOrEmpty(install) Then ' No install info; it must be in a child subkey.
Console.WriteLine($"{versionKeyName} {name}")
Else
If sp <> "" AndAlso install = "1" Then
Console.WriteLine(versionKeyName & " " & name & " SP" & sp)

If Not String.IsNullOrEmpty(sp) AndAlso install = "1" Then
Console.WriteLine($"{versionKeyName} {name} SP{sp}")
End If
End If
If name <> "" Then
If Not String.IsNullOrEmpty(name) Then
Continue For
End If
For Each subKeyName As String In versionKey.GetSubKeyNames()
For Each subKeyName In versionKey.GetSubKeyNames()
Dim subKey As RegistryKey = versionKey.OpenSubKey(subKeyName)
name = DirectCast(subKey.GetValue("Version", ""), String)
If name <> "" Then
If Not String.IsNullOrEmpty(name) Then
sp = subKey.GetValue("SP", "").ToString()
End If
install = subKey.GetValue("Install", "").ToString()
If install = "" Then
'no install info, ust be later
Console.WriteLine(versionKeyName & " " & name)
If String.IsNullOrEmpty(install) Then ' No install info; it must be later.
Console.WriteLine($"{versionKeyName} {name}")
Else
If sp <> "" AndAlso install = "1" Then
Console.WriteLine(" " & subKeyName & " " & name & " SP" & sp)
If Not String.IsNullOrEmpty(sp) AndAlso install = "1" Then
Console.WriteLine($"{subKeyName} {name} SP{sp}")
ElseIf install = "1" Then
Console.WriteLine(" " & subKeyName & " " & name)

Console.WriteLine($" {subKeyName} {name}")
End If
End If
Next
Expand All @@ -60,3 +55,9 @@ Public Module VersionTest
End Using
End Sub
End Module
' The example displays output similar to the following:
' v2.0.50727 2.0.50727.4927 SP2
' v3.0 3.0.30729.4926 SP2
' v3.5 3.5.30729.4926 SP1
' v4.0
' Client 4.0.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ Public Module VersionTest
Console.WriteLine($"Version: {Environment.Version}")
End Sub
End Module
' The example displays output similiar to the following:'
' Version: 4.0.30319.18010

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Public Module GetDotNetVersion
Private Sub Get45PlusFromRegistry()
Const subkey As String = "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"

Using ndpKey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey)
Using ndpKey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey)
If ndpKey IsNot Nothing AndAlso ndpKey.GetValue("Release") IsNot Nothing
Console.WriteLine(".NET Framework Version: " + CheckFor45PlusVersion(ndpKey.GetValue("Release")))
Else
Expand Down