views:

71

answers:

3

Hey all,

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.

A: 

You can add any miscellaneous file to your project by rightclicking on the project and selecting add new

Henri
"so that I could hand my *compiled* application to a co-worker"
Blorgbeard
...sure, but I think they want to execute it too
spender
Also, I think you mean add existing.
Blorgbeard
+2  A: 

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();
    }
JimEvans
Virus scanners *really* like .exe files that appear out of nowhere, gives them a reason for being. Just deploy the .exe along with the rest of the program.
Hans Passant
A: 

You might try something like:

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