SaveFileDialog sample please!

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 10 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
Srinath
Posts: 111
Last visit: Tue Feb 09, 2016 12:12 pm

SaveFileDialog sample please!

Post by Srinath »

I'm trying to save a html file from web browser control. I called save file dialog but not sure how can save it!
PowerShell Code
Double-click the code block to select all.
$saveReportToolStripMenuItem_Click={
 if($savefiledialogReport.ShowDialog() -eq "OK") {
  $savefiledialogReport.OpenFile($savefiledialogReport.FileName)
 }
}
Any further hints would be appreciated. Thanks.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SaveFileDialog sample please!

Post by jvierra »

You want to save the file and not create a file. Try copying the html contents of the web control to a file using Out-File.
User avatar
Srinath
Posts: 111
Last visit: Tue Feb 09, 2016 12:12 pm

Re: SaveFileDialog sample please!

Post by Srinath »

What I'm trying to do is when user click on save menu item, it will show a save dialog with pre-populated file name and extension. When user clicks on OK button, it will save the actual file content ($reportOutput) to the mentioned file.

I'm new to savefileDialog. Do you have any examples by chance? Thanks.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SaveFileDialog sample please!

Post by jvierra »

Assuming $reportOutput is teh contents of teh file then:
$reportOutput | Out-File $filename
will save to the file.
PowerShell Code
Double-click the code block to select all.
$saveToolStripMenuItem_Click={
	#TODO: Place custom script here
     [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.VisualBasic")
     $filename=[microsoft.visualbasic.interaction]::InputBox('Enter file name','File To Save','c:\scripts\myfile.html')     
     $reportOutput | Out-File $filename
}
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: SaveFileDialog sample please!

Post by davidc »

The save file dialog doesn't actually save the file. It only gives you the path that the user selected:
PowerShell Code
Double-click the code block to select all.
$saveToolStripMenuItem_Click={

	if($savefiledialog1.ShowDialog() -eq 'OK')
	{
		$reportOutput | Out-File $savefiledialog1.FileName
	}
}
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SaveFileDialog sample please!

Post by jvierra »

If you really want to use the browser5 to find a file then use this method below:
PowerShell Code
Double-click the code block to select all.
$saveToolStripMenuItem_Click={
     $openfiledialog1.FileName='c:\scripts\myfile.htm'
     $openfiledialog1.CheckFileExists=$false
     if($openfiledialog1.ShowDialog() -eq 'OK'){
          [void][System.Windows.Forms.MessageBox]::Show($openfiledialog1.FileName,"Caption")
          #$reportOutput | Out-File $openfiledialog1.FileName
	}
}
The message box can be removed after it is working.

You may have to use -force when an existing file is selected.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SaveFileDialog sample please!

Post by jvierra »

davidc wrote:The save file dialog doesn't actually save the file. It only gives you the path that the user selected:
PowerShell Code
Double-click the code block to select all.
$saveToolStripMenuItem_Click={

	if($savefiledialog1.ShowDialog() -eq 'OK')
	{
		$reportOutput | Out-File $savefiledialog1.FileName
	}
}
You have to be sure to set teh flag to ignore teh open error as the default is to open the specified file. This can be done in the designer property editor or it can be done inline as I have done.

The OUt-FIles is required to output the results. Use -force if the user chooses an exisiting file. You will first have to close the file as the Open dialog will open any file that it finds.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: SaveFileDialog sample please!

Post by jvierra »

Here is the alternate and default method of handling the dialog results. Notice that ist is simpler and provides a good cancel path using $_
PowerShell Code
Double-click the code block to select all.
$saveToolStripMenuItem_Click={
     $openfiledialog1.FileName='c:\scripts\myfile.htm'
     $openfiledialog1.CheckFileExists=$false
     $openfiledialog1.ShowDialog()
}

$openfiledialog1_FileOk=[System.ComponentModel.CancelEventHandler]{
#Event Argument: $_ = [System.ComponentModel.CancelEventArgs]
     [void][System.Windows.Forms.MessageBox]::Show($openfiledialog1.FileName,"Caption")
     #$reportOutput | Out-File $openfiledialog1.FileName -force
}
User avatar
Srinath
Posts: 111
Last visit: Tue Feb 09, 2016 12:12 pm

Re: SaveFileDialog sample please!

Post by Srinath »

Thank you guys. It just worked with your code snippet!
This topic is 10 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked