Skip to content

Commit 8de223d

Browse files
author
Ron Petrusha
authored
Minor code revisions for determining installed Framework versions (#564)
* Minor code revisions * Added calls to String.IsNullOrEmpty
1 parent 0c2d6a7 commit 8de223d

File tree

6 files changed

+99
-82
lines changed

6 files changed

+99
-82
lines changed

snippets/csharp/framework/migration-guide/versions-installed1.cs

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,73 @@ public static void Main()
1010

1111
private static void GetVersionFromRegistry()
1212
{
13-
// Opens the registry key for the .NET Framework entry.
14-
using (RegistryKey ndpKey =
15-
RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
13+
// Opens the registry key for the .NET Framework entry.
14+
using (RegistryKey ndpKey =
15+
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).
1616
OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
17+
{
18+
foreach (var versionKeyName in ndpKey.GetSubKeyNames())
1719
{
18-
// As an alternative, if you know the computers you will query are running .NET Framework 4.5
19-
// or later, you can use:
20-
// using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
21-
// RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
22-
foreach (string versionKeyName in ndpKey.GetSubKeyNames())
23-
{
20+
// Skip .NET Framework 4.5 version information.
21+
if (versionKeyName == "v4")
22+
{
23+
continue;
24+
}
25+
2426
if (versionKeyName.StartsWith("v"))
2527
{
2628

2729
RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
28-
string name = (string)versionKey.GetValue("Version", "");
29-
string sp = versionKey.GetValue("SP", "").ToString();
30-
string install = versionKey.GetValue("Install", "").ToString();
31-
if (install == "") //no install info, must be later.
32-
Console.WriteLine(versionKeyName + " " + name);
30+
// Get the .NET Framework version value.
31+
var name = (string)versionKey.GetValue("Version", "");
32+
// Get the service pack (SP) number.
33+
var sp = versionKey.GetValue("SP", "").ToString();
34+
35+
// Get the installation flag, or an empty string if there is none.
36+
var install = versionKey.GetValue("Install", "").ToString();
37+
if (string.IsNullOrEmpty(install)) // No install info; it must be in a child subkey.
38+
Console.WriteLine($"{versionKeyName} {name}");
3339
else
3440
{
35-
if (sp != "" && install == "1")
41+
if (!(string.IsNullOrEmpty(sp)) && install == "1")
3642
{
37-
Console.WriteLine(versionKeyName + " " + name + " SP" + sp);
43+
Console.WriteLine($"{versionKeyName} {name} SP{sp}");
3844
}
39-
4045
}
41-
if (name != "")
46+
if (! string.IsNullOrEmpty(name))
4247
{
4348
continue;
4449
}
45-
foreach (string subKeyName in versionKey.GetSubKeyNames())
50+
foreach (var subKeyName in versionKey.GetSubKeyNames())
4651
{
4752
RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
4853
name = (string)subKey.GetValue("Version", "");
49-
if (name != "")
54+
if (! string.IsNullOrEmpty(name))
5055
sp = subKey.GetValue("SP", "").ToString();
56+
5157
install = subKey.GetValue("Install", "").ToString();
52-
if (install == "") //no install info, must be later.
53-
Console.WriteLine(versionKeyName + " " + name);
58+
if (string.IsNullOrEmpty(install)) //No install info; it must be later.
59+
Console.WriteLine($"{versionKeyName} {name}");
5460
else
5561
{
56-
if (sp != "" && install == "1")
62+
if (!(string.IsNullOrEmpty(sp )) && install == "1")
5763
{
58-
Console.WriteLine(" " + subKeyName + " " + name + " SP" + sp);
64+
Console.WriteLine($"{subKeyName} {name} SP{sp}");
5965
}
6066
else if (install == "1")
6167
{
62-
Console.WriteLine(" " + subKeyName + " " + name);
68+
Console.WriteLine($" {subKeyName} {name}");
6369
}
6470
}
6571
}
6672
}
6773
}
6874
}
6975
}
70-
}
76+
}
77+
// The example displays output similar to the following:
78+
// v2.0.50727 2.0.50727.4927 SP2
79+
// v3.0 3.0.30729.4926 SP2
80+
// v3.5 3.5.30729.4926 SP1
81+
// v4.0
82+
// Client 4.0.0.0

snippets/csharp/framework/migration-guide/versions-installed2.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ public static void Main()
77
Console.WriteLine($"Version: {Environment.Version}");
88
}
99
}
10+
// The example displays output similiar to the following:'
11+
// Version: 4.0.30319.18010

