如何详细操作JavaScript来修改注册表项?
- 内容介绍
- 文章标签
- 相关推荐
本文共计957个文字,预计阅读时间需要4分钟。
示例:使用WshShell对象执行本地命令。
WshShell对象是Windows Script Host(WSH,即Windows脚本宿主)的内置对象,它允许在Windows操作系统中运行脚本语言,如VBScript或JScript。以下是如何通过WshShell对象执行本地命令的示例:
javascriptvar WshShell=new ActiveXObject(WScript.Shell);
// 执行命令并获取输出var output=WshShell.Run(cmd /c dir, 0, true);console.log(output.StdOut.ReadText());
这段代码创建了WshShell对象的实例,然后使用`Run`方法执行了`dir`命令(列出当前目录下的文件和文件夹)。`0`表示在最小化窗口中执行命令,`true`表示等待命令执行完毕。最后,使用`StdOut.ReadText()`读取并输出命令的执行结果。
本实例主要通过WshShell 对象的相关方法实现。WshShell对象是WSH(WSH是Windows ing Host的缩写,内嵌于Windows操作系统中的脚本语言工作环境)的内建对象,主要负责程序的本地运行、处理注册表、创建快捷方式、获取系统文件夹信息及处理环境变量等工作。 WshShell 对象的相关方法如表11.1所示。
本实例中主要应用了 RegWrite方法,下面将对该方法进行详细介绍。
RegWrite方法用于在注册表中设置指定的键或值,其语法格式如下:
WshShell.RegWrite strName, anyValue, [strType]
参数说明
l strName:用于指定注册表的键或值,若strName以一个反斜杠 (在 中为//) 结束,则该方法设置键,否则设置值。strName 参数必须以根键名“HKEY_CURRENT_USER”、 “HKEY_LOCAL_MACHINE”、“HKEY_CLASSES_ROOT”、“HKEY_USERS”或 “HKEY_CURRENT_CONFIG”开头。
l AnyValue:用于指定注册表的键或值的值。当strType为REG_SZ或REG_EXPAND_SZ时,RegWrite方法自动将 anyValue转换为字符串。若strType为REG_DWORD,则anyValue被转换为整数。若strType为REG_BINARY,则 anyValue必须是一个整数。
l StrType:用于指定注册表的键或值的数据类型。RegWrite方法支持的数据类型为REG_SZ、REG_EXPAND_SZ、 REG_DWORD和REG_BINARY。其他的数据类型被作为strType传递,RegWrite 返回 E_INVALIDARG。
实现过程
(1)编写自定义 函数PageSetup_del()和PageSetup_set(),用于实现清空页眉页脚和恢复页眉页脚的功能。具体代码如下:
< language=" "> var HKEY_RootPath="HKEY_CURRENT_USER//Software//Microsoft//Internet Explorer//PageSetup//"; function PageSetup_del() { //清空页眉页脚 try { var WSc=new ActiveX ("W .Shell"); HKEY_Key="header"; WSc.RegWrite(HKEY_RootPath+HKEY_Key,""); HKEY_Key="footer"; WSc.RegWrite(HKEY_RootPath+HKEY_Key,""); }catch(e){} } function PageSetup_set() { //恢复页眉页脚 try{ var WSc=new ActiveX ("W .Shell"); HKEY_Key="header"; WSc.RegWrite(HKEY_RootPath+HKEY_Key,"&w&b页码,&p/&P"); HKEY_Key="footer"; WSc.RegWrite(HKEY_RootPath+HKEY_Key,"&u&b&d"); }catch(e){} } </ >
(2)建立HTML的 标签,调用WebBrowser控件,代码如下:
< id="WebBrowser" classid="ClSID:8856F961-340A-11D0-A96B-00C04Fd705A2" width="0" height="0"> </ >
(3)创建“清空页眉页脚”和“恢复页眉页脚”的超级链接,并调用自定义函数PageSetup_del()和PageSetup_set()实现相应功能。代码如下:
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ="PageSetup_del()">清空页眉页脚</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ="PageSetup_set()"> 恢复页眉页脚 </a>
(4)建立相关的打印超级链接,并调用WebBrowser控件的相应参数实现打印预览、打印等功能。代码如下:
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ="document.all.WebBrowser.Execwb(7,1)">打印预览</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ="document.all.WebBrowser.Execwb(6,1)">打印</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ="document.all.WebBrowser.Execwb(6,6)">直接打印</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ="document.all.WebBrowser.Execwb(8,1)">页面设置</a>
总结
以上所述是小编给大家介绍的JavaScript修改注册表的方法,希望对大家有所帮助!
本文共计957个文字,预计阅读时间需要4分钟。
示例:使用WshShell对象执行本地命令。
WshShell对象是Windows Script Host(WSH,即Windows脚本宿主)的内置对象,它允许在Windows操作系统中运行脚本语言,如VBScript或JScript。以下是如何通过WshShell对象执行本地命令的示例:
javascriptvar WshShell=new ActiveXObject(WScript.Shell);
// 执行命令并获取输出var output=WshShell.Run(cmd /c dir, 0, true);console.log(output.StdOut.ReadText());
这段代码创建了WshShell对象的实例,然后使用`Run`方法执行了`dir`命令(列出当前目录下的文件和文件夹)。`0`表示在最小化窗口中执行命令,`true`表示等待命令执行完毕。最后,使用`StdOut.ReadText()`读取并输出命令的执行结果。
本实例主要通过WshShell 对象的相关方法实现。WshShell对象是WSH(WSH是Windows ing Host的缩写,内嵌于Windows操作系统中的脚本语言工作环境)的内建对象,主要负责程序的本地运行、处理注册表、创建快捷方式、获取系统文件夹信息及处理环境变量等工作。 WshShell 对象的相关方法如表11.1所示。
本实例中主要应用了 RegWrite方法,下面将对该方法进行详细介绍。
RegWrite方法用于在注册表中设置指定的键或值,其语法格式如下:
WshShell.RegWrite strName, anyValue, [strType]
参数说明
l strName:用于指定注册表的键或值,若strName以一个反斜杠 (在 中为//) 结束,则该方法设置键,否则设置值。strName 参数必须以根键名“HKEY_CURRENT_USER”、 “HKEY_LOCAL_MACHINE”、“HKEY_CLASSES_ROOT”、“HKEY_USERS”或 “HKEY_CURRENT_CONFIG”开头。
l AnyValue:用于指定注册表的键或值的值。当strType为REG_SZ或REG_EXPAND_SZ时,RegWrite方法自动将 anyValue转换为字符串。若strType为REG_DWORD,则anyValue被转换为整数。若strType为REG_BINARY,则 anyValue必须是一个整数。
l StrType:用于指定注册表的键或值的数据类型。RegWrite方法支持的数据类型为REG_SZ、REG_EXPAND_SZ、 REG_DWORD和REG_BINARY。其他的数据类型被作为strType传递,RegWrite 返回 E_INVALIDARG。
实现过程
(1)编写自定义 函数PageSetup_del()和PageSetup_set(),用于实现清空页眉页脚和恢复页眉页脚的功能。具体代码如下:
< language=" "> var HKEY_RootPath="HKEY_CURRENT_USER//Software//Microsoft//Internet Explorer//PageSetup//"; function PageSetup_del() { //清空页眉页脚 try { var WSc=new ActiveX ("W .Shell"); HKEY_Key="header"; WSc.RegWrite(HKEY_RootPath+HKEY_Key,""); HKEY_Key="footer"; WSc.RegWrite(HKEY_RootPath+HKEY_Key,""); }catch(e){} } function PageSetup_set() { //恢复页眉页脚 try{ var WSc=new ActiveX ("W .Shell"); HKEY_Key="header"; WSc.RegWrite(HKEY_RootPath+HKEY_Key,"&w&b页码,&p/&P"); HKEY_Key="footer"; WSc.RegWrite(HKEY_RootPath+HKEY_Key,"&u&b&d"); }catch(e){} } </ >
(2)建立HTML的 标签,调用WebBrowser控件,代码如下:
< id="WebBrowser" classid="ClSID:8856F961-340A-11D0-A96B-00C04Fd705A2" width="0" height="0"> </ >
(3)创建“清空页眉页脚”和“恢复页眉页脚”的超级链接,并调用自定义函数PageSetup_del()和PageSetup_set()实现相应功能。代码如下:
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ="PageSetup_del()">清空页眉页脚</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ="PageSetup_set()"> 恢复页眉页脚 </a>
(4)建立相关的打印超级链接,并调用WebBrowser控件的相应参数实现打印预览、打印等功能。代码如下:
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ="document.all.WebBrowser.Execwb(7,1)">打印预览</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ="document.all.WebBrowser.Execwb(6,1)">打印</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ="document.all.WebBrowser.Execwb(6,6)">直接打印</a> <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ="document.all.WebBrowser.Execwb(8,1)">页面设置</a>
总结
以上所述是小编给大家介绍的JavaScript修改注册表的方法,希望对大家有所帮助!

