I use Costura.Fody.
There is an app Test.exe which runs pocess internalTest.exe this way:
ProcessStartInfo prcInfo = new ProcessStartInfo(strpath)
{
CreateNoWindow = false,
UseShellExecute = true,
Verb = "runas",
WindowStyle = ProcessWindowStyle.Normal
};
var p = Process.Start(prcInfo);
Now I need to provide 2 exe files to user.
Is it possible to embed internalTest.exe and then run it?
Copy the application to a folder within your solution called something like: Resources or EmbeddedResources etc
Set the Build Action to 'Embedded Resource' for that application from the solution explorer.
Now the application will be embedded within your application at build time.
In order to access it at 'Run Time' you need to extract it to a location where you can execute it from.
using (Stream input = thisAssembly.GetManifestResourceStream("Namespace.EmbeddedResources.MyApplication.exe"))
{
byte[] byteData = StreamToBytes(input);
}
/// <summary>
/// StreamToBytes - Converts a Stream to a byte array. Eg: Get a Stream from a file,url, or open file handle.
/// </summary>
/// <param name="input">input is the stream we are to return as a byte array</param>
/// <returns>byte[] The Array of bytes that represents the contents of the stream</returns>
static byte[] StreamToBytes(Stream input)
{
int capacity = input.CanSeek ? (int)input.Length : 0; //Bitwise operator - If can seek, Capacity becomes Length, else becomes 0.
using (MemoryStream output = new MemoryStream(capacity)) //Using the MemoryStream output, with the given capacity.
{
int readLength;
byte[] buffer = new byte[capacity/*4096*/]; //An array of bytes
do
{
readLength = input.Read(buffer, 0, buffer.Length); //Read the memory data, into the buffer
output.Write(buffer, 0, readLength); //Write the buffer to the output MemoryStream incrementally.
}
while (readLength != 0); //Do all this while the readLength is not 0
return output.ToArray(); //When finished, return the finished MemoryStream object as an array.
}
}
Once you have your byte[] for the application inside your parent application, you can use
System.IO.File.WriteAllBytes();
to save the byte array to your hard drive with the file name you want.
You can then use the following to start your application. You may want to use logic to determine if the application exists there already, and try to remove it if it does. If it does exist, just run it without saving over it.
System.Diagnostics.Process.Start(<FILEPATH HERE>);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With