Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

System.Windows.Forms.SaveFileDialog Replace file crashes dialog

$
0
0

Hello,

I'm having issues with the System.Windows.Forms.SaveFileDialog in that when I try to overwrite a file and I am prompted to replace the file it freezes and crashes powershell.

This seems to only occur in Powershell 2.   Unfortunately for my purpose I am restricted to Powershell 2

What I'm trying to do is create a config file of settings set in a Windows Form I developed.

The save seems to work, and open seems to work, but when I choose save overwriting a file with a different name it crashes powershell.

If I save over the file with the same name then it saves no problem.

Here is a snippet of the Function

Function save-FileName {

param
(
  [Parameter()]$initialdirectory,
  [Parameter()]$filetype = "Powershell (*.ps1) | *.ps1"
)

$saveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
$saveFileDialog.ShowHelp = $True
$saveFileDialog.InitialDirectory = $initialdirectory
$saveFileDialog.filter = $filetype
$saveFileDialog.ShowDialog() | Out-Null
$saveFileDialog.FileName

$saveFileDialog.Dispose()

} # End function save-filename

Here is a reduced version of the Function save-config that calls save-filename

Function save-config {

param
(
    [switch]$saveas
)

IF ($objForm.text -eq "Untitled") {$saveas = $true}
Else {$Filename = $objForm.Text}

IF($saveas){$Filename = save-FileName -initialdirectory “$ScriptPath\Output” -filetype "XML (*.xml) | *.xml" }

IF ($Filename -ne "")
{

if (test-path $Filename) {clear-Content $Filename}

  Add-Content $Filename  ('<?xml version="1.0"?>')
  Add-Content $Filename "<config>"

  # Header

  Add-Content $Filename "<header>"
    Add-Content $Filename ("<opid>" + $OPIDTextbox.text + "</opid>")
    Add-Content $Filename ("<vbserial>" + $VBSerialTextBox.Text + "</vbserial>")
    Add-Content $Filename ("<company>" + $CompanyTextBox.Text + "</company>")
  Add-Content $Filename "</header>"

    # Settings

  Add-Content $Filename "<settings>"
     Add-Content $Filename ("<dns1>" + $dns1TextBox.Text + "</dns1>")
     Add-Content $Filename ("<dns2>" + $dns2TextBox.Text + "</dns2>")
     Add-Content $Filename ("<ntp1>" + $ntp1TextBox.Text + "</ntp1>")
     Add-Content $Filename ("<ntp2>" + $ntp2TextBox.Text + "</ntp2>")
     Add-Content $Filename ("<syslog1>" + $syslog1TextBox.Text + "</syslog1>")
     Add-Content $Filename ("<syslog2>" + $syslog2TextBox.Text + "</syslog2>")
     Add-Content $Filename ("<syslog3>" + $syslog3TextBox.Text + "</syslog3>")
     Add-Content $Filename ("<syslog4>" + $syslog4TextBox.Text + "</syslog4>")
     Add-Content $Filename ("<communities>" + $communityTextBox.Text + "</communities>")
     Add-Content $Filename ("<target1>" + $target1TextBox.Text + "</target1>")
     Add-Content $Filename ("<target2>" + $target2TextBox.Text + "</target2>")
     Add-Content $Filename ("<target3>" + $target3TextBox.Text + "</target3>")
  Add-Content $Filename "</settings>"

  Add-Content $Filename "</config>"

  $objForm.Text = $Filename
  $status.text = "Completed Save to $Filename"

} # End IF Statement

Else {$status.Text = "Cancelled Save"}

} # End of Save-Config


Walter


Viewing all articles
Browse latest Browse all 15028

Trending Articles