简体   繁体   中英

Include and Execute EXE in C# Command Line App

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#.

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.

Any help is appreciated.

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.

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

You might try something like:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM