在C#命令行应用程序中包含并执行EXE

所以我找到了一个很棒的小EXE命令行应用程序(我们称之为program.exe),它输出了一些我想用C#操作的数据。

我想知道是否有办法将program.exe“打包”到我的visual studio项目文件中,这样我就可以将编译好的应用程序交给同事,而不必将它们发送给program.exe。

任何帮助表示赞赏。

有几种方法可以实现这一目标。 首先,您应该将program.exe添加到项目中。 您可以通过右键单击Visual Studio中的项目,然后选择添加>现有项…选择program.exe,它将出现在项目中。 查看其属性,您可以将“复制到输出目录”设置为“始终复制”,它将显示在应用程序旁边的输出目录中。

解决问题的另一种方法是将其作为资源嵌入。 将program.exe添加到项目后,将项目的Build Action属性从Content更改为Embedded Resource。 在运行时,您可以使用Assembly.GetManifestResourceStream提取命令行可执行文件并执行它。

private static void ExtractApplication(string destinationPath) { // The resource name is defined in the properties of the embedded string resourceName = "program.exe"; Assembly executingAssembly = Assembly.GetExecutingAssembly(); Stream resourceStream = executingAssembly.GetManifestResourceStream(resourceName); FileStream outputStream = File.Create(destinationPath); byte[] buffer = new byte[1024]; int bytesRead = resourceStream.Read(buffer, 0, buffer.Length); while (bytesRead > 0) { outputStream.Write(buffer, 0, bytesRead); bytesRead = resourceStream.Read(buffer, 0, buffer.Length); } outputStream.Close(); resourceStream.Close(); } 

您可以尝试以下方法:

 try { System.Diagnostics.Process foobar = Process.Start("foobar.exe"); } catch (Exception error) { // TODO: HANDLE error.Message } 

您可以通过右键单击项目并选择“添加新项”,将任何杂项文件添加到项目中