使用regedit包添加注册表,regedit是对node的子进程模块进行了封装,使得我们不用去写shell脚本或者window命令之类的
安装
npm install --save regedit
使用,注意这里我是把作者的vbs目录拷贝到我的项目里了,要不然无法使用,另外是无法往HKEY_CLASSES_ROOT添加的(管理员可以,如果有人知道怎么可以不用管理员身份也可以添加的,请教下我0.0),我 目前是在HKEY_CURRENT_USER添加的,因为这个是允许当前用户的
var regedit = require('regedit');
//添加到注册表的key
var 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');
//首先添加我们需要创建的key
regedit.createKey(regeditList, function(err) {
console.log(err);
})
//往不同的key中添加值和类型,使用了es6语法添加对象的key
var 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/167560
console.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注册,我目前不知道如何操作,如果有大神知道的,请指点下!