Could not load file or assembly … An attempt was m

2018-12-31 19:47发布

I have two projects, ProjectA and ProjectB. ProjectB is a console application, which depends on ProjectA. Yesterday, everything was working fine, but suddenly today when I run ProjectB I get this:

BadImageFormatException was unhandled:
Could not load file or assembly 'ProjectA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Both are just regular projects, with no dependencies on any other non-.Net projects. Both are fully .Net - there is no native code, and no P/Invoke. I have other projects which depend on ProjectA and still work just fine.

Things I have tried:

  • Make sure both projects are set to "Any CPU," with the build checkbox checked. They are.
  • Make sure both projects are for the same Target Framework (.Net 4.0 Client Profile).
  • Under ProjectB --> References --> ProjectA --> Properties, make sure "Copy Local" is set to "True" _ (I verified that ProjectA.dll is being copied correctly)
  • Clean/Rebuild the solution. I even tried manually deleting the /bin and /obj folders in both projects.
  • Restart Visual Studio. Restart my computer.
  • Check out an entirely new copy of the repository.

But I still get the same error. I have no idea what I did to cause this, nor how to fix it. Any ideas?

22条回答
与君花间醉酒
2楼-- · 2018-12-31 20:28

This also can happen just by having multiple supported frameworks defined in the app.config file and, forcing the app to run in a different .NET framework other than the one mentioned first in the app.config file.

And also this fires when you have both of the mentioned frameworks available in your system.

As a workaround, bring up the target framework you are going to use for the debugging up in the app.config

ex: if you trying to run in .NET 4, config file should have something similar to this,

<supportedRuntime version="v4.0"/>
<supportedRuntime version="v2.0.50727"/>
查看更多
后来的你喜欢了谁
3楼-- · 2018-12-31 20:28

Are you trying to run your .exe file from the cmd? This was my mistake. Just run the .exe file by double clicking it. If it's a .NET Core SCD for Windows 8.1/Windows Server 2012 R2 x64.

查看更多
无色无味的生活
4楼-- · 2018-12-31 20:29

You might also see this issue if you're trying to package a 64bit project with an MSI installer in VS. ("The reason is because the native shim packaged with the .msi file is a 32-bit executable.")

See here for more details: http://blogs.msdn.com/b/heaths/archive/2006/02/01/64-bit-managed-custom-actions-with-visual-studio.aspx

查看更多
几人难应
5楼-- · 2018-12-31 20:29

I got this when building a project via Visual Studio Online (VSTS) Build using Visual Studio Build Steps.

The solution was:

  • Delete the existing source folder
  • Explicitly set 'Any CPU' in the platform for all Visual Studio Builds including dependencies (see screenshot below).
  • Re-run the build

VSO Screenshot

查看更多
栀子花@的思念
6楼-- · 2018-12-31 20:30

First of all I got this in VS2017 with an old project I needed to make a tiny change to and upraded all the projects to framework 4.7.


Several others have mentioned selecting Any CPU can fix this issue.

There's a couple places you need to do it, and it might not just be as simple as selecting from the dropdown. This fixed it for me:

1) You need to do it both here:

enter image description here

2) And also in Configuration Manager (right click on solution)

enter image description here

But what if it isn't there???

Then click New and choose these settings: (thanks @RckLN)

enter image description here

查看更多
一个人的天荒地老
7楼-- · 2018-12-31 20:31

I encountered the same issue. It popped up out of the blue and that seemed strange to me.

In the Exception snapshot, for the FusionLog, I saw the following within its message:

... C:\Windows\Microsoft.NET\Framework64 ...

More about the fusion log: http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx

All the projects had a Target CPU of AnyCPU. I changed the application project (the project that references all the other projects) to a Target CPU of x86. It now works.

Not sure how the Target CPU mix up occurred for no apparent reason, but it did.

查看更多
登录 后发表回答