Inno Setup - 如何识别源目录的 USB 驱动器

Inno Setup - How to identify a USB drive for source directory

我希望 Inno Setup 编译器自动检测 USB 盘符并将其用作安装程序文件的源路径。

但我不知道如何正确识别驱动器。什么是正确的 SourceDir=

源驱动器不应是修复驱动器。

Inno Setup 无法自行执行此操作。但是您可以从 Inno Setup 预处理器调用简单的 PowerShell 代码。

基于Get the drive letter of USB drive in PowerShell, the following will set SourceDir to first removable drive (not necessarily the USB drive, and it won't use USB hard drives). If you really want the first USB drive, try the answer by @CB.

#define GetUsbDrive() \
  Local[0] = AddBackslash(GetEnv("TEMP")) + "usb_drive.txt", \
  Local[1] = \
    "-ExecutionPolicy Unrestricted -Command """ + \
    "$drive = @(Get-WmiObject Win32_Volume -Filter DriveType='2'); " + \
    "if ($drive) { $drive = $drive[0].DriveLetter }; " + \
    "Set-Content -Path '" + Local[0] + "' -NoNewline -Value $drive " + \
    """", \
  Exec("powershell.exe", Local[1], SourcePath, , SW_HIDE), \
  Local[2] = FileOpen(Local[0]), \
  Local[3] = FileRead(Local[2]), \
  FileClose(Local[2]), \
  DeleteFileNow(Local[0]), \
  Local[3]

#define UsbDrive GetUsbDrive()
#if Len(UsbDrive) == 0
#error No USB drive found
#endif

[Setup]
SourceDir={#UsbDrive}