electron应用自启动,添加、查询、修改、删除注册列表

搜索关键字:
1.electron应用自启动,添加、修改、删除注册列表
2.nodejs添加、修改注册自启动注册列表
3.regedit.deleteKey 删除注册列表
4.regedit 添加、删除注册列表
5.npm regedit 添加注册列表
6.nodejs添加、修改、删除注册列表

/**
 * @file 添加、删除注册列表的key值
 */

// eslint-disable-next-line camelcase
const child_process = require('child_process');

//  注意:`HKEY_CURRENT_USER`可以简写为`HKCU`,在网上看到的`HKCU`也就是`HKEY_CURRENT_USER`的意思

// 默认的自启动注册列表地址
const keyPath = 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\';

function deleteKey(keyPath, value) {
    return new Promise((resolve, reject) => {
        try {
            // reg delete HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run /v electron.app.Electron /f
            const result = child_process.exec(`reg delete ${keyPath} /v ${value} /f`);
            resolve(result);
        } catch (error) {
            reject(error);
        }
    });
}

// deleteKey(keyPath, 'electron.app.DuGuanjiaTray')
//     .then(() => console.log('deleteKey ok '))
//     .catch((err) => console.log('deleteKey error', err));

function addKey(keyPath, name, value) {
    return new Promise((resolve, reject) => {
        try {
            // eslint-disable-next-line max-len
            // reg add HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run /v electron.app.Electron /t REG_SZ /d hello.exe /f
            // reg delete HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run /v electron.app.Electron /f

            const result = child_process.exec(`reg add ${keyPath} /v ${name} /t REG_SZ /d ${value} /f`);
            resolve(result);
        } catch (error) {
            reject(error);
        }
    });
}

// addKey(keyPath, 'mytest', 'hello22.exe')
//     .then(() => console.log('addKey ok '))
//     .catch((err) => console.log('addKey error', err));

// deleteKey(keyPath, 'mytest')
//     .then(() => console.log('deleteKey ok '))
//     .catch((err) => console.log('deleteKey error', err));

参考 编写注册表reg文件及批处理操作注册表:https://blog.csdn.net/tp7309/article/details/53449792

参考 npm regedit:https://www.npmjs.com/package/regedit

reg 注册列表查询命令
查询命令:

PS D:\dev> reg query HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run\          

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
    OneDrive    REG_SZ    "C:\Users\username\AppData\Local\Microsoft\OneDrive\OneDrive.exe" /background
    Dingtalk    REG_SZ    D:\tools\dingding\DingTalkLite\DingtalkLauncher.exe /autorun
    Wechat    REG_SZ    D:\tools\wechat\WeChat.exe
    DeviceGuid    REG_SZ    TJ5ensce5bptg9jOVO33PE12iS5jIWaQaqcntprN8nBQqZirH8fhH6lPWtWlhLe8
    Docker Desktop    REG_SZ    "C:\Program Files\Docker\Docker\Docker Desktop.exe" -Autostart
    electron.app.electron-screen    REG_SZ    C:\Users\username\AppData\Local\Programs\electron-screen\electron-screen.exe

PS D:\dev> reg query HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run\ /v electron.app.electron-screen

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
    electron.app.electron-screen    REG_SZ    C:\Users\username\AppData\Local\Programs\electron-screen\electron-screen.exe

PS D:\dev>
const child_process = require('child_process');
function queryKeys(keyPath, value) {
    return new Promise(function (resolve, reject) {
        try {
            // reg query HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run /v electron.app.Electron
            child_process.exec(`reg query ${keyPath} /v ${value}`, (error, stdout, stderr) => {
                if (error) {
                    reject(error);
                    return;
                }
                resolve({stdout, stderr});
            });
        } catch (error) {
            reject(error);
        }
    });
}
(async () => {
    try {
        const {stdout, stderr} = await queryKeys('HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run', 'Wechat');
        console.log('stdout', stdout);
        // 成功
        // 查询到 有这个app启动项
    } catch (error) {
        // 没有查询到该app启动项目
        console.log('error :>> ', error);
    }
})();

你可能感兴趣的:(electron应用自启动,添加、查询、修改、删除注册列表)