如何在 C# Windows Forms 应用程序中 运行 嵌入 .exe 文件?

How can I run an embedded .exe file in C# Windows Forms applications?

如何在 C# 中 运行 嵌入 .exe 文件(安装程序)Windows 以最简单的方式构建应用程序?

我只想单击一个按钮,安装程序就会打开。我的 .exe 文件的名称是 setup.

如果我尝试 Process.Start(setup.exe); 我得到一个错误:

The name 'setup' does not exist in the current context

如果我尝试

System.Diagnostics.Process.Start("setup");

它将打开文件夹 C:\Windows\System32\setup

如果您期望的是最简单的方法,那么不要将 setup.exe 作为 资源 嵌入,而是作为 内容 .

(将setup.exe添加到你的项目中,在解决方案资源管理器中右击setup.exe编辑属性,设置为内容,select 复制 如果较新。)

另一个选项如果setup.exe是你的Visual Studio解决方案的一个项目,就是自动复制setup.exe在launcher项目的输出目录中:如果属于解决方案,添加对setup.exe的引用,这样每次编译都会自动复制,有变化。

最后是非常简单的代码 - 实际上您已经有了:

System.Diagnostics.Process.Start("HelloWorld.exe");

如果需要,您可以更改当前目录:

Environment.CurrentDirectory = @"c:\someSetupExeDir";

但更好的是,您可以使用 Path.Combine:

String fullPath = Path.Combine(directoryPath, fileName);