snippets/csharp/framework/migration-guide/versions-installed3.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,41 @@ private static void Get45PlusFromRegistry()
1212
{
1313
const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
1414

15-
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
15+
using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
1616
{
1717
if (ndpKey != null && ndpKey.GetValue("Release") != null) {
1818
Console.WriteLine(".NET Framework Version: " + CheckFor45PlusVersion((int) ndpKey.GetValue("Release")));
1919
}
2020
else {
2121
Console.WriteLine(".NET Framework Version 4.5 or later is not detected.");
2222
}
23-
}
24-
}
25-
26-
// Checking the version using >= will enable forward compatibility.
27-
private static string CheckFor45PlusVersion(int releaseKey)
28-
{
29-
if (releaseKey >= 461808)
30-
return "4.7.2 or later";
31-
if (releaseKey >= 461308)
32-
return "4.7.1";
33-
if (releaseKey >= 460798)
34-
return "4.7";
35-
if (releaseKey >= 394802)
36-
return "4.6.2";
37-
if (releaseKey >= 394254)
38-
return "4.6.1";
39-
if (releaseKey >= 393295)
40-
return "4.6";
41-
if (releaseKey >= 379893)
42-
return "4.5.2";
43-
if (releaseKey >= 378675)
44-
return "4.5.1";
45-
if (releaseKey >= 378389)
46-
return "4.5";
47-
// This code should never execute. A non-null release key should mean
48-
// that 4.5 or later is installed.
49-
return "No 4.5 or later version detected";
23+
}
24+
25+
// Checking the version using >= enables forward compatibility.
26+
string CheckFor45PlusVersion(int releaseKey)
27+
{
28+
if (releaseKey >= 461808)
29+
return "4.7.2 or later";
30+
if (releaseKey >= 461308)
31+
return "4.7.1";
32+
if (releaseKey >= 460798)
33+
return "4.7";
34+
if (releaseKey >= 394802)
35+
return "4.6.2";
36+
if (releaseKey >= 394254)
37+
return "4.6.1";
38+
if (releaseKey >= 393295)
39+
return "4.6";
40+
if (releaseKey >= 379893)
41+
return "4.5.2";
42+
if (releaseKey >= 378675)
43+
return "4.5.1";
44+
if (releaseKey >= 378389)
45+
return "4.5";
46+
// This code should never execute. A non-null release key should mean
47+
// that 4.5 or later is installed.
48+
return "No 4.5 or later version detected";
49+
}
5050
}
5151
}
5252
// This example displays output like the following:

snippets/visualbasic/framework/migration-guide/versions-installed1.vb

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,45 @@ Public Module VersionTest
88
Private Sub GetVersionFromRegistry()
99

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

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

21-
For Each versionKeyName As String In ndpKey.GetSubKeyNames()
2218
If versionKeyName.StartsWith("v") Then
2319
Dim versionKey As RegistryKey = ndpKey.OpenSubKey(versionKeyName)
24-
Dim name As String = DirectCast(versionKey.GetValue("Version", ""), String)
25-
Dim sp As String = versionKey.GetValue("SP", "").ToString()
26-
Dim install As String = versionKey.GetValue("Install", "").ToString()
27-
If install = "" Then
28-
'no install info, ust be later
29-
Console.WriteLine(versionKeyName & " " & name)
20+
' Get the .NET Framework version value.
21+
Dim name = DirectCast(versionKey.GetValue("Version", ""), String)
22+
' Get the service pack (SP) number.
23+
Dim sp = versionKey.GetValue("SP", "").ToString()
24+
25+
Dim install = versionKey.GetValue("Install", "").ToString()
26+
If String.IsNullOrEmpty(install) Then ' No install info; it must be in a child subkey.
27+
Console.WriteLine($"{versionKeyName} {name}")
3028
Else
31-
If sp <> "" AndAlso install = "1" Then
32-
Console.WriteLine(versionKeyName & " " & name & " SP" & sp)
33-
29+
If Not String.IsNullOrEmpty(sp) AndAlso install = "1" Then
30+
Console.WriteLine($"{versionKeyName} {name} SP{sp}")
3431
End If
3532
End If
36-
If name <> "" Then
33+
If Not String.IsNullOrEmpty(name) Then
3734
Continue For
3835
End If
39-
For Each subKeyName As String In versionKey.GetSubKeyNames()
36+
For Each subKeyName In versionKey.GetSubKeyNames()
4037
Dim subKey As RegistryKey = versionKey.OpenSubKey(subKeyName)
4138
name = DirectCast(subKey.GetValue("Version", ""), String)
42-
If name <> "" Then
39+
If Not String.IsNullOrEmpty(name) Then
4340
sp = subKey.GetValue("SP", "").ToString()
4441
End If
4542
install = subKey.GetValue("Install", "").ToString()
46-
If install = "" Then
47-
'no install info, ust be later
48-
Console.WriteLine(versionKeyName & " " & name)
43+
If String.IsNullOrEmpty(install) Then ' No install info; it must be later.
44+
Console.WriteLine($"{versionKeyName} {name}")
4945
Else
50-
If sp <> "" AndAlso install = "1" Then
51-
Console.WriteLine(" " & subKeyName & " " & name & " SP" & sp)
46+
If Not String.IsNullOrEmpty(sp) AndAlso install = "1" Then
47+
Console.WriteLine($"{subKeyName} {name} SP{sp}")
5248
ElseIf install = "1" Then
53-
Console.WriteLine(" " & subKeyName & " " & name)
54-
49+
Console.WriteLine($" {subKeyName} {name}")
5550
End If
5651
End If
5752
Next
@@ -60,3 +55,9 @@ Public Module VersionTest
6055
End Using
6156
End Sub
6257
End Module
58+
' The example displays output similar to the following:
59+
' v2.0.50727 2.0.50727.4927 SP2
60+
' v3.0 3.0.30729.4926 SP2
61+
' v3.5 3.5.30729.4926 SP1
62+
' v4.0
63+
' Client 4.0.0.0

snippets/visualbasic/framework/migration-guide/versions-installed2.vb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ Public Module VersionTest
99
Console.WriteLine($"Version: {Environment.Version}")
1010
End Sub
1111
End Module
12+
' The example displays output similiar to the following:'
13+
' Version: 4.0.30319.18010
1214

snippets/visualbasic/framework/migration-guide/versions-installed3.vb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Public Module GetDotNetVersion
88
Private Sub Get45PlusFromRegistry()
99
Const subkey As String = "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"
1010

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

0 commit comments

Comments
 (0)