将应用程序复制到解决方案中的文件夹,例如:Resources 或 EmbeddedResources 等
从解决方案资源管理器中将该应用程序的构建操作设置为“嵌入式资源”。
现在应用程序将在构建时嵌入到您的应用程序中。
为了在“运行时”访问它,您需要将其提取到可以执行它的位置。
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.
}
}
在父应用程序中获得应用程序的字节 [] 后,您可以使用
System.IO.File.WriteAllBytes();
使用您想要的文件名将字节数组保存到硬盘驱动器。
然后,您可以使用以下内容启动您的应用程序。您可能希望使用逻辑来确定应用程序是否已经存在,如果存在则尝试将其删除。如果它确实存在,只需运行它而不保存它。
System.Diagnostics.Process.Start(<FILEPATH HERE>);