我正在尝试构建Powershell脚本来转换文件夹中的某些音频文件,并且我正在使用FolderBrowserDialog询问用户输出文件夹的位置,并使用OpenFileDialog来获取转换器程序的路径(以防万一它不在脚本的同一文件夹中)。这两个对话框都具有单独的功能,这些功能由主程序调用。
问题是,当我从每个函数返回'OpenFileDialog.FileName'和'FolderBrowserDialog.SelectedPath'时,我得到一个包含路径和其他值的对象,而不是包含字符串本身的路径。
这是我从函数中获得的对象:OpenFileDialog.FileName ResultFolderBrowserdialog.SelectedPath Result
功能是:
Function GetConverterPath
{
$currentDirectory = Split-Path -Parent $PSCommandPath;
$isConverterInCurrentDirectory = Test-Path $($currentDirectory + "\tfa2wav.exe")
if($isConverterInCurrentDirectory)
{
return ($currentDirectory + "\tfa2wav.exe");
}
[System.Windows.MessageBox]::Show("The converter's *.exe file was not found in the same directory as this script`n" +
"Please, point to the right file in the next dialog...", 'Converter not found...','Ok','Information');
[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms');
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog;
$OpenFileDialog.filter = 'Executable files (*.exe)|*.exe';
$result = $OpenFileDialog.ShowDialog()
if($result -eq 'OK')
{
return $OpenFileDialog.FileName;
}
else
{
exit;
}
}
Function AskForOutputFolder
{
[System.Windows.MessageBox]::Show("In the next dialog you should select the folder where the converted sound files will be placed",
'Information','Ok','Information');
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null;
$folderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog;
$folderBrowserDialog.Description = "Select a folder";
$result = $folderBrowserDialog.ShowDialog()
if($result -eq 'OK')
{
return $folderBrowserDialog.SelectedPath;
}
else
{
exit;
}
}
关于如何解决此问题的任何想法?
而且,如何防止每个对话框之后在控制台中出现“确定”消息?
在PowerShell中,每个语句的返回为输出,即使没有包含Return关键字的语句。
使用如下的Get-Help 'about_return'
结构
[System.Void]
作为替代:
[void][System.Windows.MessageBox]::Show(…);
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms');
另一种选择(最慢,因此请避免在循环中使用它:
$null = [System.Windows.MessageBox]::Show(…);