C# 从Assembly启动EXE(应用加壳)

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;
            }
        }
    }

}

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值