-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExample2.ps1
85 lines (75 loc) · 2.82 KB
/
Example2.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
###########
# Learn how to build Material Design based PowerShell apps
# --------------------
# Example2: Open and Save Dialog boxes
# --------------------
# Avi Coren (c)
# Blog - https://www.materialdesignps.com
# Github - https://github.com/DrHalfBaked/PowerShell.MaterialDesign
# LinkedIn - https://www.linkedin.com/in/avi-coren-6647b2105/
#
[Void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
[Void][System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\Assembly\MaterialDesignThemes.Wpf.dll")
[Void][System.Reflection.Assembly]::LoadFrom("$PSScriptRoot\Assembly\MaterialDesignColors.dll")
try {
[xml]$Xaml = (Get-content "$PSScriptRoot\Example2.xaml")
$Reader = New-Object System.Xml.XmlNodeReader $Xaml
$Window = [Windows.Markup.XamlReader]::Load($Reader)
}
catch {
Write-Error "Error building Xaml data.`n$_"
exit
}
$Xaml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name) -Scope Script }
$InitialDirectory = "$([Environment]::GetFolderPath("MyDocuments"))"
$FileFilter = "Text files|*.txt|All Files|*.*"
function Save-File {
Param (
[string] $InitialDirectory,
[string] $Filter
)
try {
$SaveFileDialog = New-Object Microsoft.Win32.SaveFileDialog
$SaveFileDialog.initialDirectory = $initialDirectory
$SaveFileDialog.filter = $Filter
$SaveFileDialog.CreatePrompt = $False;
$SaveFileDialog.OverwritePrompt = $True;
$SaveFileDialog.ShowDialog() | Out-Null
return $SaveFileDialog.filename
}
catch {
Throw "Save-File Error $_"
}
}
function Open-File {
Param (
[string] $InitialDirectory,
[string] $Filter
)
try{
$OpenFileDialog = New-Object Microsoft.Win32.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = $Filter
# Examples of other common filters: "Word Documents|*.doc|Excel Worksheets|*.xls|PowerPoint Presentations|*.ppt |Office Files|*.doc;*.xls;*.ppt |All Files|*.*"
$OpenFileDialog.ShowDialog() | Out-Null
return $OpenFileDialog.filename
}
catch {
Throw "Open-File Error $_"
}
}
$Btn_OpenFile.Add_Click({ On_OpenFile })
$Btn_SaveFile.Add_Click({ On_SaveFile })
Function On_OpenFile {
$OpenedFile = Open-File -InitialDirectory $InitialDirectory -Filter $FileFilter
if ($OpenedFile) {
$TextBox_Editor.Text = Get-content $OpenedFile -Raw
}
}
Function On_SaveFile {
$SavePath = Save-File -InitialDirectory $InitialDirectory -Filter $FileFilter
if ($SavePath) {
$TextBox_Editor.Text | Out-File -FilePath $SavePath -Encoding UTF8
}
}
$Window.ShowDialog() | out-null