FolderBrowserDialog startingat last selected folder

Welcome Forums General PowerShell Q&A FolderBrowserDialog startingat last selected folder

Viewing 6 reply threads
  • Author
    Posts
    • #189028
      Participant
      Topics: 1
      Replies: 3
      Points: 18
      Rank: Member

      I am using the FolderBrowserDialog in a Powershellscript.

      Is it possible to remember the selected folder and use it next time as starting location for browsing folders? Now it starts everytime in the same folder (regardless if I use RootFolder and/or SelectedPath)

    • #189052
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      It is hard to give an intelligent and helpul advice if you do not show some of the code you're using. But anyway you can save any location in a variable and use it later on if you need.

    • #189064
      Participant
      Topics: 1
      Replies: 3
      Points: 18
      Rank: Member

      Hello and tnx for your answer!

      I will give some more information.

      The code is part of an addin (called DataStandard) for Autodesk Inventor (3D CAD software) and Vault (Datamanagement software). With this addin a dialog pops up when saving a file for the first time. See this Example.

      This DataStandard exists of several XAML files for the dialogs and ps1 files for the logic.

      In the XMLfile I have made a button for selecting a folder for saving the file. This button points to a simple ps1 file for showing the FolderDialog. This ps1 file has this code:

      function SelectFolder
      {
      ### Zie ook: https://www.powershellmagazine.com/2013/06/28/pstip-using-the-system-windows-forms-folderbrowserdialog-class/
      
      $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
      $FolderBrowser.RootFolder = "MyComputer"
      $FolderBrowser.SelectedPath = "C:\VaultWerkmap\Lightronics\"
      $FolderBrowser.ShowNewFolderButton = "true"
      $FolderBrowser.ShowDialog()
      
      $Prop['Folder'].Value = $FolderBrowser.SelectedPath.Replace("C:\VaultWerkmap\Lightronics\","")
      }

      Buteverytime I use this DataStandard dialog, the folder dialog starts at the same folder. I have tried with and without the Rootfolder and SelectedPath lines.

    • #189070
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      I've used a similar function in the past and it worked just as expected ...

      function Select-Folder {
          [CmdletBinding()]
          [OutputType([string])]
          param(
              [System.String]
              $PreSelection = 'C:\temp'
          )
          Add-Type -AssemblyName System.Windows.Forms
          $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog -Property @{
              SelectedPath = $PreSelection
          }
       
          [void]$FolderBrowser.ShowDialog()
          $FolderBrowser.SelectedPath 
      }

      When you call it without any path it will start in c:\temp but if you provide an existing path it will just start with this path preselected.

    • #189244
      Participant
      Topics: 1
      Replies: 3
      Points: 18
      Rank: Member

      Hello Olaf,

      Even if it is more or less the same code, I tried your code in my case, but it still returns to the preselected location everytime the folderbrowser dialog starts. Maybe this is happening, because everytime I am openeing the folderbrowser, the addin/schript is started again, so it starts as if it is started for the first time?

      I mean, the script is not a constantly running programm, but is used by the addin in the CAD software (and does not "live" inside this CAD programm), and it will be started as a new instance of the script? I don't know how I can explain it clearly...

      Maybe I have to do it in some kind of dirty way by storing the last used path in a txt file.

    • #189247
      Participant
      Topics: 1
      Replies: 3
      Points: 18
      Rank: Member

      Maybe it is not the most clean way, but it seems to work for me:

       

      function SelectFolder
      {
      ### Zie ook: https://www.powershellmagazine.com/2013/06/28/pstip-using-the-system-windows-forms-folderbrowserdialog-class/
      
      # txt bestand om laatst geselecteerde folder in op te slaan
      $savedLastSelectedFolder = "C:\ProgramData\Autodesk\Vault 2020\Extensions\DataStandard\CAD.Custom\addins\Selectedfolder.txt"
      $defaultFolder = "C:\VaultWerkmap\Lightronics"
      
      # Als deze niet bestaat, dan aanmaken met default locatie
      if (!(Test-Path $savedLastSelectedFolder)){
      write-output $defaultFolder | add-content $savedLastSelectedFolder
      }
      
      # nieuwe folderbrowser object aanmaken
      $FolderBrowser = New-Object System.Windows.Forms.FolderBrowserDialog
      $FolderBrowser.RootFolder = "MyComputer"
      #$FolderBrowser.SelectedPath = "C:\VaultWerkmap\Lightronics\"
      $FolderBrowser.SelectedPath = Get-Content -Path $savedLastSelectedFolder # lees textfile uit met laatst opgeslagen locatie
      $FolderBrowser.ShowNewFolderButton = "true"
      $FolderBrowser.ShowDialog()
      
      $Prop['Folder'].Value = $FolderBrowser.SelectedPath.Replace("C:\VaultWerkmap\Lightronics\","")
      
      # tekstfile met laatst opgeslagen locatie verwijderen als deze bestaat en nieuwe aanmaken
      if (Test-Path $savedLastSelectedFolder){
      Remove-Item $savedLastSelectedFolder
      }
      write-output $FolderBrowser.SelectedPath | add-content $savedLastSelectedFolder
      }
    • #189310
      Participant
      Topics: 1
      Replies: 1632
      Points: 3,074
      Helping Hand
      Rank: Community Hero

      ... the addin/schript is started again, so it starts as if it is started for the first time? ...

      That's what I meant in my first answer. You will have to find a way to determine the folder you want to start the forlder browser dialog in. That should happen outside of the function. Then you can use my code example and run the folder browser dialog with the starting folder you like by calling the function with the folder you want provided as parameter.

Viewing 6 reply threads
  • You must be logged in to reply to this topic.