包括在C#中的命令行应用程序执行EXE [英] Include and Execute EXE in C# Command Line App

查看:200
本文介绍了包括在C#中的命令行应用程序执行EXE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我发现了一个伟大的小EXE命令行应用程序(我们的Program.exe称呼它),输出一些数据,我想用C#来操作。

So I found a great little EXE command line app (we'll call it program.exe) that outputs some data I would like to manipulate with C#.

,我想知道是否有一种方法来包装的Program.exe到我的Visual Studio项目文件,这样我可以交出我的编译的应用程序到同事,而不必向他们发送PROGRAM.EXE了。

I was wondering if there was a way to "package" program.exe into my visual studio project file, so that I could hand my compiled application to a co-worker without having to send them program.exe too.

任何帮助表示赞赏。

推荐答案

有几种方法可以做到这一点。首先,你应该添加的Program.exe到项目中。你会用鼠标右键单击在Visual Studio中的项目,并选择添加>现有项...选择的Program.exe做到这一点,它会出现在项目中。查看其属性,可以设置复制到输出目录为一直拷贝了,它会出现在你的应用程序旁边的输出目录。

There are several ways you could accomplish this. First, you should add program.exe to the project. You would do this by right-clicking the project in Visual Studio, and selecting Add > Existing Item... Select program.exe, and it will appear in the project. Viewing its properties, you can set "Copy to Output Directory" to "Copy Always", and it will appear in your output directory beside your application.

另一种方式接近问题是将其嵌入作为资源。添加的Program.exe到项目后,该项目的生成操作属性从内容到嵌入的资源改变。在运行时,你可以使用Assembly.GetManifestResourceStream提取命令行可执行文件并执行它。

Another way to approach the problem is to embed it as a resource. After adding program.exe to your project, change the Build Action property of the item from Content to Embedded Resource. At runtime, you could extract the command-line executable using Assembly.GetManifestResourceStream and execute it.

    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();
    }

这篇关于包括在C#中的命令行应用程序执行EXE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