作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试从我的 Java 程序中获取安装在 Windows 10 中的所有应用程序的列表。我尝试了以下方法:
Runtime.getRuntime().exec("Get-WmiObject -class Win32_Product | Select-Object -Property Name");
我得到:
Cannot run program "Get-WmiObject": CreateProcess error=2
我也试过:
Process p = Runtime.getRuntime().exec("Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize");
结果相似。
最后,我尝试使用“win32”库,但它只返回一些已安装程序的名称。
我需要在 powershell 中执行以下命令时得到的相同结果:
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize
我一直在 stackoverflow 中寻找其他问题,但没有一个能给我解决方案。我需要在每个磁盘单元中搜索(不仅在 C: 中)。有人可以告诉我一个可能的解决方案吗?
谢谢。
最佳答案
您不能直接运行 PowerShell 命令,您必须通过 PowerShell 进程启动它们:
powershell -command "PowerShell commands with parameters"
所以像这样更改您的 exec 调用:
Process p = Runtime.getRuntime().exec("powershell -command \"Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize\"");
关于java - 如何从 Java 获取 Windows 10 上已安装的应用程序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48672200/
我是一名优秀的程序员,十分优秀!