2012-03-29 49 views
1

我想创建一个程序来生成可执行的幻灯片。创建一个生成自定义EXE的.NET程序

因此,我需要它输出一个EXE的一些必需的代码和某些嵌入式资源(图片)。

.NET是否提供这种功能?

+1

MSDN:'System.Reflection.Emit' – 2012-03-29 14:28:48

+0

你确实需要嵌入图片吗?如果没有,最好制作一个程序从一个文件夹中加载图片并显示(全部),您只需要在文件夹中放入新图片以创建新的幻灯片,或为其他文件夹提供程序位置 – ata 2012-03-29 14:32:01

+0

你为什么要这样做?为什么你不能只使用powerpoint和/或查看器与一些[命令行选项](http://office.microsoft.com/en-us/powerpoint-help/command-line-switches-for-powerpoint-2007-和最的PowerPoint查看器-2007-HA010153889.aspx)? – mtijn 2012-03-29 14:43:09

回答

1

您可以使用CSharpCodeProvider类在运行时编译代码并添加嵌入的资源。看看这篇文章,我解释如何做到这一点:SlideShow Builder

0

这会为你生成一个进程具有指定名称(你仍然需要为图片添加代码):

public static Process GenerateRuntimeProcess(string processName, int aliveDuration, bool throwOnException = true) 
    { 
     Process result = null; 
     try 
     { 
      AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName() { Name = processName }, AssemblyBuilderAccess.Save); 
      ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(processName, processName + ".EXE"); 
      TypeBuilder typeBuilder = moduleBuilder.DefineType("Program", TypeAttributes.Public); 
      MethodBuilder methodBuilder = typeBuilder.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static, null, null); 
      ILGenerator il = methodBuilder.GetILGenerator(); 
      il.UsingNamespace("System.Threading"); 
      il.EmitWriteLine("Hello World"); 
      il.Emit(OpCodes.Ldc_I4, aliveDuration); 
      il.Emit(OpCodes.Call, typeof(Thread).GetMethod("Sleep", new Type[] { typeof(int) })); 
      il.Emit(OpCodes.Ret); 
      typeBuilder.CreateType(); 
      assemblyBuilder.SetEntryPoint(methodBuilder.GetBaseDefinition(), PEFileKinds.ConsoleApplication); 
      assemblyBuilder.Save(processName + ".EXE", PortableExecutableKinds.Required32Bit, ImageFileMachine.I386); 
      result = Process.Start(new ProcessStartInfo(processName + ".EXE") 
      { 
       WindowStyle = ProcessWindowStyle.Hidden 
      }); 
     } 
     catch 
     { 
      if (throwOnException) 
      { 
       throw; 
      } 
      result = null; 
     } 
     return result; 
    } 

可以findmore上System.Reflection.Emit信息在MSDN here或教程herehere

如果我是你,我也会考虑使用powerpoint和/或查看器应用程序和一些命令行选项作为详细here。也许你不需要“制作一个应用程序,使另一个应用程序是幻灯片放映”。

1

这很容易完成。

您可以将图片添加为嵌入资源,然后使用Reflection技术来发现和检索嵌入的图片。

因此,您编写的程序与图片列表无关,图片列表只是嵌入的资源。您可以使用Visual Studio将图片嵌入为资源,或者创建一个自定义程序来执行此操作。

你可以在http://msdn.microsoft.com/en-us/library/aa287676(v=VS.71).aspxhttp://www.java2s.com/Code/CSharp/Development-Class/Saveandloadimagefromresourcefile.htm找到一些例子。

祝你好运!