prosource

Powershell에서 유형 [System]을(를) 찾을 수 없습니다.창문들.Forms.KeyEventHandler]

probook 2023. 8. 11. 22:22
반응형

Powershell에서 유형 [System]을(를) 찾을 수 없습니다.창문들.Forms.KeyEventHandler]

이것은 꽤 간단한 질문일 수도 있지만 저는 완전히 길을 잃었고 답을 찾는 것은 도움이 되지 않았습니다.

텍스트 상자로 간단한 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]

스크립트의 맨 위에 다음 어셈블리를 로드해야 합니다.

[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
    }
})

이 코드는 메모장을 열고 "안녕하세요"를 입력합니다.

[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+shift+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})")

언급URL : https://stackoverflow.com/questions/27791783/powershell-unable-to-find-type-system-windows-forms-keyeventhandler

반응형