如果您已安装 PowerShell,则可以执行以下操作:
@echo off
setlocal
set ps_cmd=powershell "Add-Type -AssemblyName System.windows.forms|Out-Null;$f=New-Object System.Windows.Forms.OpenFileDialog;$f.Filter='Model Files (*.mod)|*.mod|All files (*.*)|*.*';$f.showHelp=$true;$f.ShowDialog()|Out-Null;$f.FileName"
for /f "delims=" %%I in ('%ps_cmd%') do set "filename=%%I"
if defined filename (
echo You chose %filename%
) else (
echo You didn't choose squat!
)
goto :EOF
或者,如果您想分解 powershell cmdlet 以便于维护:
@echo off
setlocal
set "ps=Add-Type -AssemblyName System.windows.forms | Out-Null;"
set "ps=%ps% $f=New-Object System.Windows.Forms.OpenFileDialog;"
set "ps=%ps% $f.Filter='Model Files (*.mod)|*.mod|All files (*.*)|*.*';"
set "ps=%ps% $f.showHelp=$true;"
set "ps=%ps% $f.ShowDialog() | Out-Null;"
set "ps=%ps% $f.FileName"
for /f "delims=" %%I in ('powershell "%ps%"') do set "filename=%%I"
if defined filename (
echo You chose %filename%
) else (
echo You didn't choose squat!
)
goto :EOF
(PowerShell 命令无情地从Just Tinkering 博客中窃取。)请参阅OpenFileDialog 类文档以了解您可以设置的其他属性,例如Title
和InitialDirectory
。