System.IO.Compression问题
问题描述:
我有一个压缩旧文件的.Net 4.5的PowerShell v2脚本。就像这样:System.IO.Compression问题
[Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem")
...
$files = Get-Childitem ...
...
$zip = [System.IO.Compression.ZipFile]::Open($arcpath + $curname, "Create")
foreach ($onefile in $files) {
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip, $onefile.FullName, $onefile.Name, "Optimal") | out-null
}
它工作得很好,直到我已经安装了.NET 4.6.1(这是需要另一个项目)。
现在,它抛出一个错误:
The following exception occurred while retrieving member "Open": "Could not load file or assembly 'System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)" At line:1 char:7 + $zip = <<<< [System.IO.Compression.ZipFile]::Open('C:\Temp\1.zip', "Create") + CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemException + FullyQualifiedErrorId : CatchFromBaseGetMember
我安装WMF 5.1使用本地PS5压缩功能。但它也抛出一个错误:
Compress-Archive -Path $files -DestinationPath C:\Temp\Script\test.zip -CompressionLevel Optimal
Add-Type : Could not load file or assembly 'System.IO.Compression, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058) At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Archive\Microsoft.PowerShell.Archive.psm1:469 char:9 + Add-Type -AssemblyName System.IO.Compression + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Add-Type], BadImageFormatException + FullyQualifiedErrorId : System.BadImageFormatException,Microsoft.PowerShell.Commands.AddTypeCommand
我怎样才能解决呢?
答
最后发现问题。我们使用使用NLog的自定义解决方案。 NLog向解决方案添加对System.IO.Compression的引用。在安装解决方案期间,它会部署dll(可能是旧的或以某种不正确的方式)。我们从安装程序中排除了dll,现在一切正常。
答
卸载NET 4.6.1,安装NET 4.6.2。问题已修复。
听起来像安装不成功。如果没有更好的建议,我会使用特殊的NET卸载工具卸载所有的.NET框架,然后重新安装所有东西。附:您的代码适用于Windows 7 + NET 4.6.2和WMF 5.1 – wOxxOm
非常感谢! NET 4.6.1的卸载和安装4.6.2帮助! – Alterant