using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AssemblyRun
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());
//Application.Run(); // 在当前线程上运行应用程序消息循环
// 载入Assembly并启动
string path = AppDomain.CurrentDomain.BaseDirectory + "test2.exe"; // 用于启动的Windows.Forms应用程序
Assembly assembly = getAssembly(path);
startAssembly(assembly);
}
// 获取Assembly
private static Assembly getAssembly(string path)
{
byte[] bytes = File2Bytes(path);
Assembly assembly = Assembly.Load(bytes);
return assembly;
}
// 从Assebly启动
private static void startAssembly(Assembly assembly)
{
string NameSpace = GetNamespace(assembly);
string classFullName = NameSpace + ".Program";
string methodName = "Main";
object[] args = null;
// 调用程序集的静态方法: Type.InvokeMember
Type type = assembly.GetType(classFullName, true, true);
//object[] arg = new object[] { "参数1", "参数2" };
//object tmp = type.InvokeMember(methodName, BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, args);
object tmp = type.InvokeMember(methodName, BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Static, null, null, args);
}
/// <summary>
/// 获取Assembly所在的命名空间名称
/// </summary>
private static string GetNamespace(Assembly asssembly)
{
string Namespace = "";
Namespace = asssembly.EntryPoint.DeclaringType.Namespace;
return Namespace;
}
/// <summary>
/// 将文件转换为byte数组
/// </summary>
/// <param name="path">文件地址</param>
/// <returns>转换后的byte数组</returns>
public static byte[] File2Bytes(string path)
{
if (!File.Exists(path))
{
return new byte[0];
}
FileInfo fi = new FileInfo(path);
byte[] buff = new byte[fi.Length];
FileStream fs = fi.OpenRead();
fs.Read(buff, 0, Convert.ToInt32(fs.Length));
fs.Close();
return buff;
}
}
}
从Assembly入口启动:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FileProcesser2
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AssemblyRun
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
// 载入Assembly并启动
//string path = AppDomain.CurrentDomain.BaseDirectory + "test2.exe"; // 用于启动的Windows.Forms应用程序
String path = @"F:\Program\VS2013_data\FileProcesser\FileProcesser\bin\Debug\notepad++.exe";
Assembly assembly = getAssembly(path);
startAssembly(assembly);
}
/// <summary>
/// 获取Assembly
/// </summary>
private static Assembly getAssembly(string path)
{
byte[] bytes = File2Bytes(path);
Assembly assembly = Assembly.Load(bytes);
return assembly;
}
/// <summary>
/// 从Assebly入口启动
/// </summary>
private static void startAssembly(Assembly assembly)
{
assembly.EntryPoint.Invoke(null, null);
}
/// <summary>
/// 将文件转换为byte数组
/// </summary>
/// <param name="path">文件地址</param>
/// <returns>转换后的byte数组</returns>
public static byte[] File2Bytes(string path)
{
if (!File.Exists(path))
{
return new byte[0];
}
FileInfo fi = new FileInfo(path);
byte[] buff = new byte[fi.Length];
FileStream fs = fi.OpenRead();
fs.Read(buff, 0, Convert.ToInt32(fs.Length));
fs.Close();
return buff;
}
}
}
}
版权声明:本文为scimence原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。