Tuesday, June 9, 2015

#83 : Save File Dialog with Powershell

This is continuation of previous article. In fact, there is no difference in save dialog and open file dialog. The difference if that here you can get message of replacing file. Save File dialog does not save anything, you have to save it yourself.

Below is the code for the same :

#----------------------------------------------------------------------------------------#
#-- Function Declaration 
#----------------------------------------------------------------------------------------#

function Save-File([string] $initialDirectory ) 

{

[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

$OpenFileDialog = New-Object System.Windows.Forms.SaveFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() |  Out-Null

return $OpenFileDialog.filename
} 

#----------------------------------------------------------------------------------------#
#Usage : 
#----------------------------------------------------------------------------------------#

$File=Save-File "C:\temp\tips" 

if ( $File -ne "" ) 
{
echo "You choose FileName: $File" 
} 
else 
{
echo "No File was chosen"
}





Hope this article was useful for you. Thanks for reading!

1 comment:

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...