title: How to handle button to open a file using Powershell?
body: <p>I want to open a file after I select the file using the GUI. When I try my code, I can not open the file I selected. It returns Get-Content Cannot find the path. Anyone can help me, please.</p>
  <pre><code>Function File ($InitialDirectory)
  {
      Add-Type -AssemblyName System.Windows.Forms
      $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
      $OpenFileDialog.Title = &quot;Please Select File&quot;
      $OpenFileDialog.InitialDirectory = $InitialDirectory
      $OpenFileDialog.filter = “All files (*.*)| *.*”
      If ($OpenFileDialog.ShowDialog() -eq &quot;Cancel&quot;) 
      {
      [System.Windows.Forms.MessageBox]::Show(&quot;No File Selected. Please select a file !&quot;, &quot;Error&quot;, 0, 
      [System.Windows.Forms.MessageBoxIcon]::Exclamation)
      }   $Global:SelectedFile = $OpenFileDialog.SafeFileName
  
   
      Get-Content &quot;$Global:SelectedFile&quot; | ForEach-Object {
      $_.Trim()
      } | Where-Object {
  
  
      $_ -notmatch '^(;|$)'
      } | ForEach-Object {
      if ($_ -match '^\[.*\]$') {
          $section = $_ -replace '\[|\]'
          $ini_file[$section] = @{}
      } else {
          $key, $value = $_ -split '\s*=\s*', 2
          $ini_file[$section][$key] = $value
      }
      }
         
  }
  
  
  Add-Type -AssemblyName System.Windows.Forms
  [System.Windows.Forms.Application]::EnableVisualStyles()
  
  $Form                            = New-Object system.Windows.Forms.Form
  $Form.AutoSize                   = $true
  $Form.text                       = &quot;OpenFile&quot;
  $Form.TopMost                    = $true
  #----------------------
  
  $Choose1                      = New-Object system.Windows.Forms.Label
  $Choose1.text                 = &quot;MLs&quot;
  $Choose1.AutoSize             = $true
  $Choose1.width                = 25
  $Choose1.height               = 10
  $Choose1.location             = New-Object System.Drawing.Point(28,20)
  $Choose1.ForeColor            = &quot;#000000&quot;
  
  $Sel                        = New-Object system.Windows.Forms.TextBox
  $Sel.AutoSize               = $true
  $Sel.width                  = 150
  $Sel.height                 = 30
  $Sel.location               = New-Object System.Drawing.Point(120,40)
  $Sel.Text                   = &quot;Selected&quot;
  
  $Choose2                        = New-Object System.Windows.Forms.Button
  $Choose2.text                   = &quot;Select File&quot;
  $Choose2.AutoSize               = $true
  $Choose2.width                  = 90
  $Choose2.height                 = 20
  $Choose2.location               = New-Object System.Drawing.Point(28,38)
  $Choose2.ForeColor              = &quot;#ffffff&quot;
  $Choose2.BackColor              = &quot;#093c76&quot;
  
  $Close                         = New-Object system.Windows.Forms.Button
  $Close.BackColor               = &quot;#6996c8&quot;
  $Close.text                    = &quot;Close&quot;
  $Close.width                   = 98
  $Close.height                  = 30
  $Close.location                = New-Object System.Drawing.Point(450,190)
  $Close.Add_Click({$Form.Close()})
  #----------
  
  $Choose2.Add_Click({Sel_File
  $Sel.Text = $Global:SelectedFile
  }) 
  
  
  $Form.Controls.AddRange(@($Choose1, $Sel, $Choose2, $Close))
  [void] $Form.ShowDialog()
  </code></pre>
  <p>My expectation, I can open the file because I want to process the file. The file that I want to open is .INI file.</p>
  <hr />
  <pre><code>Function File ($InitialDirectory)
  {
      Add-Type -AssemblyName System.Windows.Forms
      $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
      $OpenFileDialog.Title = &quot;Please Select File&quot;
      $OpenFileDialog.InitialDirectory = $InitialDirectory
      $OpenFileDialog.filter = “All files (*.*)| *.*”
      If ($OpenFileDialog.ShowDialog() -eq &quot;Cancel&quot;) 
      {
      [System.Windows.Forms.MessageBox]::Show(&quot;No File Selected. Please select a file !&quot;, &quot;Error&quot;, 0, 
      [System.Windows.Forms.MessageBoxIcon]::Exclamation)
      }   $Global:SelectedFile = $OpenFileDialog.FileName
  }
  
  Function Get-IniContent ($Global:SelectedFile)
  {
      $ini = @{}
      switch -regex -file $Global:SelectedFile
      {
          “^\[(.+)\]” # Section
          {
              $section = $matches[1]
              $ini[$section] = @{}
              $CommentCount = 0
          }
          “^(;.*)$” # Comment
          {
              $value = $matches[1]
              $CommentCount = $CommentCount + 1
              $name = “Comment” + $CommentCount
              $ini[$section][$name] = $value
          } 
          “(.+?)\s*=(.*)” # Key
          {
              $name,$value = $matches[1..2]
              $ini[$section][$name] = $value
          }
      }
      return $ini
  }
  
  $iniContent = Get-IniContent $Global:SelectedFile
  $Region = $iniContent[“Location”]
  
  
  Add-Type -AssemblyName System.Windows.Forms
  [System.Windows.Forms.Application]::EnableVisualStyles()
  
  $Form                            = New-Object system.Windows.Forms.Form
  $Form.AutoSize                   = $true
  $Form.text                       = &quot;OpenFile&quot;
  $Form.TopMost                    = $true
  #----------------------
  
  $Choose1                      = New-Object system.Windows.Forms.Label
  $Choose1.text                 = &quot;MLs&quot;
  $Choose1.AutoSize             = $true
  $Choose1.width                = 25
  $Choose1.height               = 10
  $Choose1.location             = New-Object System.Drawing.Point(28,20)
  $Choose1.ForeColor            = &quot;#000000&quot;
  
  $Sel                        = New-Object system.Windows.Forms.TextBox
  $Sel.AutoSize               = $true
  $Sel.width                  = 150
  $Sel.height                 = 30
  $Sel.location               = New-Object System.Drawing.Point(120,40)
  $Sel.Text                   = &quot;Selected&quot;
  
  $Choose2                        = New-Object System.Windows.Forms.Button
  $Choose2.text                   = &quot;Select File&quot;
  $Choose2.AutoSize               = $true
  $Choose2.width                  = 90
  $Choose2.height                 = 20
  $Choose2.location               = New-Object System.Drawing.Point(28,38)
  $Choose2.ForeColor              = &quot;#ffffff&quot;
  $Choose2.BackColor              = &quot;#093c76&quot;
  
  $Close                         = New-Object system.Windows.Forms.Button
  $Close.BackColor               = &quot;#6996c8&quot;
  $Close.text                    = &quot;Close&quot;
  $Close.width                   = 98
  $Close.height                  = 30
  $Close.location                = New-Object System.Drawing.Point(450,190)
  $Close.Add_Click({$Form.Close()})
  #----------
  
  $Choose2.Add_Click({File
  $Sel.Text = $Global:SelectedFile
  }) 
  
  
  
  $Form.Controls.AddRange(@($Choose1, $Sel, $Choose2, $Close))
  [void] $Form.ShowDialog()
  </code></pre>
  <p>I use edited code, but I can not handle the button. I want to read the INI Section [Location] after I select the file. With this code, The section already printed before I select the file.</p>