electron通过注册表打开软件

electron 通过注册表获取软件安装路径

相关依赖

使用

  • 需要在package.jsonbuild对象中添加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 = '网易云音乐' // 在regeditPath中的软件名称(下图蓝色标注)
  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) // 获取成功,clientPath就是安装路径
            } else {
              dialog.showErrorBox('提示', '未找到该软件') // 注册表中有,但是没有该exe文件
            }
          })
        })
    } else {
      dialog.showErrorBox('提示', '未找到该软件') // 注册表中没有软件路径
    }
  })
}
  • 上述代码图解

在这里插入图片描述

要使用Electron和Node.js在浏览器中打开本地视频,你可以使用Node.js的`fs`模块来读取本地视频文件,然后将文件路径作为`<video>`标签的`src`属性值,最后使用Electron的`BrowserWindow`模块来创建一个包含`<video>`标签的窗口。 具体步骤如下: 1. 在你的Electron项目目录下,创建一个视频文件夹,并将你的本地视频文件放入该文件夹中。 2. 在你的JavaScript文件中,使用`fs`模块读取本地视频文件,示例代码如下: ```javascript const { app, BrowserWindow } = require('electron'); const path = require('path'); const url = require('url'); const fs = require('fs'); let mainWindow; function createWindow() { mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true, }, }); const filePath = path.join(__dirname, '/videos/video.mp4'); // 视频文件路径 const fileExists = fs.existsSync(filePath); if (fileExists) { mainWindow.loadURL(url.format({ pathname: path.join(__dirname, 'index.html'), protocol: 'file:', slashes: true, })); mainWindow.webContents.on('did-finish-load', () => { mainWindow.webContents.send('play-video', filePath); }); } else { console.error('视频文件不存在'); } mainWindow.on('closed', () => { mainWindow = null; }); } app.on('ready', createWindow); app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); app.on('activate', () => { if (mainWindow === null) { createWindow(); } }); ``` 其中,`filePath`是你本地视频文件的路径,`fileExists`变量用于判断视频文件是否存在。 3. 在你的HTML文件中,创建一个包含`<video>`标签的页面,示例代码如下: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>My Video Player</title> </head> <body> <video id="video-player" width="640" height="360" controls> Your browser does not support the video tag. </video> <script src="./renderer.js"></script> </body> </html> ``` 其中,`<video>`标签的`id`属性为`video-player`,`controls`属性用于显示视频控制条。 4. 在你的renderer.js文件中,使用`ipcRenderer`模块来接收主进程发送的视频文件路径,并将其作为`<video>`标签的`src`属性值,示例代码如下: ```javascript const { ipcRenderer } = require('electron'); ipcRenderer.on('play-video', (event, filePath) => { const videoPlayer = document.getElementById('video-player'); videoPlayer.src = `file://${filePath}`; }); ``` 其中,`ipcRenderer.on('play-video', ...)`用于接收主进程发送的视频文件路径,`videoPlayer.src`用于将其作为`<video>`标签的`src`属性值。 5. 运行JavaScript文件,即可通过Electron打开包含`<video>`标签的窗口,自动播放指定的本地视频文件。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值