locked
Getting the file location using openfiledialog in powershell RRS feed

  • Question

  • Hi

    I have the following code where the user will pick a file and i get the file location. I also set the file location to my button text.

    Add-Type -AssemblyName system.Windows.Forms
    $SetBackupLocation = New-Object System.Windows.Forms.OpenFileDialog
    
    $SetBackupLocation.Filter = "All files (*.*)|*.*"
    $SetBackupLocation.FilterIndex = 2
    $SetBackupLocation.RestoreDirectory = $true
    
    $rc = $SetBackupLocation.ShowDialog()
    if ($rc -eq [System.Windows.Forms.DialogResult]::OK)
    {
    
    $BackupLocation = $SetBackupLocation.FileName #Full path with filename
    $BackupLocationFile = $SetBackupLocation.SafeFileName #File name
    $BackupLocation = $BackupLocation.Replace($BackupLocationFile, "")		
    
    $SetSiteBackupLocBttn.Text = $BackupLocation		
    	
    }

    However, is there a way that i can avoid having to choose a file to get its location? I am really just after a path. So that the user can choose where he wants to save the "backup-file" if you get me.

    br

    Bjorn


    Monday, July 15, 2013 11:56 AM

Answers

  • It might be easier to use the SaveFileDialog instead as you can specify a non-existent filename and pull the folder path much like you are already doing with the OpenFileDialog. This way the user wouldn't have to worry about having to select a file to 'open'.

    Add-Type -AssemblyName system.Windows.Forms
    $SetBackupLocation = New-Object System.Windows.Forms.SaveFileDialog
    $SetBackupLocation.Filter = "All files (*.*)|*.*"
    $SetBackupLocation.FilterIndex = 2
    $SetBackupLocation.RestoreDirectory = $true
    $SetBackupLocation.FileName = 'backupfile'
    $rc = $SetBackupLocation.ShowDialog()


    Boe Prox
    Blog | PoshWSUS | PoshPAIG | PoshChat

    Monday, July 15, 2013 12:10 PM
  • Thank you Boe

    You are right, didnt think about that..

    Here's my code:

    Add-Type -AssemblyName system.Windows.Forms
    $SetBackupLocation = New-Object System.Windows.Forms.SaveFileDialog
    $SetBackupLocation.Filter = "All files (*.*)|*.*"
    $SetBackupLocation.FilterIndex = 2
    $SetBackupLocation.RestoreDirectory = $true
    $SetBackupLocation.FileName = 'Location'
    $rc = $SetBackupLocation.ShowDialog()
    	
    if ($rc -eq [System.Windows.Forms.DialogResult]::OK)
    {
    
    $BackupLocation = $SetBackupLocation.FileName
    $BackupLocation = $BackupLocation.Replace('Location', "")
    		
    $SetSiteBackupLocBttn.Text = $BackupLocation
    		
    }

    :-)

    Monday, July 15, 2013 12:50 PM

All replies

  • It might be easier to use the SaveFileDialog instead as you can specify a non-existent filename and pull the folder path much like you are already doing with the OpenFileDialog. This way the user wouldn't have to worry about having to select a file to 'open'.

    Add-Type -AssemblyName system.Windows.Forms
    $SetBackupLocation = New-Object System.Windows.Forms.SaveFileDialog
    $SetBackupLocation.Filter = "All files (*.*)|*.*"
    $SetBackupLocation.FilterIndex = 2
    $SetBackupLocation.RestoreDirectory = $true
    $SetBackupLocation.FileName = 'backupfile'
    $rc = $SetBackupLocation.ShowDialog()


    Boe Prox
    Blog | PoshWSUS | PoshPAIG | PoshChat

    Monday, July 15, 2013 12:10 PM
  • Thank you Boe

    You are right, didnt think about that..

    Here's my code:

    Add-Type -AssemblyName system.Windows.Forms
    $SetBackupLocation = New-Object System.Windows.Forms.SaveFileDialog
    $SetBackupLocation.Filter = "All files (*.*)|*.*"
    $SetBackupLocation.FilterIndex = 2
    $SetBackupLocation.RestoreDirectory = $true
    $SetBackupLocation.FileName = 'Location'
    $rc = $SetBackupLocation.ShowDialog()
    	
    if ($rc -eq [System.Windows.Forms.DialogResult]::OK)
    {
    
    $BackupLocation = $SetBackupLocation.FileName
    $BackupLocation = $BackupLocation.Replace('Location', "")
    		
    $SetSiteBackupLocBttn.Text = $BackupLocation
    		
    }

    :-)

    Monday, July 15, 2013 12:50 PM