Powershell使用OpenFileDialog


支持PS3.0及以后版本

这里有一个不错的函数它能支持ISE及PS控制台(PS3.0及以后版本),它叫Show-OpenFileDialog:

function Show-OpenFileDialog
{
  param
  (
    $StartFolder = [Environment]::GetFolderPath('MyDocuments'),

    $Title = 'Open what?',
    
    $Filter = 'All|*.*|Scripts|*.ps1|Texts|*.txt|Logs|*.log'
  )
  
  
  Add-Type -AssemblyName PresentationFramework
  
  $dialog = New-Object -TypeName Microsoft.Win32.OpenFileDialog
  
  
  $dialog.Title = $Title
  $dialog.InitialDirectory = $StartFolder
  $dialog.Filter = $Filter
  
  
  $resultat = $dialog.ShowDialog()
  if ($resultat -eq $true)
  {
    $dialog.FileName
  }
}

它打开了一个打开文件窗口,用户可以选择输入一个文件到控制台中。假设以后你的脚本需要打开一个CSV文件的话,你就可以额外享受这种打开选择窗口方法。

原文地址:Using the OpenFile Dialog

本文链接: https://www.pstips.net/using-the-openfile-dialog.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注