none
C# Add a resource to assembly during runtime?

    Question

  • Basically I want to show a GUI at run time allowing the user to select an image with a button called "Save", when "Save" is clicked I want to be able to take the image off the disk and load it into the assembly as a resource so the next time the program is run the image is automatically shown on the screen without needing the physical file on the disk but rather an embedded copy in the resources of the executable.
    I believe I need to use ResourceWriter but I have no idea how I can implement this to save the resource into the exe file during run-time? Would I need to write a new executable file or recompile again?

    Thanks,
    Yoshistr
    Sunday, May 31, 2009 7:38 PM

Answers

  • Yes, that's possible.  You can run the compiler with the CSharpCodeProvider class.  Write a program first that does this with some sample resources.  Pay attention to the arguments passed to the compiler, they are visible in the Output window when you build the program.  Use CSharpCodeProvider.CompileAssemblyFromFile(), passing CompilerParameters with those same options.

    Hans Passant.
    • Marked as answer by BlkDemon Sunday, May 31, 2009 11:34 PM
    Sunday, May 31, 2009 9:03 PM

All replies

  • Hmm, technically this is all possible, just dramatically inefficient.  The resource needs to be embedded in a .dll, which requires running Al.exe.  Which requires de-compiling the original DLL first.  There is no way that you can get it into your .exe, it is locked while your program is running.  Furthermore, writing a DLL is troublesome, you don't usually have write access to the folder.  Restricted user accounts, like those enforced by Vista UAC, only lets you read from folders that contain executables.

    More to the point, since you need to write to a separate file anyway, you might as well make that an image file instead of a DLL.  That also saves you the trouble, and delay, of extracting the image from the DLL.  Keep it simple, just copy the image file.

    Hans Passant.
    Sunday, May 31, 2009 8:10 PM
  • I would like to create an external file but it can only be an executable file because the user will send it to someone to show them a slideshow of images, so I need to load all images directly into a single exe file that the user can add images to and then send a single exe to a friend who can open it showing them a slideshow by using the resources. It is easy enough to add the images at run-time but I want to let the user select the images to show in the slideshow, which means I need to add the files into the exe based on the user's selected files.

    Please help me out here, I've seen people do it with C# by adding the resource images and compiling the program manually and writing out the exe slideshow to a different file instead of overwriting the original and it works fine. Do I need to use a C# runtime compiler to compile everything including the resources? This seems too tedious for something that should be simple enough...

    Thanks for any assistance.
    Sunday, May 31, 2009 8:15 PM
  • Yes, that's possible.  You can run the compiler with the CSharpCodeProvider class.  Write a program first that does this with some sample resources.  Pay attention to the arguments passed to the compiler, they are visible in the Output window when you build the program.  Use CSharpCodeProvider.CompileAssemblyFromFile(), passing CompilerParameters with those same options.

    Hans Passant.
    • Marked as answer by BlkDemon Sunday, May 31, 2009 11:34 PM
    Sunday, May 31, 2009 9:03 PM