VMware Cloud Community
emcclure
Enthusiast
Enthusiast
Jump to solution

Opening a window to save a file using FolderBrowserDialog

Hello,

So I'm trying to introduce a GUI option to save a file when exporting an OVF.  I have this in my script:

[System.Reflection.Assembly]::LoadWithpartialName("System.windows.forms") | Out-Null
$FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowser.Description = "Select a folder"
$FolderBrowser.rootfolder = "MyComputer"
$FolderBrowser.SelectedPath

$result = $FolderBrowser.ShowDialog()
$result

if($result -eq "OK") {
Write-Host "Selected Location:" -ForegroundColor Green
$FolderBrowser.SelectedPath
}
else {Write-Host "File Save Dialog Canceled" -ForegroundColor Yellow
  &$commands
}

However when the prompt comes up it's only really showing me what's on the local machine.  From googling around I haven't found much to create a browse button to a network share and if a user has a mapped drive to a share that's easy to do, however I don't want to assume the user has a mapped drive.  I'd like for them to be able to use something like SaveFileDialog, but just able to put a folder instead of a file so the OVF is saved off.  Is there something else in FolderBrowserDialog I'm missing that will work with this or do I need to use SaveFileDialog but somehow modify it so it works with just allowing me to select a folder?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$PickFolder = New-Object -TypeName System.Windows.Forms.OpenFileDialog

$PickFolder.FileName = 'dummy'

$PickFolder.Filter = 'Folder Selection|*.*'

$PickFolder.AddExtension = $false

$PickFolder.CheckFileExists = $false

$PickFolder.Multiselect = $false

$PickFolder.CheckPathExists = $true

$PickFolder.ShowReadOnly = $false

$PickFolder.ReadOnlyChecked = $true

$PickFolder.ValidateNames = $false

$result = $PickFolder.ShowDialog()

if($result -eq [System.Windows.Forms.DialogResult]::OK) {

    $destFolder = Split-Path $PickFolder.FileName

    Write-Host "Selected Location: $destFolder" -ForegroundColor Green

    Get-VM -Name "$vm" | Export-VApp -Destination $destFolder -Format OVF -Force

}

else {

    Write-Host "File Save Dialog Canceled" -ForegroundColor Yellow

#    &$commands

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
7 Replies
emcclure
Enthusiast
Enthusiast
Jump to solution

Another thing I just noticed on this is even after running my script and specifying a network share it saves the files on my desktop.  Puts them in a folder called OK which is rather odd.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like there is no native Windows dialog that offers that functionality.
There are indeed some that seem to use the SaveFileDialog with a dummy filename, but that is not really an elegant solution.

$PickFolder = New-Object -Typename System.Windows.Forms.OpenFileDialog

$PickFolder.FileName = 'dummy'

$PickFolder.Filter = 'Folder Selection|*.*'

$PickFolder.AddExtension = $false

$PickFolder.CheckFileExists = $false

$PickFolder.Multiselect = $false

$PickFolder.CheckPathExists = $true

$PickFolder.ShowReadOnly = $false

$PickFolder.ReadOnlyChecked = $true

$PickFolder.ValidateNames = $false


$PickFolder.ShowDialog()


Split-Path $PickFolder.FileName


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
emcclure
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

I used the code you provided, but it's still putting the files in a folder called OK on my desktop even though I provided a network share.  Any idea why that's happening?

$PickFolder = New-Object -TypeName System.Windows.Forms.OpenFileDialog

$PickFolder.FileName = 'dummy'

$PickFolder.Filter = 'Folder Selection|*.*'

$PickFolder.AddExtension = $false

$PickFolder.CheckFileExists = $false

$PickFolder.Multiselect = $false

$PickFolder.CheckPathExists = $true

$PickFolder.ShowReadOnly = $false

$PickFolder.ReadOnlyChecked = $true

$PickFolder.ValidateNames = $false

$result = $PickFolder.ShowDialog()

if($result -eq [System.Windows.Forms.DialogResult]::OK) {
Write-Host "Selected Location:" -ForegroundColor Green
$PickFolder.FileName
}

else {Write-Host "File Save Dialog Canceled" -ForegroundColor Yellow

  &$commands

}

What's also interesting is if I hit cancel it will still attempt to export the OVF onto my desktop in a folder called cancel.  Something is seriously messed up here, but I'm not sure what.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not really, I get the UNC path after I do the Split-Path (\\server\share\folder)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
emcclure
Enthusiast
Enthusiast
Jump to solution

Ok so what am I missing exactly?

$PickFolder = New-Object -TypeName System.Windows.Forms.OpenFileDialog

$PickFolder.FileName = 'dummy'

$PickFolder.Filter = 'Folder Selection|*.*'

$PickFolder.AddExtension = $false

$PickFolder.CheckFileExists = $false

$PickFolder.Multiselect = $false

$PickFolder.CheckPathExists = $true

$PickFolder.ShowReadOnly = $false

$PickFolder.ReadOnlyChecked = $true

$PickFolder.ValidateNames = $false

Split-Path $PickFolder.FileName

$result = $PickFolder.ShowDialog()

if($result -eq [System.Windows.Forms.DialogResult]::OK) {
Write-Host "Selected Location:" -ForegroundColor Green
$PickFolder.FileName
}
else {Write-Host "File Save Dialog Canceled" -ForegroundColor Yellow
  &$commands
}

Get-VM -Name "$vm" | Export-VApp -Destination "$result" -Format OVF -Force

It is still saving it to my desktop for some reason, even though I've specified the network path.  I'm watching it put the files on there and not the network share.  Would SaveFileDialog work better?  Any of what's above as far as .FileName, .AddExtension, etc different for SaveFileDialog or would that just not make a difference?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this

$PickFolder = New-Object -TypeName System.Windows.Forms.OpenFileDialog

$PickFolder.FileName = 'dummy'

$PickFolder.Filter = 'Folder Selection|*.*'

$PickFolder.AddExtension = $false

$PickFolder.CheckFileExists = $false

$PickFolder.Multiselect = $false

$PickFolder.CheckPathExists = $true

$PickFolder.ShowReadOnly = $false

$PickFolder.ReadOnlyChecked = $true

$PickFolder.ValidateNames = $false

$result = $PickFolder.ShowDialog()

if($result -eq [System.Windows.Forms.DialogResult]::OK) {

    $destFolder = Split-Path $PickFolder.FileName

    Write-Host "Selected Location: $destFolder" -ForegroundColor Green

    Get-VM -Name "$vm" | Export-VApp -Destination $destFolder -Format OVF -Force

}

else {

    Write-Host "File Save Dialog Canceled" -ForegroundColor Yellow

#    &$commands

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
emcclure
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

That was it.  I'm now able to export an OVF to a network share.  Thanks so much for the help.

0 Kudos