Powershell 无法找到类型 [System.Windows.Forms.KeyEventHandler]

问题描述 投票:0回答:4

这可能是一个非常简单的问题,但我完全迷失了,寻找答案并没有帮助。

我有一些 powershell 代码来显示带有文本框的简单 GUI。 某些文本框允许用户按 Enter 键来运行 Button_Click 代码。 当我尝试运行 PS1 脚本时,出现以下错误:

Unable to find type [System.Windows.Forms.KeyEventHandler].
Make sure that the assembly that contains this type is loaded.At C:\Scripts\GUI-Testing.ps1:161 char:1

$TestVar=[System.Windows.Forms.KeyEventHandler]
CategoryInfo          : InvalidOperation: (System.Windows.Forms.KeyEventHandler:TypeName)
FullyQualifiedErrorId : TypeNotFound

奇怪的是,如果我关闭 GUI 然后重新运行脚本,我不会收到

Unable to find type
错误,并且按 Enter 键可以按预期工作。

以为我有答案,我尝试使用

[void][reflection.assembly]::Load('System.Windows.Forms.KeyEventHandler')
,这给出了这个错误
Exception calling "Load" with "1" argument(s): "Could not load file or assembly 'System.Windows.Forms.KeyEventHandler' or one of its dependencies. [FileNotFoundException]

powershell user-interface .net-assembly
4个回答
42
投票

确保在脚本顶部加载以下程序集:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

如果仍然不起作用,您可以执行以下操作:

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Enter") 
    {    
        #Write Code Here
    }
})

0
投票

此代码打开记事本并输入“Hello”

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

add-type -AssemblyName microsoft.VisualBasic
add-type -AssemblyName System.Windows.Forms

Start-Process -FilePath "notepad.exe"
start-sleep -Milliseconds 500

[Microsoft.VisualBasic.Interaction]::AppActivate("Notepad")
start-sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("+{h}")
start-sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("{e}")
start-sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("{l}")
start-sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("{l}")
start-sleep -Milliseconds 100
[System.Windows.Forms.SendKeys]::SendWait("{o}")
start-sleep -Milliseconds 100

这是下面的另一个示例,没有显示任何内容 而是发送击键“ctrl+schft+alt+a”:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

add-type -AssemblyName microsoft.VisualBasic

start-sleep -Milliseconds 2500

[System.Windows.Forms.SendKeys]::SendWait("(^+%{a})")

0
投票

我无法让这些工作:

  • [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    ,
  • [reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral')
  • Add-Type -AssemblyName 'System.Windows.Forms'

但是通过诉诸通配符路径搜索,我能够得到以下结果:

$framework = "C:\Windows\Microsoft.NET\Framework$(if([Environment]::Is64BitProcess){'64'})\v4.0.*";
[Reflection.Assembly]::LoadFrom(("$framework\System.Drawing.dll" | Resolve-Path).ToString())
[Reflection.Assembly]::LoadFrom(("$framework\System.Windows.Forms.dll" | Resolve-Path).ToString())
[Reflection.Assembly]::LoadFrom(("$framework\Microsoft.VisualBasic.dll" | Resolve-Path).ToString())
[Reflection.Assembly]::LoadFrom(("$framework\*\PresentationFramework.dll" | Resolve-Path).ToString())

enter image description here


0
投票

将其添加到顶部对我有用

Add-Type -AssemblyName System.Windows.Forms
© www.soinside.com 2019 - 2024. All rights reserved.