在Windows中使用Java查找特定的注册表项 [英] Finding a specific registry key using java in windows
问题描述
我可以在Windows中转到regedit,然后转到edit-> find并键入要在计算机上找到的密钥(在我的情况下为Maxima),然后找到所需的密钥(为maxima)(在我的情况下为在"HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \ Microsoft \ Windows \ CurrentVersion \ Uninstall \ Maxima-5.17.1_is1"中) 但我需要在Java中动态执行此操作,并在Windows中找到已安装的maxima的当前版本及其目录位置.我不知道如何进行.
I can go to regedit in windows then go to edit->find and type in the key I want to find in my computer (in my case Maxima) and locate the required key (of maxima) (in my case it is in "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Maxima-5.17.1_is1") but I need to do this dynamically in java and find the current version of maxima installed and its directory location in windows. I have no idea how to proceed.
我尝试使用此处所述的方法:读取/写入Windows注册表使用Java ,但是要使用这些方法,我需要知道密钥.如何在Java中动态查找密钥?还是有其他方法可以使用Java在Windows中查找软件的版本和目录位置?
I tried to use the methods stated here: read/write to Windows Registry using Java, but to use these methods I need to know the key. How can I find the key dynamically in java? Or is there any other ways to find the version and directory location of a software in windows using java?
推荐答案
我将使用此类作为您的答案.因为它是用纯Java代码编写的.
I will use this class for your answer. Because it is written in pure java code.
- 在此处中具有
WinRegistry
类. - 获取父键中所有键的列表.
- 过滤列表以获取最合适的密钥(或精确密钥).
- 然后您可以在此键中检查所需的值.
- Have a
WinRegistry
class from here. - Get a list of all keys in parent key.
- filter list to get the most appropriate key (Or exact key).
- Then you can check the value you want in this key.
以下代码可为您提供帮助:
Here is the code to help you :
List<String> ls = WinRegistry.readStringSubKeys(WinRegistry.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\");
String key = ls.stream().filter(st -> st.matches("Maxima.*")).findAny().get();
现在,此key
值将为Maxima-5.17.1_is1
(如果存在,否则将引发java.util.NoSuchElementException
).而且,您可以使用它来获取任何值.
Now this key
value will be Maxima-5.17.1_is1
(if present otherwise java.util.NoSuchElementException
will be thrown). And you can use it to get any Value.
这篇关于在Windows中使用Java查找特定的注册表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!