In what order does the function RegistryKey.GetSubKeyNames return the subkeys? Are they in alphabetical order? Or are they in some random order corresponding to their storage in the Registry?
I've done multiple searches and checked MSDN, but I haven't found a definitive answer. Ideally, MSDN would state that keys come out sorted or explicitly state that their order is random...
The reason I want to know is that I want to write code to detect all versions of a COM class in the registry. They will have names like <base-name>13, <base-name>14, etc. If the keys are sorted, I could just find the first one that matches, iterate over all that match my search, and short-circuit out once the keys no longer match my search. Then I wouldn't have to loop over ALL of the subkeys.
Since the documentation does not guarantee any particular order, you cannot assume any particular order. if you need a particular order, you must sort if yourself. (The order is not random, but it is not sorted either.)
I can't say this is a definitive answer but while trying to answer the same question for myself, I concluded that it is the stored order, that is, the FIFO or natural order.
All Registry editors I've used alphabetically sort the key and value names. This is not the natural order. RegScanner showed me what I think is the natural order for HKLM\Software\Microsoft\Windows\CurrentVersion\Run.
I don't know of any way to make the natural order match the alphabetical order other than deleting all the values in the key and then putting them back in alphabetical order. However, this order will be broken if a new value is added later because that new value will be added at the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With