我正在搜索NodeJS实现:
reg query "HKEY_CURRENT_USER\Volatile Environment" /s /v CLIENTNAME
递归在 \ Volatile Environment 注册表中搜索CLIENTNAME变量。
现在我的解决方案是:
var Registry = require( 'winreg' ),
regKey = new Registry( {
hive: Registry.HKCU,
key: "\\Volatile Environment\\3\\" // for my use-case, the \\3\\ varies and I need something Dynamic
} );
regKey.get( Registry.DEFAULT_VALUE, function( err, item ) {
if ( err ) {
console.log( "Error get: " );
console.log( err );
}
else {
console.log( "Success get: " );
console.dir( item.value );
}
} );
是否有一些优雅的方式使关键路径动态化?
答案 0 :(得分:0)
作为一个临时解决方案,我编写了这个事件发送递归函数(我没有将此标记为已接受的答案,因为我肯定期待在WINREG库中更改参数值的行中的某些内容)< / p>
由于我只是在学习NodeJS,所以我为了好玩而制作了一个组件:
var EventEmitter = require( 'events' ).EventEmitter;
function RegistryDataEvents( env_var_name ) {
this.events = undefined;
this.initEvents( env_var_name );
} // cunstruct RegistryDataEvents
RegistryDataEvents.prototype.initEvents = function( env_var_name ) {
this.events = new EventEmitter();
var _that = this;
process.nextTick(
function() {
_that.retrieveClientName( 0, false, "", env_var_name );
}
);
}; // initEvents
module.exports.RegistryDataEvents = RegistryDataEvents;
RegistryDataEvents.prototype.rerun = function( env_var_name ) {
this.retrieveClientName( 0, false, "", env_var_name );
}; // rerun
RegistryDataEvents.prototype.retrieveClientName = function( count,
found,
result,
env_var_name ) {
var _that = this;
if ( count === 0 ) {
_that.events.emit( 'start', result );
}
if ( found ) {
_that.events.emit( 'success', result );
return;
}
var key = "\\Volatile Environment\\"+ count +"\\";
var Registry = require( './registry.js' ),
regKey = new Registry( {
hive: Registry.HKCU,
key: key
} );
regKey.get( env_var_name, function( err, item ) {
if ( err ) {
// obviously this is a downfall of the module, any suggestions will be appreciated :)
if ( count > 100 ) {
_that.events.emit( 'failure', result, err, count );
return;
}
_that.retrieveClientName( ++count, false, "", env_var_name );
}
else {
found = true;
_that.retrieveClientName( ++count, true, item.value, env_var_name );
return;
}
} );
return;
}; // retrieveClientName
<强>用法强>
var RegistryDataEvents = require( './reg_data_evts.js' ).RegistryDataEvents;
// Register the event + listeners
var reg_data_evts = new RegistryDataEvents( "CLIENTNAME" );
// handle listener logic
reg_data_evts.events.on( 'start', function( result ) {
console.log( "Started " + result );
} );
reg_data_evts.events.on( 'success', function( result ) {
console.log( "Success: " + result );
} );
reg_data_evts.events.on( 'failure', function( result, err, count ) {
console.log( "Failure: " + result + " - " + err + " after " + count + " attempts" );
} );
// just to show u can rerun the retrieveClientName method
reg_data_evts.rerun( "CLIENTNAME" );
// just to show u can rerun the retrieveClientName method
reg_data_evts.rerun( "CLIENTNAME" );