'Powershell and Winforms - Encoding to Base64 and supplying the result to a TextBox control is extremely slow
I am working on a small Winforms GUI Powershell Script that takes an input file, encodes to Base64, and then spits out the string into a standard System.Windows.Forms.TextBox.
Here's the form for demonstration purposes:
Unfortunately when I try this on files that are above 400-800kb in size, the application hangs and takes forever to populate the result.
Here's the relevant code:
$SelectedFileChanged = {
if ($CBOXLoadedFiles.SelectedIndex -eq 0) {
return
}
$P = $FilesCollection[$CBOXLoadedFiles.SelectedIndex].Filepath
$B64String = [System.convert]::ToBase64String((Get-Content -LiteralPath $P -AsByteStream -Raw))
$TXTBOXBase64String.Text = $B64String
}
Now, if I try the same thing - but instead of populating the TextBox with the result I just simply write it to a file using Out-File - the operation completes almost instantly! This leads me to believe that the bottleneck is actually writing to the textbox in my GUI.
But this is unacceptable. There has to be a way to optimize things somehow so this doesn't take forever.
I could really use some help here. Any recommendations are highly appreciated!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

