Powershell - 禁用打开文件对话框的部分

时间:2013-07-18 23:06:18

标签: .net powershell

是否可以禁用打开文件对话框窗口的部分内容,例如创建新文件夹按钮?

我正在使用的代码示例(取自http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/01/hey-scripting-guy-september-1.aspx)是:

Function Get-FileName($initialDirectory)
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = "All files (*.*)| *.*"
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename
} #end function Get-FileName

Get-FileName -initialDirectory "c:\fso"

由于

1 个答案:

答案 0 :(得分:0)

好的,根据评论,这个修改后的脚本版本会禁用在目标目录中创建文件夹的权限,同时对话框处于活动状态,然后在功能完成之前删除该块:

    Function Get-FileName($initialDirectory)
{   
 <#DENY CreateDirectories privilege
  to currently logged on security principal#>

 $acl = get-acl $initialDirectory

 $right = "CreateDirectories"

 $principal = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name

 $denyrule = New-Object System.Security.AccessControl.FileSystemAccessRule($principal,$right,"DENY")

 $acl.AddAccessRule($denyrule)

 set-acl $initialDirectory $acl

 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $OpenFileDialog.initialDirectory = $initialDirectory
 $OpenFileDialog.filter = "All files (*.*)| *.*"
 $OpenFileDialog.ReadOnlyChecked = $true
 $OpenFileDialog.ShowDialog() | Out-Null
 $OpenFileDialog.filename

 #remove block now
 $acl.RemoveAccessRule($denyrule)

 set-acl $initialDirectory $acl

} #end function Get-FileName


Get-FileName -initialDirectory "c:\fso"