Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Executing EXE embedded in assembly

Back to Forum: Tech Off
  • User profile image
    DiViNO

    Is it possible to add an exe and 2 dll files to a C#.NET project, and execute the exe (the exe needs the 2 dll files to run properly) from within the assembly ??
    Saw somewhere a thread, where they talk about extracting the needed files from the assembly to a temporary directory and launch it from there...after program finished remove those files.

    /me is a C# 'beginner' so I don't know how to do that move Wink

    Thx

  • User profile image
    pierlove

    I believe you can using Process() ,, to execute the exe.

    This class is located in the System.Diagnostics namespace.  If its a COM application you may be able to use Interop to expose the exe to your managed code. 

    Here is a MSDN Resource: DiagnosticsProcessClassTopic

    Hope this helps... 

    Jamie

  • User profile image
    DiViNO

    With the process class I'm only able to execute a file outside the assembly (on disk)
    I want to execute the file from the assembly or the second option I gave

  • User profile image
    KevinClement

    Yes this can be done using embedded resources.  I have done this using visual studio.  I'm not sure the syntax to do this using commandline compiler, csc.exe

    In visual studio you can add the binaries to your project, then right click on the binary and open its properties.  Change the build action to embedded resource.  Then that resource will now live in your final assembly.

    Below code is then used to pull that out of the assembly at runtime.

    There is also the idea of the content build action but I have not looked into it any further.  I use this approach for unit testing some times.  For instance, if there is a couple of files that the tests are dependant on, ill compile them into the assembly and guarantee that they'll be there when the test runs.


    -----------------------------------------------------Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream(fileName);

    if(File.Exists(fileName)) File.Delete(fileName);

    BinaryWriter output = new BinaryWriter(new FileStream(fileName,FileMode.CreateNew));

    byte[] buffer = new byte[input.Length];
    input.Read(buffer,0,(int)input.Length);
    output.Write(buffer);

    input.Close();
    output.Close();

  • User profile image
    DiViNO

    Thx KevinClement, you solution worked !

  • User profile image
    miltiadhe

    What if I want to execute the exe without saving it to disk, and the exe has not been made with .net (so that something like this will not work:

    Assembly a = Assembly.Load(bytes);//bytes = the bytes of the exe
    )

    Is there a way to execute an embedded non .net exe without saving it to disk?

    Thanks

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.