2

I am trying to generate an exe with CSharpCodeProvider, exe that will be a windows forms project. The exe has to have a resource which will be accessed at runtime by the generated assembly. Now to generate that resource I am using ResourceWriter class, then appending it to the compiler options, embeddedResources. But it does not seem to work and I get the MissingManifestResourceException.

Here is the code:

FileStream fs = new FileStream("Form1.resources", FileMode.Create);
        ResourceWriter writer2 = new ResourceWriter(fs);
        writer2.AddResource("bitmap", 3);
        writer2.Generate();
        writer2.Close();

        CSharpCodeProvider code = new CSharpCodeProvider();

        CompilerParameters options = new CompilerParameters();
        options.GenerateExecutable = true;
        options.EmbeddedResources.Add("Form1.resources");

        options.OutputAssembly = @"C:\screen.exe";


        options.GenerateInMemory = false;


        options.TreatWarningsAsErrors = false;

        options.ReferencedAssemblies.Add("System.Windows.Forms.dll");
        options.ReferencedAssemblies.Add("System.dll");
        options.ReferencedAssemblies.Add("System.Drawing.dll");
        //options.ReferencedAssemblies.Add("System.Resources.dll");

        CompilerResults res = code.CompileAssemblyFromFile(options, "Program.cs", "Form1.cs", "Form1.Designer.cs");

Hope someone is able to help me because I have been trying this for over a week. Thank you.

Cristi M
  • 446
  • 4
  • 13
  • 1
    The value 3 is not a bitmap, try using a Bitmap. And a namespace, like "WindowsFormsApplication1.Form1.resource". Use ildasm.exe to see the resulting manifest, compare to a known good assembly generated by the IDE. – Hans Passant Sep 01 '12 at 18:00
  • 1
    Sorry, I later realized that I changed from bitmap to int, just to try it out. That's not a problem anyways but I'll try giving it a namespace. – Cristi M Sep 02 '12 at 10:45
  • Thanks, renaming the file from Form1.resource to namespace.Form1.resource worked. – Cristi M Sep 02 '12 at 10:57

0 Answers0