regedit或child_process添加注册表
生活随笔
收集整理的這篇文章主要介紹了
regedit或child_process添加注册表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用regedit包添加注冊表,regedit是對node的子進程模塊進行了封裝,使得我們不用去寫shell腳本或者window命令之類的
安裝
npm install --save regedit使用,注意這里我是把作者的vbs目錄拷貝到我的項目里了,要不然無法使用,另外是無法往HKEY_CLASSES_ROOT添加的(管理員可以,如果有人知道怎么可以不用管理員身份也可以添加的,請教下我0.0),我 目前是在HKEY_CURRENT_USER添加的,因為這個是允許當前用戶的
var regedit = require('regedit');//添加到注冊表的keyvar regeditList = ['HKCU\\Software\\education', 'HKCU\\Software\\education\\DefaultIcon', 'HKCU\\Software\\education\\shell', 'HKCU\\Software\\education\\shell\\open', 'HKCU\\Software\\education\\shell\\open\\command'];//值的類型var type = ['REG_SZ', 'REG_DEFAULT'];//安裝項目的路徑var execPath = process.execPath;//存放.wsf文件的目錄,我把它vbs的目錄拷貝到我的項目中regedit.setExternalVBSLocation('./src/common/vbs');//首先添加我們需要創建的keyregedit.createKey(regeditList, function(err) {console.log(err);})//往不同的key中添加值和類型,使用了es6語法添加對象的keyvar valuesToPut = {[regeditList[0]]: {[type[1]]: {value: 'education Protocol',type: type[1]},'URL Protocol': {value: '1',type: type[0]}},[regeditList[1]]: {[type[1]]:{value: execPath,type: type[1]}},[regeditList[2]]: {[type[1]]:{value: '1',type: type[1]}},[regeditList[3]]: {[type[1]]:{value: '1',type: type[1]}},[regeditList[4]]: {[type[1]]:{value: execPath,type: type[1]}}}//添加自定義的數據到注冊表regedit.putValue(valuesToPut, function(err) {console.log(err);})//查詢注冊列表regedit.list(regeditList[0], function(err, result) {console.log(err);console.log(result);})child_process添加注冊表
注意命令的寫法,小心語法錯誤,如果需要看添加注冊表的用法可以在cmd上輸入REG /?,然后輸入REG ADD /?可以看到添加注冊表的命令,自己可以先在cmd上進行寫入,成功后再到程序上編寫,同樣的會有權限的問題出現
var cp = require('child_process');var decoder = new TextDecoder('gbk');//使用這個也沒有解決亂碼問題var iconv = require('iconv-lite');var encoding = 'cp936';var binaryEncoding = 'binary';cp.exec('REG ADD HKEY_CLASSES_ROOT\\education /ve /t REG_SZ /d "education Protocol" /f',{ encoding: binaryEncoding },function(error,stdout,stderr) {//有些亂碼可以顯示出來,有些還是亂碼,也沒找到完全能解決亂碼問題,參考博客:https://ask.csdn.net/questions/167560console.log(iconv.decode(new Buffer(stdout, binaryEncoding), encoding), iconv.decode(new Buffer(stderr, binaryEncoding), encoding));});cp.exec('REG ADD HKEY_CLASSES_ROOT\\education /v "URL Protocol" /t REG_SZ /d "" /f',function(error,stdout,stderr) {});cp.exec('REG ADD HKEY_CLASSES_ROOT\\education\\DefaultIcon /ve /t REG_SZ /d "C:\\Program Files\\fwsd\\education.exe" /f',function(error,stdout,stderr) {});cp.exec('REG ADD HKEY_CLASSES_ROOT\\education\\shell /ve /t REG_SZ /d "" /f',function(error,stdout,stderr) {cp.exec('REG ADD HKEY_CLASSES_ROOT\\education\\shell\\open /ve /t REG_SZ /d "" /f', function(error, stdout, stderr) { cp.exec('REG ADD HKEY_CLASSES_ROOT\\education\\shell\\open\\command /t REG_SZ /d "C:\\Program Files\\fwsd\\education.exe" /f', function(error, stdout, stderr) { }) })});我想實現的功能是點擊a標簽打開程序,如果是在HKEY_CLASSES_ROOT里注冊的是可以打開的,在HKEY_CURRENT_USER注冊,我目前不知道如何操作,如果有大神知道的,請指點下!
總結
以上是生活随笔為你收集整理的regedit或child_process添加注册表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue 实现 Open Graph 分享
- 下一篇: python3-numpy数组切片和索引