Enable and execute exe in c # command line application

So, I found a great EXE command-line application (we will call it program.exe) that displays some data that I would like to process using C #.

I was wondering if there is a way to “pack” program.exe into the visual studio project file so that I can transfer my compiled application to the employee without sending them also to program.exe.

Any help is appreciated.

+3
source share
3 answers

There are several ways to achieve this. First, you must add program.exe to the project. You would do this by right-clicking the project in Visual Studio and choosing Add> Existing Element ... Select program.exe and it will appear in the project. After viewing its properties, you can set "Copy to output directory" to "Always copy", and it will appear in your output directory next to your application.

Another way to approach the problem is to include it as a resource. After adding program.exe to your project, change the Build Action property of this element from Content into Embedded Resource. At run time, you can extract the executable from the command line 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();
    }
+4

- :

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

,

-1
source

Source: https://habr.com/ru/post/1754442/


All Articles