1

I would like to embed all files/resources into a single exe (2 exe's and a ps1)

Is it possible ? and how would one go about doing it, currently I have managed to publish the project but there are additional files ( Application Files, Application Manifest and setup exe) in the output - this I would like to embed into single exe.

Been doing some readings, seems I may need to embed the resources and perhaps create pointers? or embedding image resources in the project, not sure if I am on right track and/or how to start the code..

What I have so far

private Boolean Install_certificate()
{
    try
    {
        var newProcessInfo = new System.Diagnostics.ProcessStartInfo()
        {
            FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe",
            Verb = "runas",
            Arguments = (@"–ExecutionPolicy Bypass ""script.ps1""")

        };
        System.Diagnostics.Process.Start(newProcessInfo);


        // Install the cert & change proxy settings
        //System.Diagnostics.Process.Start(@"script.ps1" );
        return true;
    }
    catch
    {
        return false;
    }
}
private Boolean Install_exe1()
{
    try
    {
        // Install exe1
        System.Diagnostics.Process.Start(@"setup1.exe");

        return true;

    }
    catch
    {
        return false;
    }
}
private Boolean Install_exe2()
{
    try
    {
        // Install exe2
        System.Diagnostics.Process.Start(@"setup2.exe");
        return true;
    }
    catch
    {
        return false;
    }
}

Appreciate the help!

Thanks

Hex02
  • 45
  • 8

2 Answers2

1

You can use System.Diagnostics.Process to start .exe files.

You can use System.Management.Automation to run power shell scripts.

If your exes work with their dependencies from a command prompt you shouldn't have a problem in C#

If you want to embed these files into your c# project you can add them as resources and when your program runs you can save their binary content to an exe file and then execute them:

In a nutshell, right click on your project, choose properties and then click on resources. Click the link to add a default resource file to your project,

Then in the resource designer select the files, then add resource and add the files you wish to embed. This will create a Resources file that will have your files embedded in them that you can access as a byte[] from in your application.

enter image description here

Then when you application starts save them to disk and run them:

using System;
using System.IO;
using System.Diagnostics;
namespace myNs
{

    class Program
    {
        static void unpack()
        {
            if (!File.Exists("exe1.exe"))
                File.WriteAllBytes("exe1.exe", myNs.Properties.Resources.exe1);
            if (!File.Exists("exe2.exe"))
                File.WriteAllBytes("exe2.exe", myNs.Properties.Resources.exe2);
            if (!File.Exists("ps1.ps")) if (!File.Exists("ps1.ps"))
                    File.WriteAllBytes("ps1.ps", myNs.Properties.Resources.ps1);
        }
        static void Main(string[] args)
        {
            unpack();
            Process process = new Process();
            process.StartInfo.FileName = "exe1.exe";
            process.Start();
            process.WaitForExit();

            process = new Process();
            process.StartInfo.FileName = "exe2.exe";
            process.Start();
            process.WaitForExit();

            process = new Process();
            process.StartInfo.FileName = @"C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe";
            process.StartInfo.Arguments = "ps1.ps1";
            process.Start();
            process.WaitForExit();
        }
    }
}
Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41
  • Yes I am using System.Diagnostics.Process and System.Management.Automation, my question is regarding compiling the exe's + powershell into a single exe without the additional files. – Hex02 Jul 13 '17 at 01:56
  • Is there an additional question or problem? I answered how you could accomplish it and posted links to how to use the code to accomplish the task. Compiling different exe's into one exe is not the same as executing them from a single exe. – Alexander Higgins Jul 13 '17 at 01:59
  • Executing them from a single exe - That's the question/task – Hex02 Jul 13 '17 at 02:05
  • Your code will execute them from a single exe. Do you mean that you want all of your files to be embedded into a single exe? If so put that in your question. Otherwise, clarify your question to explain what you mean. – Alexander Higgins Jul 13 '17 at 02:09
  • Great thanks for the clarification, I have updated the question. – Hex02 Jul 13 '17 at 02:18
  • Updated to given an example of how to embed exes as a resource and run them. – Alexander Higgins Jul 13 '17 at 02:48
  • Thanks Alexander, I went with your suggestion and tried to implement it, however I'm having a problem stating..."Program has more than one entry point defined. Compile with main to specify the type that contains the entry point" - However I can only see one "Main". – Hex02 Jul 18 '17 at 01:42
  • Right click on your project and choose properties. In application select you main method in the startup object drop down. If you still have problems you might have to embed your resources as base64 strings. https://stackoverflow.com/questions/11743160/how-do-i-encode-and-decode-a-base64-string – Alexander Higgins Jul 18 '17 at 01:55
0

my question is regarding compiling the exe's + powershell into a single exe without the additional files

You can add the ps1 script as a resource like Alexander Higgins illustrates.

You can merge 2 assemblies together using the tool ILMerge from Microsoft. Here we merge Primary.dll Secondary.dll (and etc) into Merged.dll with a log.txt out of the merger.

ilmerge /log:log.txt /out:Merged.dll Primary.dll Secondary.dll 

Because EXEs have an entry point you cant megre 2 EXEs, to workaround this simply change one of the EXEs to be a DLL (class library).

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Sorry I dont quite understand your suggestion, what would be the use of changing the exe to dll ? – Hex02 Jul 18 '17 at 01:04
  • Read the Details section in the link I posted *The first assembly in the list of input assemblies is the primary assembly. When the primary assembly is an executable, then the target assembly is created as an executable with the same entry point as the primary assembly.* - in other words you cant have > 1 exe because there is only one `Main()` – Jeremy Thompson Jul 18 '17 at 01:38