@@ -877,7 +877,7 @@ public unsafe string[] GetValueNames()
877
877
return Array . Empty < string > ( ) ;
878
878
}
879
879
880
- var names = new List < string > ( values ) ;
880
+ string [ ] names = new string [ values ] ;
881
881
882
882
// Names in the registry aren't usually very long, although they can go to as large
883
883
// as 16383 characters (MaxValueLength).
@@ -888,6 +888,7 @@ public unsafe string[] GetValueNames()
888
888
// only if needed.
889
889
890
890
char [ ] ? name = ArrayPool < char > . Shared . Rent ( 100 ) ;
891
+ int cpt = 0 ;
891
892
892
893
try
893
894
{
@@ -896,7 +897,7 @@ public unsafe string[] GetValueNames()
896
897
897
898
while ( ( result = Interop . Advapi32 . RegEnumValue (
898
899
_hkey ,
899
- names . Count ,
900
+ cpt ,
900
901
name ,
901
902
ref nameLength ,
902
903
0 ,
@@ -909,7 +910,12 @@ public unsafe string[] GetValueNames()
909
910
// The size is only ever reported back correctly in the case
910
911
// of ERROR_SUCCESS. It will almost always be changed, however.
911
912
case Interop . Errors . ERROR_SUCCESS :
912
- names . Add ( new string ( name , 0 , nameLength ) ) ;
913
+ if ( cpt >= names . Length ) // possible new item during loop
914
+ {
915
+ Array . Resize ( ref names , names . Length * 2 ) ;
916
+ }
917
+
918
+ names [ cpt ++ ] = new string ( name , 0 , nameLength ) ;
913
919
break ;
914
920
case Interop . Errors . ERROR_MORE_DATA :
915
921
if ( IsPerfDataKey ( ) )
@@ -919,9 +925,15 @@ public unsafe string[] GetValueNames()
919
925
// to be big enough however. 8 characters is the largest
920
926
// known name. The size isn't returned, but the string is
921
927
// null terminated.
928
+
929
+ if ( cpt >= names . Length ) // possible new item during loop
930
+ {
931
+ Array . Resize ( ref names , names . Length * 2 ) ;
932
+ }
933
+
922
934
fixed ( char * c = & name [ 0 ] )
923
935
{
924
- names . Add ( new string ( c ) ) ;
936
+ names [ cpt ++ ] = new string ( c ) ;
925
937
}
926
938
}
927
939
else
@@ -949,7 +961,13 @@ public unsafe string[] GetValueNames()
949
961
ArrayPool < char > . Shared . Return ( name ) ;
950
962
}
951
963
952
- return names . ToArray ( ) ;
964
+ // Shrink array to fit found items, if necessary
965
+ if ( cpt < names . Length )
966
+ {
967
+ Array . Resize ( ref names , cpt ) ;
968
+ }
969
+
970
+ return names ;
953
971
}
954
972
955
973
/// <summary>Retrieves the specified value. <b>null</b> is returned if the value doesn't exist</summary>
0 commit comments