将应用程序复制到解决方案中的文件夹中,例如:资源(Resources)或嵌入式资源(EmbeddedResources)等。
从解决方案资源管理器中将它的构建操作设置为“嵌入式资源”(Embedded Resource)。
现在,在生成时该应用程序将会被嵌入到你的应用程序中。
为了在“运行时”访问它,你需要将其提取到可以执行的位置。
using (Stream input = thisAssembly.GetManifestResourceStream("Namespace.EmbeddedResources.MyApplication.exe"))
{
byte[] byteData = StreamToBytes(input);
}
static byte[] StreamToBytes(Stream input)
{
int capacity = input.CanSeek ? (int)input.Length : 0;
using (MemoryStream output = new MemoryStream(capacity))
{
int readLength;
byte[] buffer = new byte[capacity];
do
{
readLength = input.Read(buffer, 0, buffer.Length);
output.Write(buffer, 0, readLength);
}
while (readLength != 0);
return output.ToArray();
}
}
一旦在父应用程序中获取了应用程序的byte[],您可以使用
System.IO.File.WriteAllBytes()
要将字节数组保存到您想要的文件名的硬盘上,请使用以下方法。
然后,您可以使用以下方式启动应用程序。您可能希望使用逻辑来确定是否已经存在该应用程序,并在存在时尝试删除它。如果已存在,则无需覆盖它,只需运行即可。
System.Diagnostics.Process.Start(<FILEPATH HERE>);