我发现 Windows Powershell 出现以下奇怪行为。
脚本1:
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipArchiveMode]::Update
输出:
PS C:\Users\Administrator> C:\Users\Administrator\Documents\Untitled1.ps1
Unable to find type [System.IO.Compression.ZipArchiveMode].
At C:\Users\Administrator\Documents\Untitled1.ps1:3 char:1
+ [System.IO.Compression.ZipArchiveMode]::Update
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.IO.Compression.ZipArchiveMode:TypeName) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound
脚本2:
Add-Type -AssemblyName System.IO.Compression.FileSystem
[IO.Compression.ZipFile]::Open
[System.IO.Compression.ZipArchiveMode]::Update
输出:
PS C:\Users\Administrator> C:\Users\Administrator\Documents\Untitled1.ps1
OverloadDefinitions
-------------------
static System.IO.Compression.ZipArchive Open(string archiveFileName, System.IO.Compression.ZipArchiveMode mode)
static System.IO.Compression.ZipArchive Open(string archiveFileName, System.IO.Compression.ZipArchiveMode mode, System.Text.Encoding entryNameEncoding)
Update
为什么Script1无法加载
[System.IO.Compression.ZipArchiveMode]::Update
?如何修复 Script1 以确保 [System.IO.Compression.ZipArchiveMode]::Update
正确加载?
您还需要加载
System.IO.Compression
程序集,而不仅仅是 System.IO.Compression.FileSystem
:
# Note: Only necessary in *Windows PowerShell*.
# (These assemblies are automatically loaded in PowerShell (Core) 7+.)
Add-Type -AssemblyName System.IO.Compression, System.IO.Compression.FileSystem
# Verify that types from both assemblies were loaded.
[System.IO.Compression.ZipArchiveMode]; [IO.Compression.ZipFile]
如果将以下行添加到启动配置文件中,错误将永久消失。
添加类型-路径“C:\Windows\Microsoft.NET\Framework64 4.0.30319\System.IO.Compression.dll”-ReferencedAssemblies System.IO.Compression
要获取启动配置文件的位置,请在 PowerShell 或 PowerShell ISE 中键入 $profile。对于 ISE,它看起来像 Powershell_ise.ps1
出于某种奇怪的原因,即使您将其放在要使用 system.io.compression 的脚本顶部,它也看不到它,除非您突出显示该行并运行选择。