windows Powershell:无法找到类型[系统.IO.压缩.ZipArchiveMode]:类型未找到错误

nukf8bse  于 2023-02-09  发布在  Windows
关注(0)|答案(1)|浏览(139)

我看到以下奇怪的行为与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

55ooxyrt

55ooxyrt1#

您还需要加载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]

相关问题