问题
I want to create a program that produces an executable slideshow.
So I need it to output an EXE with some required code and certain embedded resources (pictures) in it.
Does .NET provide such capability?
回答1:
You can use CSharpCodeProvider class to compile code at runtime and add embedded resources. Have a look at this article where I explain how to do it: SlideShow Builder
回答2:
This is easy to accomplish.
You can add pictures as embedded resources and then use the technique of Reflection to discover and retrieve the embedded pictures.
So the program you write is independent of the list of pictures, which are just embedded resources. You can embed pictures as resources using Visual Studio, or create a custom program to do it.
You can find some examples at http://msdn.microsoft.com/en-us/library/aa287676(v=VS.71).aspx and http://www.java2s.com/Code/CSharp/Development-Class/Saveandloadimagefromresourcefile.htm.
Good luck!
回答3:
Like what SK-Logic said there is
http://msdn.microsoft.com/en-us/library/system.reflection.emit.aspx
here is example of that
http://olondono.blogspot.com/2008/02/creating-code-at-runtime.html
You could also create a the project file and create the code files and use the Process class to call the compiler if you want help doing this I can give an example
回答4:
this will generate a process for you with the specified name (you'll still need to add code for the pictures):
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;
}
you can findmore info on System.Reflection.Emit on MSDN here or a tutorial here or here.
if I were you I'd also look into just using powerpoint and/or the viewer app and some command line options as detailed here. maybe you don't need to "make an app that makes another app that is a slideshow" at all..
来源:https://stackoverflow.com/questions/9927403/create-a-net-program-that-produces-custom-exes