wpf 尝试在调试模式下运行项目时出错,但如果我运行 EXE 文件则有效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14322612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:01:35  来源:igfitidea点击:

Error while trying to run project in debug mode, but works if I run the EXE file

c#wpfcompiler-errors

提问by Bob.

When I am trying to run the Debugger for Visual Studio 2010 and for Visual Studio 2012, I get the below error:

当我尝试为 Visual Studio 2010 和 Visual Studio 2012 运行调试器时,出现以下错误:

Error while trying to run project: Could not load file or assembly 'MyProject' or one of its dependencies. An attempt was made to load a program with an incorrect format.

尝试运行项目时出错:无法加载文件或程序集“MyProject”或其依赖项之一。试图加载格式不正确的程序。

But when I go to the x64/Debug folder and run the EXE directly, it opens fine.

但是当我转到 x64/Debug 文件夹并直接运行 EXE 时,它打开得很好。

I've tried disabling the project that contains the 32-bit references, but it still gives me the error even when all the other projects are 64-bit (set through Configuration Manager)

我尝试禁用包含 32 位引用的项目,但即使所有其他项目都是 64 位(通过配置管理器设置),它仍然给我错误

Edit #1: In 32-bit compilation mode, I set everything to Any CPUexcept the Main startup project to x86 and it runs Debug Mode fine. When I go to 64-bit, and do the same but change Main startup project to x64 it gives the above error. I tried setting all Target values to x64, but still nothing.

编辑 #1:在 32 位编译模式下,我将Any CPU除主要启动项目之外的所有内容都设置为 x86,并且它可以正常运行调试模式。当我转到 64 位并执行相同操作但将 Main 启动项目更改为 x64 时,会出现上述错误。我尝试将所有目标值设置为 x64,但仍然没有。

回答by Everlight

The following is my fix to this problem:

以下是我对这个问题的修复:

In Visual Studio 2010 for c# in the solution explorer right click on each project -> properties -> Build. Change the Platform andthe Platform target to Any CPU for 64-bit, or x86 for 32-bit.

在 Visual Studio 2010 for c# 的解决方案资源管理器中,右键单击每个项目 -> 属性 -> 构建。将平台平台目标更改为 64 位的任何 CPU,或 32 位的 x86。

I also had a little reference problem after that, simply remove and re-add any references that cause errors.

之后我也遇到了一个小参考问题,只需删除并重新添加任何导致错误的参考即可。

If you already found a solution to this problem, please post one! Thank you.

如果您已经找到此问题的解决方案,请发布一个!谢谢你。

回答by Siroj Matchanov

I can confirm that Everlight's trick does fix the problem.

我可以确认 Everlight 的技巧确实解决了这个问题。

My situation was as following:

我的情况如下:

  • The Solution was created in Visual Studio 2012 running on x86 (32-bit) Windows 7.
  • After upgrading to x64 (64-bit) Windows 8, I could no longer debug the solution in the same Visual Studio version. However I could run the compiled exe file manually.
  • The error I was getting was: "Error while trying to run project: Unable to start program 'path-to-solution-exe-file'. Unknown Windows web-services infrastructure error has occurred."
  • 该解决方案是在 x86(32 位)Windows 7 上运行的 Visual Studio 2012 中创建的。
  • 升级到 x64(64 位)Windows 8 后,我无法再在同一 Visual Studio 版本中调试解决方案。但是我可以手动运行编译后的 exe 文件。
  • 我得到的错误是:“尝试运行项目时出错:无法启动程序‘path-to-solution-exe-file’。发生了未知的 Windows Web 服务基础结构错误。”

The fix is: Project > {project name} Properties... > Build > Platform target. It should be "Any CPU" by default. Change it to x86 or x64.

修复方法是:项目 > {项目名称} 属性... > 构建 > 平台目标。默认情况下它应该是“任何 CPU”。将其更改为 x86 或 x64。

I hope this helps someone.

我希望这可以帮助别人。

回答by Crash_hp

I've had the same problem with debugging x64 empty console application. Application ran without debugging in any case, but debugging was working only with x86/AnyCPU target platform (32 bit process), but with x64 I was getting:

我在调试 x64 空控制台应用程序时遇到了同样的问题。应用程序在任何情况下都无需调试即可运行,但调试仅适用于 x86/AnyCPU 目标平台(32 位进程),但使用 x64 我得到:

Error while trying to run project: Could not load file or assembly 'ConsoleApplication1' or one of its dependencies. An attempt was made to load a program with an incorrect format.

尝试运行项目时出错:无法加载文件或程序集“ConsoleApplication1”或其依赖项之一。试图加载格式不正确的程序。

My solution was to turn off Project -> Properties -> Debug -> Enable the Visual Studio hosting process

我的解决方案是关闭项目 -> 属性 -> 调试 -> 启用 Visual Studio 托管进程