Function Get-Folder($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
$foldername = New-Object System.Windows.Forms.FolderBrowserDialog
$foldername.Description = "Select a folder"
$foldername.rootfolder = "NetworkShortcuts"
if($foldername.ShowDialog() -eq "OK")
{
$folder += $foldername.SelectedPath
}
return $folder
}
$a = Get-Folder
如何指定初始目录而不是Desktop或NetworkShortcuts。我想指定 F:\ Folder1
之类的路径答案 0 :(得分:1)
这是我随时需要的功能:
//create hashmap
for(int i = 0; i<testDoc.length; i++){
if(setW.contains(testDoc[i])) {
doc.indexOf(testDoc[i]);
//add string and its index to hashmap
}
用法非常简单:
Function Get-FolderPath{
[CmdletBinding()]
Param(
[String]$Description,
[String]$InitialDirectory = "C:\",
[Switch]$NewFolderButton = $false)
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowserDialog.SelectedPath = $InitialDirectory
$FolderBrowserDialog.Description = $Description
$FolderBrowserDialog.ShowNewFolderButton = $NewFolderButton
If($FolderBrowserDialog.ShowDialog() -eq "OK"){
$FolderBrowserDialog.SelectedPath
}
}
或者如果您不希望显示该按钮,请忽略$a = Get-FolderPath -Description "Select a folder" -InitialDirectory "F:\Folder1" -NewFolderButton
。
答案 1 :(得分:1)
将rootfolder
换成SelectedPath
,这应该是您所要的。
Function Get-Folder($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
$foldername = New-Object System.Windows.Forms.FolderBrowserDialog
$foldername.Description = "Select a folder"
$foldername.SelectedPath = "F:\Folder1"
if($foldername.ShowDialog() -eq "OK")
{
$folder += $foldername.SelectedPath
}
return $folder
}
$a = Get-Folder