【问题标题】:Powershell: Unable to find type [System.IO.Compression.ZipArchiveMode]: TypeNotFound errorPowershell:找不到类型 [System.IO.Compression.ZipArchiveMode]:TypeNotFound 错误
【发布时间】:2021-06-16 16:11:31
【问题描述】:

我看到 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 正确加载?

【问题讨论】:

    标签: windows powershell zip zipfile


    【解决方案1】:

    您还需要加载System.IO.Compression 程序集,而不仅仅是System.IO.Compression.FileSystem

    # Note: Only necessary in *Windows PowerShell*.
    # (These assemblies are automatically loaded on demand 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]
    

    【讨论】:

      猜你喜欢
      • 2019-08-16
      • 2020-09-07
      • 2020-04-02
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 2019-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多