有没有办法在执行时区分我的两个磁盘驱动器?
$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);
通过 Windows Powershell?还是我只能使用打开蓝光驱动器而不是 DVD 驱动器的命令?
答案1
你见过这个吗:C# 打开关闭多个磁盘驱动器? 注意,有一种方法可以指定驱动器号:
要打开磁盘驱动器,您需要使用 mciSendStringA 发送两个命令字符串。第一个命令字符串将为所需驱动器指定一个名称。第二个命令将实际打开磁盘托盘:
mciSendStringA("open " + driveLetter + ": type CDaudio alias drive" + driveLetter,
returnString, 0, 0);
mciSendStringA("set drive" + driveLetter + " door open", returnString, 0, 0);
您可以使用静态在 PowerShell 中获取驱动器信息(字母、类型等)获取驱动器方法来自DriveInfo 类:
[System.IO.DriveInfo]::GetDrives()
以下是上述 C# 代码的 PowerShell 版本:
$Winmm = 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
# Replace X with your DVD drive letter
$DvdDrive = 'X'
$Winmm::mciSendStringA("open ${DvdDrive}: type CDaudio alias drive$DvdDrive", $null, 0, 0);
$Winmm::mciSendStringA("set drive$DvdDrive door open", $null, 0, 0);