2

I'm making a program to change various icons through RegEdit, and I figured a nice GUI where people can choose which .ico files they would like their file changed to would be a good touch. Only thing is, I know nothing about PowerShell, but it was much easier to manipulate RegEdit than with Python. And I know less so about PowerShell GUI.

Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ 
    InitialDirectory = [Environment]::GetFolderPath('Desktop') 
    Filter = 'Icons (*.ico)|*.ico'
}
$result = $FileBrowser.ShowDialog()
<#What do here?#>

The last line, where the comment is, what do I do to get the string, or whatever datatype, that contains the file name I just searched for? For anyone curious, that $FileBrowser just gives a default Windows file search window, which I just placed a .ico restriction on. What method do I call? I read something about DialogResult but I don't understand how that method or applet ties in to ShowDialog nor how it is used to obtain the filename, (if that is the right solution)

2 Answers2

2
Add-Type -AssemblyName System.Windows.Forms

$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ 
    InitialDirectory = [Environment]::GetFolderPath('Desktop') 
    Filter = 'Icons (*.ico)|*.ico'
}

if ('OK' -eq $FileBrowser.ShowDialog()) {
  "User chose: $($FileBrowser.FileName)"
}
else {
  'User canceled the dialog.'
}

Note: You can use strings in PowerShell in lieu of enumeration values, which PowerShell automatically converts; you can also use the enumeration values explicitly, but that is more verbose; e.g., 'OK' vs. [System.Windows.Forms.DialogResult]::OK.


As for discovering the involved types and their members:

  • You can use the Get-Member cmdlet on a variable to discover its value's .NET type and that type's members.

  • To get more information about a given .NET type, you can simply google google it, or construct and open a URL programmatically as follows:

$result = $FileBrowser.ShowDialog()

# Get the full name of the type of the value stored in $result
$fullTypeName = $result.GetType().FullName

# Assuming the type is one that comes with .NET,
# look up its documentation online (using the default browser).
Start-Process "https://learn.microsoft.com/en-us/dotnet/api/$fullTypeName"

This answer contains a convenience function named Show-TypeHelp that wraps the above.

mklement0
  • 382,024
  • 64
  • 607
  • 775
-2

$Result | Get-Member

This will show you the way.

Dustin
  • 39
  • 6