1+ using System ;
12using System . Collections . Generic ;
23using System . Runtime . InteropServices ;
34using System . Threading . Tasks ;
@@ -22,7 +23,7 @@ private static Task<AsyncRegistryKey> GetHklmAsync()
2223 return AsyncRegistryKey . Hklm ;
2324 }
2425
25- internal class AsyncRegistryKey
26+ internal class AsyncRegistryKey : IDisposable
2627 {
2728#pragma warning disable CA1416
2829 private readonly RegistryKey _key ;
@@ -70,19 +71,24 @@ public override string ToString()
7071 {
7172 return _key . ToString ( ) ;
7273 }
74+
75+ public void Dispose ( )
76+ {
77+ _key ? . Dispose ( ) ;
78+ }
7379#pragma warning restore CA1416
7480 }
7581
76- public async IAsyncEnumerable < string > GetInstallationPaths ( )
82+ public async IAsyncEnumerable < IJavaRegistryRegistration > GetInstallations ( )
7783 {
78- var localMachine = await GetHklmAsync ( ) ;
84+ using var localMachine = await GetHklmAsync ( ) ;
7985 if ( localMachine == null )
8086 {
8187 _logger . LogVerbose ( $ "Not Checking the registry, since we're running on { RuntimeInformation . RuntimeIdentifier } ") ;
8288 yield break ;
8389 }
8490
85- var javaSoft = await localMachine . OpenSubKey ( "SOFTWARE\\ JavaSoft" ) ;
91+ using var javaSoft = await localMachine . OpenSubKey ( "SOFTWARE\\ JavaSoft" ) ;
8692 if ( javaSoft == null )
8793 {
8894 _logger . LogWarning ( @"RegKey 'HKLM\Software\JavaSoft' does not exist." ) ;
@@ -99,20 +105,20 @@ public async IAsyncEnumerable<string> GetInstallationPaths()
99105
100106 foreach ( var root in roots )
101107 {
102- var rootKey = await javaSoft . OpenSubKey ( root ) ;
108+ using var rootKey = await javaSoft . OpenSubKey ( root ) ;
103109 if ( rootKey == null )
104110 {
105111 continue ;
106112 }
107-
108- var keyNames = await rootKey . GetSubKeyNames ( ) ;
109- foreach ( var name in keyNames )
113+
114+ var versions = await rootKey . GetSubKeyNames ( ) ;
115+ foreach ( var javaVersion in versions )
110116 {
111- _logger . LogVerbose ( $ "Checking SubKey: { rootKey } \\ { name } ") ;
112- var key = await rootKey . OpenSubKey ( name ) ;
117+ _logger . LogVerbose ( $ "Checking SubKey: { rootKey } \\ { javaVersion } ") ;
118+ using var key = await rootKey . OpenSubKey ( javaVersion ) ;
113119 if ( key == null )
114120 {
115- _logger . LogWarning ( $ "SubKey '{ rootKey } \\ { name } ' was reported to exist, but it does not.") ;
121+ _logger . LogWarning ( $ "SubKey '{ rootKey } \\ { javaVersion } ' was reported to exist, but it does not.") ;
116122 continue ;
117123 }
118124
@@ -121,8 +127,20 @@ public async IAsyncEnumerable<string> GetInstallationPaths()
121127 continue ;
122128 }
123129
124- yield return javaHome ;
130+ yield return new JavaRegistryRegistration
131+ {
132+ RegKey = key . ToString ( ) ,
133+ InstallationPath = javaHome ,
134+ Version = javaVersion ,
135+ } ;
125136 }
126137 }
127138 }
139+
140+ private class JavaRegistryRegistration : IJavaRegistryRegistration
141+ {
142+ public string RegKey { get ; init ; }
143+ public string InstallationPath { get ; init ; }
144+ public string Version { get ; init ; }
145+ }
128146}
0 commit comments