C# 操作嵌入的资源

被嵌入的程序执行文件

 1 namespace AppTest
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             Console.WriteLine("args.Length:" + args.Length);
 8             if (args.Length > 0)
 9             {
10                 for (int i = 0; i < args.Length; i++)
11                 {
12                     Console.WriteLine("args[" + i + "]:" + args[i]);
13                 }
14             }
15             Console.WriteLine("结束");
16             Console.ReadLine();
17         }
18     }
19 }

将AppTest.exe 加入到LoadExeApp嵌入资源中

 1 namespace LoadExeApp
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             try
 8             {
 9                 args = new string[] { "A", "B", "C", "D" };
10                 object[] abjs = new object[] { args }; //如果不用object[]将args包装,将会报“参数计数出错”的异常
11 
12                 Assembly asm = Assembly.Load(Resource1.AppTest);
13                 MethodInfo info = asm.EntryPoint;
14                 ParameterInfo[] parameters = info.GetParameters();
15                 if ((parameters != null) && (parameters.Length > 0))
16                     info.Invoke(null, abjs);
17                 else
18                     info.Invoke(null, null);
19             }
20             catch (Exception ex)
21             {
22                 Console.WriteLine(ex.Message);
23             }
         Console.WriteLine("LoadExeApp结束");
24 Console.ReadLine(); 25 } 26 } 27 }

运行LoadExeApp即可调用

如图:

原文地址:https://www.cnblogs.com/yf2011/p/4447327.html