在 64 位机器上以 32 位方式运行 C# 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/437271/
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
Running a C# application as 32-bit on a 64-bit machine
提问by
How do I force my application to run as 32-bit on a 64-bit machine?
如何强制我的应用程序在 64 位机器上作为 32 位运行?
The code is written in C#.
代码是用 C# 编写的。
采纳答案by Matt Briggs
Right click your project, and select properties.
右键单击您的项目,然后选择属性。
In properties, select the build tab. Under platform target, select x86.
在属性中,选择构建选项卡。在平台目标下,选择 x86。
Hit Ctrl+Shift+Sto save all files, right click the solution and select "Clean" to get rid of old binaries. Any builds after that should be 32 bit
命中Ctrl+ Shift+S保存所有文件,右键单击该解决方案,然后选择“清洁”摆脱旧的二进制文件。之后的任何构建都应该是 32 位
回答by JD Conley
Assuming this is a Winforms, console app, or Windows service you have to build the exe for x86 instead of Any CPU. It's in the Configuration Manager.
假设这是一个 Winforms、控制台应用程序或 Windows 服务,您必须为 x86 而不是任何 CPU 构建 exe。它在配置管理器中。
回答by JD Conley
If you go to Configuration Manager in Visual Studio you can set the platform to x86 or x64.
如果您转到 Visual Studio 中的配置管理器,您可以将平台设置为 x86 或 x64。
回答by Campbell
Here's how I did it when we couldn't change the existing code from Any CPUto x86due to a ClickOncelimitation:
当我们由于ClickOnce限制而无法将现有代码从Any CPU更改为x86时,我是这样做的:
Create a 32-bit (x86 must be checked under project properties) 'launcher' application (Windows Application but not form):
创建一个 32 位(x86 必须在项目属性下检查)“启动器”应用程序(Windows 应用程序但不是表单):
static void Main(string[] args)
{
// Load the assembly
string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string assemblyName = Path.Combine(directory, "YourAnyCPUApplication.exe");
Assembly assembly = Assembly.LoadFile(assemblyName);
assembly.EntryPoint.Invoke(null, null);
}
Add the following code to the Main method in the Any CPUproject:
在Any CPU项目的 Main 方法中添加以下代码:
if (IntPtr.Size == 4)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// etc...
}
else
{
// Launch application in 32-bit mode
System.Diagnostics.Process.Start(Path.GetDirectoryName(Application.ExecutablePath)
+ @"\Your32BitApplicationLauncher.exe");
}
I hope this helps :-)
我希望这有帮助 :-)
回答by alexdej
Command-line form:
命令行形式:
corflags application.exe /32BIT+
回答by phuclv
Setting the project to x86 will prevent the file from executing on non-x86 platforms such as ARM. Since Visual Studio 11 and .NET framwork 4.5 there's a new option named Any CPU 32-bit preferred
and this was the default since then. The resulting code will run on anyplatforms but on 64-bit platforms they are run as 32-bit processes
将项目设置为 x86 将阻止文件在非 x86 平台(如 ARM)上执行。从 Visual Studio 11 和 .NET 框架 4.5 开始,有一个名为的新选项Any CPU 32-bit preferred
,从那时起这是默认设置。生成的代码将在任何平台上运行,但在 64 位平台上它们作为 32 位进程运行