electron 通过注册表获取软件安装路径
相关依赖
使用
- 需要在
package.json
的build
对象中添加extraResources
,否则打包后无法使用
"build": {
"extraResources": [
{
"from": "node_modules/regedit/vbs",
"to": "vbs",
"filter": [
"**/*"
]
}
]
}
const { dialog } = require('electron')
let regedit = require('regedit')
regedit.setExternalVBSLocation('./resources/vbs')
let fs = require('fs')
exports.getSoftwarePath = function (cb) {
let regeditPath =
'HKLM\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall'
let softwareName = '网易云音乐'
let hasKey = false
regedit.list(regeditPath, (err, res) => {
let keys = res[regeditPath].keys
for (let i = 0; i < keys.length; i++) {
if (keys[i] === softwareName) {
hasKey = true
break
}
}
if (hasKey) {
regedit
.list([regeditPath + '\\' + softwareName])
.on('data', function (entry) {
let clientPath = entry.data.values.DisplayIcon.value
fs.stat(clientPath, (err, stats) => {
if (stats) {
cb(clientPath)
} else {
dialog.showErrorBox('提示', '未找到该软件')
}
})
})
} else {
dialog.showErrorBox('提示', '未找到该软件')
}
})
}