You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#中YaraSharp库加载失败(System.BadImageFormatException)求助

解决YaraSharp引发的System.BadImageFormatException问题

问题代码

using YaraSharp;
YSInstance YSInstance = new YSInstance();

报错信息

System.BadImageFormatException: 'Could not load file or assembly 'YaraSharp, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null'. Format of the executable (.exe) or library (.dll) is invalid.'

Unhandled exception. System.BadImageFormatException: Could not load file or assembly 'YaraSharp, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null'. Format of the executable (.exe) or library (.dll) is invalid. File name: 'YaraSharp, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null' at Program.$(String[] args)

解决方案

  • 匹配项目平台与库架构:该错误多数是32/64位不兼容导致。右键项目 → 属性 → 生成 → 平台目标,选择与YaraSharp一致的架构(x64或x86),同时取消勾选“首选32位”选项(若为Any CPU模式)。
  • 检查原生YARA依赖库:YaraSharp依赖原生YARA的dll文件(如yara64.dll或yara32.dll),确认该文件架构与项目平台匹配,且已复制到项目输出目录(bin/Debug或bin/Release)。若缺失,需手动下载对应架构的YARA原生库放入目录。
  • 清理重建项目:删除项目的bin和obj目录,右键项目选择“清理”,再执行“生成解决方案”,避免旧缓存文件干扰。
  • 验证NuGet包兼容性:确认安装的YaraSharp版本与你的.NET项目版本兼容,部分YaraSharp版本仅支持.NET Core 3.1+/.NET 5+,若为.NET Framework项目,需安装对应兼容版本。

内容的提问来源于stack exchange,提问作者Abid Raza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 11:17:08