Windows下右键编辑js文件无法打开记事本——在注册表中使用环境变量

Windows下右键编辑js文件无法打开记事本——在注册表中使用环境变量
我创建了一个vbs文件然后右键编辑可以正常打开。我又检查了js和vbs的注册表相关键发现是一样的。……等等真的一样吗仔细对比Shell\Edit\Command下的默认值发现VBSFile\Shell\Edit\Command的默认值类型为REG_EXPAND_SZJSFile\Shell\Edit\Command的默认值类型为REG_SZ两者的数据内容相同均为%SystemRoot%\system32\notepad.exe %1但类型不同导致了不同的行为。%SystemRoot%是一个环境变量指向 Windows 系统目录如C:\Windows。REG_EXPAND_SZ类型会由系统自动将环境变量展开为实际路径而REG_SZ类型只是普通字符串系统不会对其进行展开。因此当右键编辑.js文件时Windows 尝试去寻找一个字面名为%SystemRoot%的路径自然无法找到于是报出无法访问指定的设备、路径或文件的错误。但经过尝试注册表键的默认值即名称为空的值无法直接修改为REG_EXPAND_SZ类型。直接新建“可扩充字符串值”系统也会要求输入一个名称无法留空。解决方法是使用reg命令。REG ADD KeyName [/v ValueName | /ve] [/t Type] [/s Separator] [/d Data] [/f] [/reg:32 | /reg:64] KeyName [\\Machine\]FullKey Machine Name of remote machine - omitting defaults to the current machine. Only HKLM and HKU are available on remote machines. FullKey ROOTKEY\SubKey ROOTKEY [ HKLM | HKCU | HKCR | HKU | HKCC ] SubKey The full name of a registry key under the selected ROOTKEY. /v The value name, under the selected Key, to add. /ve adds an empty value name (Default) for the key. /t RegKey data types [ REG_SZ | REG_MULTI_SZ | REG_EXPAND_SZ | REG_DWORD | REG_QWORD | REG_BINARY | REG_NONE ] If omitted, REG_SZ is assumed. /s Specify one character that you use as the separator in your data string for REG_MULTI_SZ. If omitted, use \0 as the separator. /d The data to assign to the registry ValueName being added. /f Force overwriting the existing registry entry without prompt. /reg:32 Specifies the key should be accessed using the 32-bit registry view. /reg:64 Specifies the key should be accessed using the 64-bit registry view. Examples: REG ADD \\ABC\HKLM\Software\MyCo Adds a key HKLM\Software\MyCo on remote machine ABC REG ADD HKLM\Software\MyCo /v Data /t REG_BINARY /d fe340ead Adds a value (name: Data, type: REG_BINARY, data: fe340ead) REG ADD HKLM\Software\MyCo /v MRU /t REG_MULTI_SZ /d fax\0mail Adds a value (name: MRU, type: REG_MULTI_SZ, data: fax\0mail\0\0) REG ADD HKLM\Software\MyCo /v Path /t REG_EXPAND_SZ /d ^%systemroot^% Adds a value (name: Path, type: REG_EXPAND_SZ, data: %systemroot%) Notice: Use the caret symbol ( ^ ) inside the expand string以管理员身份打开命令提示符执行以下命令reg add HKEY_CLASSES_ROOT\JSFile\Shell\Edit\Command /ve /t REG_EXPAND_SZ /d %SystemRoot%\system32\notepad.exe %1执行后提示操作成功完成回到regedit刷新即可看到默认值已变为REG_EXPAND_SZ。此时右键编辑.js