windows平台
首先先明確;開機自啟動寫入注冊表的位置,在KEY_CURRENT_USER\Software\\Microsoft\\Windows\\CurrentVersion\\Run
打開注冊表方法: win + R ,運行regedit,按照上面路徑點到Run上,右側的值就是開機啟動項。
確認開機自啟動設置成功方法: win + R , 運行 msconfig,點開啟動項,前面有對號的就是開機的啟動項。
寫入注冊表:
var regedit = require('regedit'); //引入regedit regedit.putValue({ 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run':{ 'vanish2' : { value : "C:\Windows", type : 'REG_SZ' //type值為REG_DEFAULT時,不會自動創建新的name } } },function(err){ console.log(err); })
列出regedit列表:
regedit.list('HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run',function(err,result){ console.log(result); })
創建Key值,這個key值,其實就是注冊表里的項值,新建項值(一個文件夾,不是文件夾里的值)。
regedit.createKey('HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\ele', function(err) { console.log(err); })
刪除Key值,會刪除文件夾下所有值
regedit.deleteKey('HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run', function(err) { console.log(err); })
刪除值,regedit的文檔中並沒看到這一項,尋找到的文檔中,給出了一個湊合着的方案就是清空鍵值,利用putValue,給原來Name的值變為空
代碼示例:
regedit.putValue({ 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run':{ 'vanish2' : { value : "", type : 'REG_SZ' } } },function(err){ console.log(err); })
刪除參照原github地址:https://github.com/ironSource/node-regedit#regeditdeletekeystringarray-function
當然這種方式治標不治本,所以想要真正的去刪除一個鍵值,還需要靠其他的東西(或許regedit 有,只是我沒看到。)
新的刪除方法,需要引用 child_process
示例代碼:
var cp = require('child_process'); //刪除vanish2的鍵值 cp.exec("REG DELETE HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v vanish2 /f",function(err) { console.log(err); });
這種child_process 也可以去添加注冊表,原理是用系統的reg功能,想要查看Reg功能,請在cmd中,運行 REG /?,會得到提示,同時這些在cmd之中,可以直接運行,所以可以先在cmd運行成功后,再放入程序中調試。
REG 操作查詢:
REG QUERY /?
REG ADD /?
REG DELETE /?
REG COPY /?
REG SAVE /?
REG RESTORE /?
REG LOAD /?
REG UNLOAD /?
REG COMPARE /?
REG EXPORT /?
REG IMPORT /?
REG FLAGS /?
所以通過child_process就可以上面的命令了:
var cp = require('child_process'); cp.exec("REG QUERY HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run",function(error,stdout,stderr) {
console.log(error);
console.log(stdout);
console.log(stderr);
});
希望對你有幫助,謝謝。