如何在 Windows 中关闭光驱托盘?

Rez*_* M. 8 windows eject optical-drive

我一直想知道为什么 Windows 有一个弹出选项,但在光驱的上下文菜单中没有包含相应的关闭/关闭托盘选项。

有没有办法在使用 Windows 中的任何第三方软件的情况下关闭光驱托盘?

Kar*_*ran 14

只有这样,才能IMO做到这一点,而无需使用3党utils的(如的NirCmdWizmo)将通过的VBScriptPowerShell的。到目前为止,我看到的所有 VBScript 解决方案都使用过时的 Windows Media Player OCX。我不知道最新版本的 WMP 是否包含具有类似功能的 OCX,加上通过Windows 功能禁用/卸载它可能会在任何情况下干扰脚本的运行。

通过代码实现此功能的常用方法是使用媒体控制接口 (MCI) API(特别是set命令)。但是,由于 VBScript不支持调用普通的 Windows API 函数,甚至不支持从任意 DLL 调用函数,因此我们只能使用 PowerShell。因此,在预装 PS 的 Windows 7+ 和安装 PS 后的 XP/Vista 中,以下内容应该是开箱即用的。MCI DLL 即 Windows\System32\WinMM.dll 应该作为 XP+ 中默认安装的一部分可用。

1) 将以下内容另存为CD_Open.ps1

$cd = Add-Type -memberDefinition @"
[DllImport("winmm.dll", CharSet = CharSet.Ansi)] public static extern int mciSendStringA(string lpstrCommand, string lpstrReturnString, int uReturnLength, IntPtr hwndCallback);
"@ -passthru -name mciSendString
$cd::mciSendStringA('set cdaudio door open', $null, 0, 0);
Run Code Online (Sandbox Code Playgroud)

2) 将以下内容另存为CD_Close.ps1

$cd = Add-Type -memberDefinition @"
[DllImport("winmm.dll", CharSet = CharSet.Ansi)] public static extern int mciSendStringA(string lpstrCommand, string lpstrReturnString, int uReturnLength, IntPtr hwndCallback);
"@ -passthru -name mciSendString
$cd::mciSendStringA("set cdaudio door closed", $null, 0, 0);
Run Code Online (Sandbox Code Playgroud)

现在问题来了。默认情况下,出于安全原因,无法在 Windows 中执行未签名的 PS 脚本。键入get-help about_signing在PS提示知道更多关于这一点,包括如何自我签署脚本等。

幸运的是,上面的get-help命令有一个解决方法:

TO PERMIT SIGNED SCRIPTS TO RUN
-------------------------------
   When you start Windows PowerShell on a computer for the first time, the
   Restricted execution policy (the default) is likely to be in effect.

   The Restricted policy does not permit any scripts to run.

   To find the effective execution policy on your computer, type:

       get-executionpolicy

   To run unsigned scripts that you write on your local computer and signed
   scripts from other users, use the following command to change the execution
   policy on the computer to RemoteSigned:

       set-executionpolicy remotesigned

   For more information, see Set-ExecutionPolicy.
Run Code Online (Sandbox Code Playgroud)

3) 因此,从提升的命令提示符中,运行以下命令:

powershell set-executionpolicy remotesigned

(您可以运行powershell set-executionpolicy restricted以恢复到默认设置。)

此命令只需运行一次,并在您再次更改执行策略之前一直有效。

4) 现在您可以使用以下命令(即使来自非提升的命令提示符)来打开/关闭光驱托盘:

powershell -file CD_Open.ps1
powershell -file CD_Close.ps1

当然,您也可以创建快捷方式,以便您可以通过单击或组合键来打开/关闭托盘:

CD 托盘快捷方式

您还可以使用以下 .REG 文件将关闭命令添加到光驱的上下文菜单中:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\SystemFileAssociations\Drive.CDROM\shell]
@="none"

[HKEY_CLASSES_ROOT\SystemFileAssociations\Drive.CDROM\shell\closetray]
@="Close"

[HKEY_CLASSES_ROOT\SystemFileAssociations\Drive.CDROM\shell\closetray\command]
@="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\Powershell.exe -windowstyle hidden -file I:\\CD_Close.ps1"
Run Code Online (Sandbox Code Playgroud)

(根据需要编辑路径。此外,-WindowStyle参数仅适用于 PS 2.0+。)

  • 我会为 PowerShell 脚本为您 +1,然后再次为注册表想法 +1。 (2认同)