Add-Type -AssemblyName PresentationFramework
Add-Type -AssemblyName System.Windows.Forms
[System.Reflection.Assembly]::LoadWithPartialName('System.Drawing')  | Out-Null

function Load_root {
    [xml]$Form = Get-Content "$PSScriptRoot\root.xaml" -Encoding utf8
    $NR=(New-Object System.Xml.XmlNodeReader $Form)
    $window=[Windows.Markup.XamlReader]::Load($NR)
    $Script:btncleaner = $window.FindName("btncleaner")
    $Script:btnfile = $window.findName("btnfile")
    $Script:btnok = $window.FindName("btnok")
    $Script:tbcleaner = $window.FindName("tbcleaner")
    $Script:tbfile = $window.FindName("tbfile")
    $Script:cbo = $window.FindName("cbo")
    $Script:cbb = $window.FindName("cbb")
    $Script:cbv = $window.FindName("cbv")
    $Script:cba = $window.FindName("cba")
    return $window
}
$main = Load_root

$Script:btncleaner.Add_Click({
    $Script:cleaner=Select-FolderDialog
    if (test-path "$Script:cleaner\icons.csv") {
        $Script:tbcleaner.Text = "Cleaner: $Script:Cleaner"
    }else{
        $Script:tbcleaner.Text = "Cleaner: not valid"
    }
})
$Script:btnfile.Add_Click({
    $Script:file=Select-FileDialog
    $Script:tbfile.Text = "Icon: $Script:file"
})
$Script:btnok.Add_Click({
    $csv=import-csv path $Script:Cleaner\icons.csv
    $type=$null
    Write-host "$Script:file"
    $name = get_icon $Script:file
    Export-Icon -Path $Script:file -Destination "$Script:Cleaner\icons\$name.ico"
    if ($cbo.IsSelected) {
        $type=0
    }elseif ($cbb.IsSelected) {
        $type=1
    }elseif ($cbv.IsSelected) {
        $type=2
    }elseif ($cba.IsSelected) {
        $type=3
    }
    $id=($csv.ID[-1])+1
    $item = "$id,$name,$type"
    $item | Export-Csv -Path "$Script:Cleaner\icons.csv" -Append
 
})
Function Select-FolderDialog{
    param([string]$Description="Zielverzeichnis wählen",[string]$RootFolder="Desktop")
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |Out-Null     
    $objForm = New-Object System.Windows.Forms.FolderBrowserDialog
    $objForm.Rootfolder = $RootFolder
    $objForm.Description = $Description
    $Show = $objForm.ShowDialog()
    If ($Show -eq "OK"){
        Return $objForm.SelectedPath
    }else{
        Return $false
    }
}

Function Select-FileDialog{
    [void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    if ($initialDirectory) { $OpenFileDialog.initialDirectory = $initialDirectory }
    $OpenFileDialog.filter = 'All files (*.*)|*.*'
    [void] $OpenFileDialog.ShowDialog()
    return $OpenFileDialog.FileName
}

function get_icon {
    param ($data)
    $data = $data -replace '\s',''
    $found = $false
    for ($c = -1; $c -gt -10; $c--) {
        $char = $data[$c]
        if ($char -eq ".") {
            $trim=$data.Length+$c
            $trimdata=$data.Remove(0,$trim+1)
            $c=-10 
            $found = $true
        }
    }
    if (!$found) {
        $return = 0
    }else {
        $return=$trimdata
    }
    return $return
}

function Export-Icon {
    <#
    .SYNOPSIS
        Exports a file icon to a specified format.
    .DESCRIPTION
        Exports the icon associated with a while to a specified image format.
    .NOTES
        Author      : Paul Broadwith (https://github.com/pauby)
        History     : 1.0 - 25/07/17 - Initial version.
    .LINK
        https://www.github.com/pauby
    .EXAMPLE
        Export-Icon -Path "C:\Windows\Notepad.exe"
    
        Exports the icon for "C:\Windows\Notepad.exe" and saves it in bitmap
        (BMP) format to "Notepad.bmp" in the current current directory.
    
    .EXAMPLE
        Export-Icon -Path "C:\Windows\Notepad.exe" `
            -Destination "C:\Temp\Notepad-Icon.jpg" -Format JPEG
    
        Exports the icon for "C:\Windows\Notepad.exe" and saves it in JPEG 
        format to "C:\Temp\Notepad-Icon.jpg"
    #>
        [CmdletBinding()]
        Param (
            # Path of the source file the icon is to be exported from.
            [Parameter(Mandatory=$true)]
            [string]$Path,
    
            # Path to where the icon will be saved to. If this is blank or missing
            # the file will be saved in the current directory with the basename of
            # the $Path with the correct format extension.
            [string]$Destination,
    
            # Format to save the icon as. Defaults to a bitmap (BMP). 
            [System.Drawing.Imaging.ImageFormat]$Format = "Bmp"
        )
    
        if (!$Destination) {
            $basename = (Get-Item $Path).BaseName
            $extension = $format.ToString().ToLower()
            $Destination = "$basename.$extension"
        }
    
        # Extract the icon from the source file. 
        $icon = [System.Drawing.Icon]::ExtractAssociatedIcon($Path)
    
        # Save the icon in the specified format
        $icon.ToBitmap().Save($Destination, [System.Drawing.Imaging.ImageFormat]::$Format)
    }

$main.ShowDialog() | Out-Null